feat(schematics): add option for no module in lib

This commit is contained in:
--get 2018-10-04 14:39:37 -04:00 committed by Victor Savkin
parent f4c106a644
commit f1d4dd9f46
4 changed files with 97 additions and 51 deletions

View File

@ -241,49 +241,55 @@ function updateProject(options: NormalizedSchema): Rule {
host.delete(path.join(options.projectRoot, 'tsconfig.spec.json'));
}
host.overwrite(
path.join(libRoot, `${options.name}.module.ts`),
`
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
]
})
export class ${options.moduleName} { }
`
);
if (options.unitTestRunner !== 'none') {
host.create(
path.join(libRoot, `${options.name}.module.spec.ts`),
if (options.module) {
host.overwrite(
path.join(libRoot, `${options.name}.module.ts`),
`
import { async, TestBed } from '@angular/core/testing';
import { ${options.moduleName} } from './${options.name}.module';
describe('${options.moduleName}', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ ${options.moduleName} ]
})
.compileComponents();
}));
it('should create', () => {
expect(${options.moduleName}).toBeDefined();
});
});
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
]
})
export class ${options.moduleName} { }
`
);
if (options.unitTestRunner !== 'none') {
host.create(
path.join(libRoot, `${options.name}.module.spec.ts`),
`
import { async, TestBed } from '@angular/core/testing';
import { ${options.moduleName} } from './${options.name}.module';
describe('${options.moduleName}', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ ${options.moduleName} ]
})
.compileComponents();
}));
it('should create', () => {
expect(${options.moduleName}).toBeDefined();
});
});
`
);
}
host.overwrite(
`${options.projectRoot}/src/index.ts`,
`
export * from './lib/${options.name}.module';
`
);
} else {
host.delete(path.join(libRoot, `${options.name}.module.ts`));
host.create(path.join(libRoot, `.gitkeep`), '');
host.overwrite(`${options.projectRoot}/src/index.ts`, '');
}
host.overwrite(
`${options.projectRoot}/src/index.ts`,
`
export * from './lib/${options.name}.module';
`
);
return chain([
updateJsonInTree(getWorkspacePath(host), json => {
@ -400,6 +406,21 @@ function updateLibPackageNpmScope(options: NormalizedSchema): Rule {
});
}
function addModule(options: NormalizedSchema): Rule {
return chain([
options.routing && options.lazy
? addLazyLoadedRouterConfiguration(options)
: noop(),
options.routing && options.lazy && options.parentModule
? addLoadChildren(options)
: noop(),
options.routing && !options.lazy ? addRouterConfiguration(options) : noop(),
options.routing && !options.lazy && options.parentModule
? addChildren(options)
: noop()
]);
}
export default function(schema: Schema): Rule {
return (host: Tree, context: SchematicContext) => {
const options = normalizeOptions(host, schema);
@ -428,18 +449,7 @@ export default function(schema: Schema): Rule {
: noop(),
options.publishable ? updateLibPackageNpmScope(options) : noop(),
options.routing && options.lazy
? addLazyLoadedRouterConfiguration(options)
: noop(),
options.routing && options.lazy && options.parentModule
? addLoadChildren(options)
: noop(),
options.routing && !options.lazy
? addRouterConfiguration(options)
: noop(),
options.routing && !options.lazy && options.parentModule
? addChildren(options)
: noop(),
options.module ? addModule(options) : noop(),
formatFiles(options)
])(host, context);
};

View File

@ -139,6 +139,21 @@ describe('lib', () => {
).toBeFalsy();
});
it('should not generate a module for --module false', () => {
const tree = schematicRunner.runSchematic(
'lib',
{ name: 'myLib', module: false },
appTree
);
expect(tree.exists('libs/my-lib/src/lib/my-lib.module.ts')).toEqual(
false
);
expect(tree.exists('libs/my-lib/src/lib/my-lib.module.spec.ts')).toEqual(
false
);
expect(tree.exists('libs/my-lib/src/lib/.gitkeep')).toEqual(true);
});
it('should default the prefix to npmScope', () => {
const noPrefix = schematicRunner.runSchematic(
'lib',
@ -231,6 +246,21 @@ describe('lib', () => {
tsconfigJson.compilerOptions.paths['my-dir-my-lib/*']
).toBeUndefined();
});
it('should not generate a module for --module false', () => {
const tree = schematicRunner.runSchematic(
'lib',
{ name: 'myLib', directory: 'myDir', module: false },
appTree
);
expect(
tree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.module.ts')
).toEqual(false);
expect(
tree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.module.spec.ts')
).toEqual(false);
expect(tree.exists('libs/my-dir/my-lib/src/lib/.gitkeep')).toEqual(true);
});
});
describe('router', () => {

View File

@ -6,6 +6,7 @@ export interface Schema {
directory?: string;
sourceDir?: string;
publishable: boolean;
module: boolean;
spec?: boolean;
flat?: boolean;

View File

@ -53,6 +53,11 @@
"description":
"Add RouterModule.forChild when set to true, and a simple array of routes when set to false."
},
"module": {
"type": "boolean",
"default": true,
"description": "Include an NgModule in the library."
},
"parentModule": {
"type": "string",
"description":