From c0ec046b0b122ebc4400bca7155476143147246b Mon Sep 17 00:00:00 2001 From: listepo-alterpost Date: Tue, 30 Oct 2018 11:47:02 +0200 Subject: [PATCH] fix(schematics): added skipModulePrefix --- .../src/collection/library/index.ts | 38 ++-- .../src/collection/library/library.spec.ts | 174 ++++++++++++++++++ .../src/collection/library/schema.d.ts | 1 + .../src/collection/library/schema.json | 5 + 4 files changed, 203 insertions(+), 15 deletions(-) diff --git a/packages/schematics/src/collection/library/index.ts b/packages/schematics/src/collection/library/index.ts index e4addb2a9db3f..24e00a938222d 100644 --- a/packages/schematics/src/collection/library/index.ts +++ b/packages/schematics/src/collection/library/index.ts @@ -37,6 +37,7 @@ import { move } from '../../utils/rules/move'; interface NormalizedSchema extends Schema { name: string; + fileName: string; projectRoot: string; entryFile: string; modulePath: string; @@ -83,7 +84,7 @@ function addRouterConfiguration(options: NormalizedSchema): Rule { ts.ScriptTarget.Latest, true ); - const constName = `${toPropertyName(options.name)}Routes`; + const constName = `${toPropertyName(options.fileName)}Routes`; insert(host, options.modulePath, [ insertImport( @@ -131,7 +132,9 @@ function addLoadChildren(options: NormalizedSchema): Rule { ...addRoute( options.parentModule, sourceFile, - `{path: '${toFileName(options.name)}', loadChildren: '${loadChildren}'}` + `{path: '${toFileName( + options.fileName + )}', loadChildren: '${loadChildren}'}` ) ]); @@ -188,7 +191,7 @@ function addChildren(options: NormalizedSchema): Rule { ts.ScriptTarget.Latest, true ); - const constName = `${toPropertyName(options.name)}Routes`; + const constName = `${toPropertyName(options.fileName)}Routes`; const importPath = `@${npmScope}/${options.projectDirectory}`; insert(host, options.parentModule, [ @@ -196,7 +199,7 @@ function addChildren(options: NormalizedSchema): Rule { ...addRoute( options.parentModule, sourceFile, - `{path: '${toFileName(options.name)}', children: ${constName}}` + `{path: '${toFileName(options.fileName)}', children: ${constName}}` ) ]); return host; @@ -241,8 +244,9 @@ function updateProject(options: NormalizedSchema): Rule { } if (options.module) { - host.overwrite( - path.join(libRoot, `${options.name}.module.ts`), + host.delete(path.join(libRoot, `${options.name}.module.ts`)); + host.create( + path.join(libRoot, `${options.fileName}.module.ts`), ` import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; @@ -258,10 +262,10 @@ function updateProject(options: NormalizedSchema): Rule { if (options.unitTestRunner !== 'none') { host.create( - path.join(libRoot, `${options.name}.module.spec.ts`), + path.join(libRoot, `${options.fileName}.module.spec.ts`), ` import { async, TestBed } from '@angular/core/testing'; - import { ${options.moduleName} } from './${options.name}.module'; + import { ${options.moduleName} } from './${options.fileName}.module'; describe('${options.moduleName}', () => { beforeEach(async(() => { @@ -281,11 +285,11 @@ function updateProject(options: NormalizedSchema): Rule { host.overwrite( `${options.projectRoot}/src/index.ts`, ` - export * from './lib/${options.name}.module'; + export * from './lib/${options.fileName}.module'; ` ); } else { - host.delete(path.join(libRoot, `${options.name}.module.ts`)); + host.delete(path.join(libRoot, `${options.fileName}.module.ts`)); host.create(path.join(libRoot, `.gitkeep`), ''); host.overwrite(`${options.projectRoot}/src/index.ts`, ''); } @@ -454,17 +458,20 @@ export default function(schema: Schema): Rule { } function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { + const name = toFileName(options.name); const projectDirectory = options.directory - ? `${toFileName(options.directory)}/${toFileName(options.name)}` - : toFileName(options.name); + ? `${toFileName(options.directory)}/${name}` + : name; const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-'); + const fileName = options.skipModulePrefix ? name : projectName; const projectRoot = `libs/${projectDirectory}`; - const moduleName = `${toClassName(projectName)}Module`; + + const moduleName = `${toClassName(fileName)}Module`; const parsedTags = options.tags ? options.tags.split(',').map(s => s.trim()) : []; - const modulePath = `${projectRoot}/src/lib/${projectName}.module.ts`; + const modulePath = `${projectRoot}/src/lib/${fileName}.module.ts`; const defaultPrefix = getNpmScope(host); return { @@ -476,6 +483,7 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { moduleName, projectDirectory, modulePath, - parsedTags + parsedTags, + fileName }; } diff --git a/packages/schematics/src/collection/library/library.spec.ts b/packages/schematics/src/collection/library/library.spec.ts index 4d9acaac15cf6..dc221f5f583c6 100644 --- a/packages/schematics/src/collection/library/library.spec.ts +++ b/packages/schematics/src/collection/library/library.spec.ts @@ -137,6 +137,30 @@ describe('lib', () => { expect( tree.exists('libs/my-lib/src/lib/my-lib.service.spec.ts') ).toBeFalsy(); + + const tree2 = schematicRunner.runSchematic( + 'lib', + { name: 'myLib2', skipModulePrefix: true }, + tree + ); + expect(tree2.exists(`libs/my-lib2/karma.conf.js`)).toBeTruthy(); + expect(tree2.exists('libs/my-lib2/src/index.ts')).toBeTruthy(); + expect( + tree2.exists('libs/my-lib2/src/lib/my-lib2.module.ts') + ).toBeTruthy(); + + expect( + tree.exists('libs/my-lib2/src/lib/my-lib2.component.ts') + ).toBeFalsy(); + expect( + tree.exists('libs/my-lib2/src/lib/my-lib2.component.spec.ts') + ).toBeFalsy(); + expect( + tree2.exists('libs/my-lib2/src/lib/my-lib2.service.ts') + ).toBeFalsy(); + expect( + tree2.exists('libs/my-lib2/src/lib/my-lib2.service.spec.ts') + ).toBeFalsy(); }); it('should not generate a module for --module false', () => { @@ -179,6 +203,46 @@ describe('lib', () => { }); describe('nested', () => { + it('should update nx.json', () => { + const tree = schematicRunner.runSchematic( + 'lib', + { name: 'myLib', directory: 'myDir', tags: 'one' }, + appTree + ); + const nxJson = readJsonInTree(tree, '/nx.json'); + expect(nxJson).toEqual({ + npmScope: 'proj', + projects: { + 'my-dir-my-lib': { + tags: ['one'] + } + } + }); + + const tree2 = schematicRunner.runSchematic( + 'lib', + { + name: 'myLib2', + directory: 'myDir', + tags: 'one,two', + skipModulePrefix: true + }, + tree + ); + const nxJson2 = readJsonInTree(tree2, '/nx.json'); + expect(nxJson2).toEqual({ + npmScope: 'proj', + projects: { + 'my-dir-my-lib': { + tags: ['one'] + }, + 'my-dir-my-lib2': { + tags: ['one', 'two'] + } + } + }); + }); + it('should generate files', () => { const tree = schematicRunner.runSchematic( 'lib', @@ -203,6 +267,30 @@ describe('lib', () => { expect( tree.exists('libs/my-dir/my-lib/src/lib/my-lib.service.spec.ts') ).toBeFalsy(); + + const tree2 = schematicRunner.runSchematic( + 'lib', + { name: 'myLib2', directory: 'myDir', skipModulePrefix: true }, + tree + ); + expect(tree2.exists(`libs/my-dir/my-lib2/karma.conf.js`)).toBeTruthy(); + expect(tree2.exists('libs/my-dir/my-lib2/src/index.ts')).toBeTruthy(); + expect( + tree2.exists('libs/my-dir/my-lib2/src/lib/my-lib2.module.ts') + ).toBeTruthy(); + + expect( + tree2.exists('libs/my-dir/my-lib2/src/lib/my-lib2.component.ts') + ).toBeFalsy(); + expect( + tree2.exists('libs/my-dir/my-lib2/src/lib/my-lib2.component.spec.ts') + ).toBeFalsy(); + expect( + tree2.exists('libs/my-dir/my-lib2/src/lib/my-lib2.service.ts') + ).toBeFalsy(); + expect( + tree2.exists('libs/my-dir/my-lib2/src/lib/my-lib2.service.spec.ts') + ).toBeFalsy(); }); it('should update ng-package.json', () => { @@ -291,6 +379,25 @@ describe('lib', () => { 'libs/my-dir/my-lib/src/lib/my-dir-my-lib.module.ts' ) ).toContain('RouterModule.forChild'); + + const tree2 = schematicRunner.runSchematic( + 'lib', + { + name: 'myLib2', + directory: 'myDir', + routing: true, + lazy: true, + skipModulePrefix: true + }, + tree + ); + + expect( + tree2.exists('libs/my-dir/my-lib2/src/lib/my-lib2.module.ts') + ).toBeTruthy(); + expect( + getFileContent(tree2, 'libs/my-dir/my-lib2/src/lib/my-lib2.module.ts') + ).toContain('RouterModule.forChild'); }); it('should update the parent module', () => { @@ -349,6 +456,36 @@ describe('lib', () => { '../../libs/my-dir/my-lib/src/index.ts', '../../libs/my-dir/my-lib2/src/index.ts' ]); + + const tree3 = schematicRunner.runSchematic( + 'lib', + { + name: 'myLib3', + directory: 'myDir', + routing: true, + lazy: true, + parentModule: 'apps/myapp/src/app/app.module.ts', + skipModulePrefix: true + }, + tree2 + ); + expect( + getFileContent(tree3, 'apps/myapp/src/app/app.module.ts') + ).toContain( + `RouterModule.forRoot([{path: 'my-dir-my-lib', loadChildren: '@proj/my-dir/my-lib#MyDirMyLibModule'}, {path: 'my-dir-my-lib2', loadChildren: '@proj/my-dir/my-lib2#MyDirMyLib2Module'}, {path: 'my-lib3', loadChildren: '@proj/my-dir/my-lib3#MyLib3Module'}])` + ); + + const tsConfigAppJson3 = JSON.parse( + stripJsonComments( + getFileContent(tree3, 'apps/myapp/tsconfig.app.json') + ) + ); + expect(tsConfigAppJson3.include).toEqual([ + '**/*.ts', + '../../libs/my-dir/my-lib/src/index.ts', + '../../libs/my-dir/my-lib2/src/index.ts', + '../../libs/my-dir/my-lib3/src/index.ts' + ]); }); }); @@ -374,6 +511,26 @@ describe('lib', () => { 'libs/my-dir/my-lib/src/lib/my-dir-my-lib.module.ts' ) ).toContain('const myDirMyLibRoutes: Route[] = '); + + const tree2 = schematicRunner.runSchematic( + 'lib', + { + name: 'myLib2', + directory: 'myDir', + routing: true, + skipModulePrefix: true + }, + tree + ); + expect( + tree2.exists('libs/my-dir/my-lib2/src/lib/my-lib2.module.ts') + ).toBeTruthy(); + expect( + getFileContent(tree2, 'libs/my-dir/my-lib2/src/lib/my-lib2.module.ts') + ).toContain('RouterModule'); + expect( + getFileContent(tree2, 'libs/my-dir/my-lib2/src/lib/my-lib2.module.ts') + ).toContain('const myLib2Routes: Route[] = '); }); it('should update the parent module', () => { @@ -409,6 +566,23 @@ describe('lib', () => { ).toContain( `RouterModule.forRoot([{path: 'my-dir-my-lib', children: myDirMyLibRoutes}, {path: 'my-dir-my-lib2', children: myDirMyLib2Routes}])` ); + + const tree3 = schematicRunner.runSchematic( + 'lib', + { + name: 'myLib3', + directory: 'myDir', + routing: true, + parentModule: 'apps/myapp/src/app/app.module.ts', + skipModulePrefix: true + }, + tree2 + ); + expect( + getFileContent(tree3, 'apps/myapp/src/app/app.module.ts') + ).toContain( + `RouterModule.forRoot([{path: 'my-dir-my-lib', children: myDirMyLibRoutes}, {path: 'my-dir-my-lib2', children: myDirMyLib2Routes}, {path: 'my-lib3', children: myLib3Routes}])` + ); }); }); }); diff --git a/packages/schematics/src/collection/library/schema.d.ts b/packages/schematics/src/collection/library/schema.d.ts index 1da702e0a3cbe..cc2d54d5cc426 100644 --- a/packages/schematics/src/collection/library/schema.d.ts +++ b/packages/schematics/src/collection/library/schema.d.ts @@ -3,6 +3,7 @@ import { UnitTestRunner } from '../../utils/test-runners'; export interface Schema { name: string; skipFormat: boolean; + skipModulePrefix: boolean; directory?: string; sourceDir?: string; publishable: boolean; diff --git a/packages/schematics/src/collection/library/schema.json b/packages/schematics/src/collection/library/schema.json index b947daaaf97c4..ec1d40b1037c8 100644 --- a/packages/schematics/src/collection/library/schema.json +++ b/packages/schematics/src/collection/library/schema.json @@ -34,6 +34,11 @@ "type": "boolean", "default": false }, + "skipModulePrefix": { + "description": "Skip module prefix when using --directory", + "type": "boolean", + "default": false + }, "skipPackageJson": { "type": "boolean", "default": false,