feat(schematics): add option for no module in lib
This commit is contained in:
parent
f4c106a644
commit
f1d4dd9f46
@ -241,49 +241,55 @@ function updateProject(options: NormalizedSchema): Rule {
|
|||||||
host.delete(path.join(options.projectRoot, 'tsconfig.spec.json'));
|
host.delete(path.join(options.projectRoot, 'tsconfig.spec.json'));
|
||||||
}
|
}
|
||||||
|
|
||||||
host.overwrite(
|
if (options.module) {
|
||||||
path.join(libRoot, `${options.name}.module.ts`),
|
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`),
|
|
||||||
`
|
`
|
||||||
import { async, TestBed } from '@angular/core/testing';
|
import { NgModule } from '@angular/core';
|
||||||
import { ${options.moduleName} } from './${options.name}.module';
|
import { CommonModule } from '@angular/common';
|
||||||
|
|
||||||
describe('${options.moduleName}', () => {
|
@NgModule({
|
||||||
beforeEach(async(() => {
|
imports: [
|
||||||
TestBed.configureTestingModule({
|
CommonModule
|
||||||
imports: [ ${options.moduleName} ]
|
]
|
||||||
})
|
})
|
||||||
.compileComponents();
|
export class ${options.moduleName} { }
|
||||||
}));
|
|
||||||
|
|
||||||
it('should create', () => {
|
|
||||||
expect(${options.moduleName}).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
`
|
`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
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([
|
return chain([
|
||||||
updateJsonInTree(getWorkspacePath(host), json => {
|
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 {
|
export default function(schema: Schema): Rule {
|
||||||
return (host: Tree, context: SchematicContext) => {
|
return (host: Tree, context: SchematicContext) => {
|
||||||
const options = normalizeOptions(host, schema);
|
const options = normalizeOptions(host, schema);
|
||||||
@ -428,18 +449,7 @@ export default function(schema: Schema): Rule {
|
|||||||
: noop(),
|
: noop(),
|
||||||
|
|
||||||
options.publishable ? updateLibPackageNpmScope(options) : noop(),
|
options.publishable ? updateLibPackageNpmScope(options) : noop(),
|
||||||
options.routing && options.lazy
|
options.module ? addModule(options) : noop(),
|
||||||
? 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(),
|
|
||||||
formatFiles(options)
|
formatFiles(options)
|
||||||
])(host, context);
|
])(host, context);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -139,6 +139,21 @@ describe('lib', () => {
|
|||||||
).toBeFalsy();
|
).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', () => {
|
it('should default the prefix to npmScope', () => {
|
||||||
const noPrefix = schematicRunner.runSchematic(
|
const noPrefix = schematicRunner.runSchematic(
|
||||||
'lib',
|
'lib',
|
||||||
@ -231,6 +246,21 @@ describe('lib', () => {
|
|||||||
tsconfigJson.compilerOptions.paths['my-dir-my-lib/*']
|
tsconfigJson.compilerOptions.paths['my-dir-my-lib/*']
|
||||||
).toBeUndefined();
|
).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', () => {
|
describe('router', () => {
|
||||||
|
|||||||
@ -6,6 +6,7 @@ export interface Schema {
|
|||||||
directory?: string;
|
directory?: string;
|
||||||
sourceDir?: string;
|
sourceDir?: string;
|
||||||
publishable: boolean;
|
publishable: boolean;
|
||||||
|
module: boolean;
|
||||||
|
|
||||||
spec?: boolean;
|
spec?: boolean;
|
||||||
flat?: boolean;
|
flat?: boolean;
|
||||||
|
|||||||
@ -53,6 +53,11 @@
|
|||||||
"description":
|
"description":
|
||||||
"Add RouterModule.forChild when set to true, and a simple array of routes when set to false."
|
"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": {
|
"parentModule": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description":
|
"description":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user