diff --git a/packages/schematics/src/app/index.ts b/packages/schematics/src/app/index.ts index c8c50b8629..1567a3d1bf 100644 --- a/packages/schematics/src/app/index.ts +++ b/packages/schematics/src/app/index.ts @@ -6,6 +6,7 @@ import * as path from 'path'; import * as ts from 'typescript'; import {addBootstrapToModule} from '@schematics/angular/utility/ast-utils'; import {insertImport} from '@schematics/angular/utility/route-utils'; +import {addApp} from '../utility/config-file-utils'; function addBootstrap(path: string): Rule { return (host: Tree) => { @@ -35,7 +36,13 @@ function addNxModule(path: string): Rule { } function addAppToAngularCliJson(options: Schema): Rule { return (host: Tree) => { - const appConfig = { + if (!host.exists('.angular-cli.json')) { + throw new Error('Missing .angular-cli.json'); + } + + const sourceText = host.read('.angular-cli.json')!.toString('utf-8'); + const json = JSON.parse(sourceText); + json.apps = addApp(json.apps, { 'name': options.name, 'root': path.join('apps', options.name, options.sourceDir), 'outDir': `dist/apps/${options.name}`, @@ -51,18 +58,7 @@ function addAppToAngularCliJson(options: Schema): Rule { 'scripts': [], 'environmentSource': 'environments/environment.ts', 'environments': {'dev': 'environments/environment.ts', 'prod': 'environments/environment.prod.ts'} - }; - - if (!host.exists('.angular-cli.json')) { - throw new Error('Missing .angular-cli.json'); - } - - const sourceText = host.read('.angular-cli.json')!.toString('utf-8'); - const json = JSON.parse(sourceText); - if (!json['apps']) { - json['apps'] = []; - } - json['apps'].push(appConfig); + }); host.overwrite('.angular-cli.json', JSON.stringify(json, null, 2)); return host; diff --git a/packages/schematics/src/lib/index.ts b/packages/schematics/src/lib/index.ts index 3f83af7d1a..99f5428641 100644 --- a/packages/schematics/src/lib/index.ts +++ b/packages/schematics/src/lib/index.ts @@ -2,26 +2,22 @@ import {apply, branchAndMerge, chain, externalSchematic, mergeWith, move, Rule, import {Schema} from './schema'; import {names, toFileName} from '@nrwl/schematics'; import * as path from 'path'; +import {addApp} from '../utility/config-file-utils'; function addLibToAngularCliJson(options: Schema): Rule { return (host: Tree) => { - const appConfig = { - 'name': options.name, - 'root': path.join('libs', options.name, options.sourceDir), - 'test': '../../../test.js', - 'appRoot': '' - }; - if (!host.exists('.angular-cli.json')) { throw new Error('Missing .angular-cli.json'); } const sourceText = host.read('.angular-cli.json')!.toString('utf-8'); const json = JSON.parse(sourceText); - if (!json['apps']) { - json['apps'] = []; - } - json['apps'].push(appConfig); + json.apps = addApp(json.apps, { + 'name': options.name, + 'root': path.join('libs', options.name, options.sourceDir), + 'test': '../../../test.js', + 'appRoot': '' + }); host.overwrite('.angular-cli.json', JSON.stringify(json, null, 2)); return host; diff --git a/packages/schematics/src/utility/config-file-utils.spec.ts b/packages/schematics/src/utility/config-file-utils.spec.ts new file mode 100644 index 0000000000..6e6fe298f9 --- /dev/null +++ b/packages/schematics/src/utility/config-file-utils.spec.ts @@ -0,0 +1,23 @@ +import {addApp} from './config-file-utils'; + +describe('configFileUtils', () => { + describe('sortApps', () => { + it('should handle undefined', () => { + expect(addApp(undefined, {name: 'a'})).toEqual([{name: 'a'}]); + }); + + it('should handle an empty array', () => { + expect(addApp([], {name: 'a'})).toEqual([{name: 'a'}]); + }); + + it('should sort apps by name', () => { + expect(addApp([{name: 'a'}, {name: 'b'}], {name: 'c'})).toEqual([{name: 'a'}, {name: 'b'}, {name: 'c'}]); + }); + + it('should prioritize apps with "main" defined', () => { + expect(addApp([{name: 'c'}, {name: 'a'}, {name: 'a', main: 'a'}], {name: 'b', main: 'b'})).toEqual([ + {name: 'a', main: 'a'}, {name: 'b', main: 'b'}, {name: 'a'}, {name: 'c'} + ]); + }); + }); +}); diff --git a/packages/schematics/src/utility/config-file-utils.ts b/packages/schematics/src/utility/config-file-utils.ts new file mode 100644 index 0000000000..f7e215134b --- /dev/null +++ b/packages/schematics/src/utility/config-file-utils.ts @@ -0,0 +1,15 @@ +export function addApp(apps: any[]|undefined, newApp: any): any[] { + if (!apps) { + apps = []; + } + apps.push(newApp); + + apps.sort((a: any, b: any) => { + if (a.main && !b.main) return -1; + if (!a.main && b.main) return 1; + if (a.name > b.name) return 1; + return -1; + }); + + return apps; +}