Skip to content

Commit

Permalink
feat(schematics): add an option to pass a custom module name when cre…
Browse files Browse the repository at this point in the history
…ating libs
  • Loading branch information
vsavkin committed Mar 26, 2018
1 parent 2746f03 commit c1ca7df
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/schematics/src/collection/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function addChildren(schema: NormalizedSchema): Rule {
export default function(schema: Schema): Rule {
return wrapIntoFormat(() => {
const options = normalizeOptions(schema);
const moduleFileName = `${toFileName(options.name)}.module`;
const moduleFileName = `${options.moduleName}.module`;
const modulePath = `${options.fullPath}/${moduleFileName}.ts`;
const indexFile = `libs/${toFileName(options.fullName)}/index.ts`;

Expand All @@ -251,7 +251,7 @@ export default function(schema: Schema): Rule {
url(options.nomodule ? './files' : './ngfiles'),
[
template({
...names(options.name),
...names(options.moduleName),
dot: '.',
tmpl: '',
...(options as object)
Expand Down Expand Up @@ -284,6 +284,9 @@ function normalizeOptions(options: Schema): NormalizedSchema {
const fullName = options.directory
? `${toFileName(options.directory)}/${name}`
: name;
const moduleName = options.moduleName
? toFileName(options.moduleName)
: toFileName(options.name);
const fullPath = `libs/${fullName}/src`;
return { ...options, sourceDir: 'src', name, fullName, fullPath };
return { ...options, sourceDir: 'src', name, fullName, fullPath, moduleName };
}
16 changes: 15 additions & 1 deletion packages/schematics/src/collection/lib/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('lib', () => {
]);
});

it('should generate files', () => {
it('should generate files when nomodule = true', () => {
const tree = schematicRunner.runSchematic(
'lib',
{ name: 'myLib', nomodule: true },
Expand All @@ -68,6 +68,20 @@ describe('lib', () => {
getFileContent(tree, 'libs/my-lib/src/my-lib.module.ts')
).toContain('class MyLibModule');
});

it('should generate files when moduleName is set', () => {
const tree = schematicRunner.runSchematic(
'lib',
{ name: 'myLib', moduleName: 'other' },
appTree
);
expect(tree.exists('libs/my-lib/src/other.module.ts')).toBeTruthy();
expect(tree.exists('libs/my-lib/src/other.module.spec.ts')).toBeTruthy();
expect(tree.exists('libs/my-lib/index.ts')).toBeTruthy();
expect(getFileContent(tree, 'libs/my-lib/src/other.module.ts')).toContain(
'class OtherModule'
);
});
});

describe('nested', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/schematics/src/collection/lib/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Schema {
name: string;
moduleName: string;
directory?: string;
sourceDir?: string;
nomodule: boolean;
Expand Down
4 changes: 4 additions & 0 deletions packages/schematics/src/collection/lib/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"type": "string",
"description": "A directory where the app is placed"
},
"moduleName": {
"type": "string",
"description": "A module name"
},
"nomodule": {
"type": "boolean",
"default": false,
Expand Down

0 comments on commit c1ca7df

Please sign in to comment.