Skip to content

Commit

Permalink
fix(schematics): added skipModulePrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
listepo-alterpost authored and vsavkin committed Nov 18, 2018
1 parent ace390e commit c0ec046
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 15 deletions.
38 changes: 23 additions & 15 deletions packages/schematics/src/collection/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { move } from '../../utils/rules/move';

interface NormalizedSchema extends Schema {
name: string;
fileName: string;
projectRoot: string;
entryFile: string;
modulePath: string;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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}'}`
)
]);

Expand Down Expand Up @@ -188,15 +191,15 @@ 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, [
insertImport(sourceFile, options.parentModule, constName, importPath),
...addRoute(
options.parentModule,
sourceFile,
`{path: '${toFileName(options.name)}', children: ${constName}}`
`{path: '${toFileName(options.fileName)}', children: ${constName}}`
)
]);
return host;
Expand Down Expand Up @@ -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';
Expand All @@ -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(() => {
Expand All @@ -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`, '');
}
Expand Down Expand Up @@ -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 {
Expand All @@ -476,6 +483,7 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
moduleName,
projectDirectory,
modulePath,
parsedTags
parsedTags,
fileName
};
}
174 changes: 174 additions & 0 deletions packages/schematics/src/collection/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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',
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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'
]);
});
});

Expand All @@ -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', () => {
Expand Down Expand Up @@ -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}])`
);
});
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/src/collection/library/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions packages/schematics/src/collection/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c0ec046

Please sign in to comment.