Loading libs/config/ng-package.json +4 −1 Original line number Diff line number Diff line Loading @@ -6,6 +6,9 @@ }, "allowedNonPeerDependencies": [ "@rxap/schematics-utilities", "@rxap/schematics-ts-morph", "@angular-devkit/schematics", "ts-morph", "tslib" ] } libs/config/package.json +5 −2 Original line number Diff line number Diff line Loading @@ -40,6 +40,9 @@ }, "dependencies": { "@rxap/schematics-utilities": "^12.1.0", "tslib": "^2.2.0" "@angular-devkit/schematics": "^12.1.1", "tslib": "^2.2.0", "ts-morph": "^11.0.0", "@rxap/schematics-ts-morph": "^12.1.0" } } libs/config/src/schematics/ng-add/index.ts +93 −4 Original line number Diff line number Diff line import { Rule } from '@angular-devkit/schematics'; import { InstallPeerDependencies } from '@rxap/schematics-utilities'; import { Rule, Tree, chain, SchematicsException } from '@angular-devkit/schematics'; import { InstallPeerDependencies, GetProjectSourceRoot, UpdateAngularJson } from '@rxap/schematics-utilities'; import { NgAddSchema } from './schema'; import { Project, QuoteKind, IndentationText } from 'ts-morph'; import { AddDir, ApplyTsMorphProject, AddToArray } from '@rxap/schematics-ts-morph'; import { join } from 'path'; export default function(): Rule { return InstallPeerDependencies(); export default function(options: NgAddSchema): Rule { return (host: Tree) => { const rules: Rule[] = [ InstallPeerDependencies() ]; if (options.project) { const projectSourceRoot = GetProjectSourceRoot(host, options.project); const project = new Project({ useInMemoryFileSystem: true, manipulationSettings: { quoteKind: QuoteKind.Single, indentationText: IndentationText.TwoSpaces } }); AddDir(host.getDir(projectSourceRoot), project); const mainSourceFile = project.getSourceFile('/main.ts'); if (!mainSourceFile) { throw new SchematicsException(`Could not find the main.ts source file in '[projectSourceRoot]/main.ts'`); } AddToArray( mainSourceFile, 'setup', 'ConfigService.Load()', 'Promise<any>[]' ); mainSourceFile.addImportDeclarations([ { namedImports: [ 'ConfigService' ], moduleSpecifier: '@rxap/config' } ]); rules.push(ApplyTsMorphProject(project, projectSourceRoot)); const configFilePath = join(projectSourceRoot, 'config.json'); rules.push(UpdateAngularJson(angular => { const p = angular.projects.get(options.project!); if (p) { const buildTarget = p.targets.get('build'); if (buildTarget) { if (!buildTarget.options.assets) { buildTarget.options.assets = []; } const assets: string[] = buildTarget.options.assets; if (!assets.includes(configFilePath)) { assets.push(configFilePath); } } } })); rules.push(tree => { if (!tree.exists(configFilePath)) { tree.create(configFilePath, '{}'); } }); } return chain(rules); }; } libs/config/src/schematics/ng-add/schema.d.ts +1 −0 Original line number Diff line number Diff line export interface NgAddSchema { project?: string; } libs/config/src/schematics/ng-add/schema.json +9 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,14 @@ "$schema": "http://json-schema.org/draft-07/schema", "$id": "config-ng-add", "type": "object", "properties": {}, "properties": { "project": { "type": "string", "description": "The project where the environment feature should be added", "$default": { "$source": "projectName" } } }, "required": [] } Loading
libs/config/ng-package.json +4 −1 Original line number Diff line number Diff line Loading @@ -6,6 +6,9 @@ }, "allowedNonPeerDependencies": [ "@rxap/schematics-utilities", "@rxap/schematics-ts-morph", "@angular-devkit/schematics", "ts-morph", "tslib" ] }
libs/config/package.json +5 −2 Original line number Diff line number Diff line Loading @@ -40,6 +40,9 @@ }, "dependencies": { "@rxap/schematics-utilities": "^12.1.0", "tslib": "^2.2.0" "@angular-devkit/schematics": "^12.1.1", "tslib": "^2.2.0", "ts-morph": "^11.0.0", "@rxap/schematics-ts-morph": "^12.1.0" } }
libs/config/src/schematics/ng-add/index.ts +93 −4 Original line number Diff line number Diff line import { Rule } from '@angular-devkit/schematics'; import { InstallPeerDependencies } from '@rxap/schematics-utilities'; import { Rule, Tree, chain, SchematicsException } from '@angular-devkit/schematics'; import { InstallPeerDependencies, GetProjectSourceRoot, UpdateAngularJson } from '@rxap/schematics-utilities'; import { NgAddSchema } from './schema'; import { Project, QuoteKind, IndentationText } from 'ts-morph'; import { AddDir, ApplyTsMorphProject, AddToArray } from '@rxap/schematics-ts-morph'; import { join } from 'path'; export default function(): Rule { return InstallPeerDependencies(); export default function(options: NgAddSchema): Rule { return (host: Tree) => { const rules: Rule[] = [ InstallPeerDependencies() ]; if (options.project) { const projectSourceRoot = GetProjectSourceRoot(host, options.project); const project = new Project({ useInMemoryFileSystem: true, manipulationSettings: { quoteKind: QuoteKind.Single, indentationText: IndentationText.TwoSpaces } }); AddDir(host.getDir(projectSourceRoot), project); const mainSourceFile = project.getSourceFile('/main.ts'); if (!mainSourceFile) { throw new SchematicsException(`Could not find the main.ts source file in '[projectSourceRoot]/main.ts'`); } AddToArray( mainSourceFile, 'setup', 'ConfigService.Load()', 'Promise<any>[]' ); mainSourceFile.addImportDeclarations([ { namedImports: [ 'ConfigService' ], moduleSpecifier: '@rxap/config' } ]); rules.push(ApplyTsMorphProject(project, projectSourceRoot)); const configFilePath = join(projectSourceRoot, 'config.json'); rules.push(UpdateAngularJson(angular => { const p = angular.projects.get(options.project!); if (p) { const buildTarget = p.targets.get('build'); if (buildTarget) { if (!buildTarget.options.assets) { buildTarget.options.assets = []; } const assets: string[] = buildTarget.options.assets; if (!assets.includes(configFilePath)) { assets.push(configFilePath); } } } })); rules.push(tree => { if (!tree.exists(configFilePath)) { tree.create(configFilePath, '{}'); } }); } return chain(rules); }; }
libs/config/src/schematics/ng-add/schema.d.ts +1 −0 Original line number Diff line number Diff line export interface NgAddSchema { project?: string; }
libs/config/src/schematics/ng-add/schema.json +9 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,14 @@ "$schema": "http://json-schema.org/draft-07/schema", "$id": "config-ng-add", "type": "object", "properties": {}, "properties": { "project": { "type": "string", "description": "The project where the environment feature should be added", "$default": { "$source": "projectName" } } }, "required": [] }