feat(schematics): sort apps by type and name

This commit is contained in:
vsavkin 2017-10-05 10:28:48 -04:00
parent eb9f7b2436
commit 148049f3e2
4 changed files with 54 additions and 24 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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'}
]);
});
});
});

View File

@ -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;
}