feat(schematics): sort apps by type and name
This commit is contained in:
parent
eb9f7b2436
commit
148049f3e2
@ -6,6 +6,7 @@ import * as path from 'path';
|
|||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
import {addBootstrapToModule} from '@schematics/angular/utility/ast-utils';
|
import {addBootstrapToModule} from '@schematics/angular/utility/ast-utils';
|
||||||
import {insertImport} from '@schematics/angular/utility/route-utils';
|
import {insertImport} from '@schematics/angular/utility/route-utils';
|
||||||
|
import {addApp} from '../utility/config-file-utils';
|
||||||
|
|
||||||
function addBootstrap(path: string): Rule {
|
function addBootstrap(path: string): Rule {
|
||||||
return (host: Tree) => {
|
return (host: Tree) => {
|
||||||
@ -35,7 +36,13 @@ function addNxModule(path: string): Rule {
|
|||||||
}
|
}
|
||||||
function addAppToAngularCliJson(options: Schema): Rule {
|
function addAppToAngularCliJson(options: Schema): Rule {
|
||||||
return (host: Tree) => {
|
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,
|
'name': options.name,
|
||||||
'root': path.join('apps', options.name, options.sourceDir),
|
'root': path.join('apps', options.name, options.sourceDir),
|
||||||
'outDir': `dist/apps/${options.name}`,
|
'outDir': `dist/apps/${options.name}`,
|
||||||
@ -51,18 +58,7 @@ function addAppToAngularCliJson(options: Schema): Rule {
|
|||||||
'scripts': [],
|
'scripts': [],
|
||||||
'environmentSource': 'environments/environment.ts',
|
'environmentSource': 'environments/environment.ts',
|
||||||
'environments': {'dev': 'environments/environment.ts', 'prod': 'environments/environment.prod.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));
|
host.overwrite('.angular-cli.json', JSON.stringify(json, null, 2));
|
||||||
return host;
|
return host;
|
||||||
|
|||||||
@ -2,26 +2,22 @@ import {apply, branchAndMerge, chain, externalSchematic, mergeWith, move, Rule,
|
|||||||
import {Schema} from './schema';
|
import {Schema} from './schema';
|
||||||
import {names, toFileName} from '@nrwl/schematics';
|
import {names, toFileName} from '@nrwl/schematics';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import {addApp} from '../utility/config-file-utils';
|
||||||
|
|
||||||
function addLibToAngularCliJson(options: Schema): Rule {
|
function addLibToAngularCliJson(options: Schema): Rule {
|
||||||
return (host: Tree) => {
|
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')) {
|
if (!host.exists('.angular-cli.json')) {
|
||||||
throw new Error('Missing .angular-cli.json');
|
throw new Error('Missing .angular-cli.json');
|
||||||
}
|
}
|
||||||
|
|
||||||
const sourceText = host.read('.angular-cli.json')!.toString('utf-8');
|
const sourceText = host.read('.angular-cli.json')!.toString('utf-8');
|
||||||
const json = JSON.parse(sourceText);
|
const json = JSON.parse(sourceText);
|
||||||
if (!json['apps']) {
|
json.apps = addApp(json.apps, {
|
||||||
json['apps'] = [];
|
'name': options.name,
|
||||||
}
|
'root': path.join('libs', options.name, options.sourceDir),
|
||||||
json['apps'].push(appConfig);
|
'test': '../../../test.js',
|
||||||
|
'appRoot': ''
|
||||||
|
});
|
||||||
|
|
||||||
host.overwrite('.angular-cli.json', JSON.stringify(json, null, 2));
|
host.overwrite('.angular-cli.json', JSON.stringify(json, null, 2));
|
||||||
return host;
|
return host;
|
||||||
|
|||||||
23
packages/schematics/src/utility/config-file-utils.spec.ts
Normal file
23
packages/schematics/src/utility/config-file-utils.spec.ts
Normal 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'}
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
15
packages/schematics/src/utility/config-file-utils.ts
Normal file
15
packages/schematics/src/utility/config-file-utils.ts
Normal 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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user