feat(schematics): introduce framework option for lib schematic

This commit is contained in:
Jason Jean 2019-02-01 13:45:20 -05:00 committed by Victor Savkin
parent fed4793911
commit db0fa65b32
5 changed files with 74 additions and 10 deletions

View File

@ -41,6 +41,7 @@ import {
} from '../../utils/cli-config-utils';
import { formatFiles } from '../../utils/rules/format-files';
import { updateKarmaConf } from '../../utils/rules/update-karma-conf';
import { Framework } from '../../utils/frameworks';
interface NormalizedSchema extends Schema {
name: string;
@ -260,7 +261,7 @@ function updateProject(options: NormalizedSchema): Rule {
host.delete(path.join(options.projectRoot, 'tsconfig.spec.json'));
}
if (options.module) {
if (options.framework === Framework.Angular) {
host.delete(path.join(libRoot, `${options.name}.module.ts`));
host.create(
path.join(libRoot, `${options.fileName}.module.ts`),
@ -472,7 +473,7 @@ function addModule(options: NormalizedSchema): Rule {
export default function(schema: Schema): Rule {
return (host: Tree, context: SchematicContext) => {
const options = normalizeOptions(host, schema);
const options = normalizeOptions(host, context, schema);
if (!options.routing && options.lazy) {
throw new Error(`routing must be set`);
}
@ -493,19 +494,23 @@ export default function(schema: Schema): Rule {
options.unitTestRunner === 'jest'
? schematic('jest-project', {
project: options.name,
skipSetupFile: !options.module,
skipSerializers: !options.module
skipSetupFile: options.framework !== Framework.Angular,
skipSerializers: options.framework !== Framework.Angular
})
: noop(),
options.publishable ? updateLibPackageNpmScope(options) : noop(),
options.module ? addModule(options) : noop(),
options.framework === Framework.Angular ? addModule(options) : noop(),
formatFiles(options)
])(host, context);
};
}
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
function normalizeOptions(
host: Tree,
context: SchematicContext,
options: Schema
): NormalizedSchema {
const name = toFileName(options.name);
const projectDirectory = options.directory
? `${toFileName(options.directory)}/${name}`
@ -522,6 +527,13 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const modulePath = `${projectRoot}/src/lib/${fileName}.module.ts`;
const defaultPrefix = getNpmScope(host);
if (!options.module) {
context.logger.warn(
'Deprecated: --module is deprecated in favor of --framework'
);
options.framework = Framework.None;
}
return {
...options,
prefix: options.prefix ? options.prefix : defaultPrefix,

View File

@ -1,5 +1,3 @@
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Tree, VirtualTree } from '@angular-devkit/schematics';
import {
createApp,
@ -10,6 +8,7 @@ import { getFileContent } from '@schematics/angular/utility/test';
import * as stripJsonComments from 'strip-json-comments';
import { readJsonInTree } from '../../utils/ast-utils';
import { NxJson } from '../../command-line/shared';
import { UnitTestTree } from '@angular-devkit/schematics/testing';
describe('lib', () => {
let appTree: Tree;
@ -224,6 +223,33 @@ describe('lib', () => {
].prefix
).toEqual('custom');
});
describe('--framework', () => {
describe('none', () => {
let tree: UnitTestTree;
beforeEach(async () => {
tree = await runSchematic(
'lib',
{
name: 'myLib',
framework: 'none'
},
appTree
);
});
it('should generate a basic typescript lib', () => {
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);
});
});
});
});
describe('nested', () => {

View File

@ -1,4 +1,5 @@
import { UnitTestRunner } from '../../utils/test-runners';
import { Framework } from '../../utils/framework';
export interface Schema {
name: string;
@ -20,5 +21,7 @@ export interface Schema {
parentModule?: string;
tags?: string;
framework: Framework;
unitTestRunner: UnitTestRunner;
}

View File

@ -18,6 +18,26 @@
"description": "A directory where the app is placed",
"x-prompt": "In which directory should the library be generated?"
},
"framework": {
"type": "string",
"enum": ["angular", "none"],
"description": "The framework this library uses",
"default": "angular",
"x-prompt": {
"message": "What framework should this library use?",
"type": "list",
"items": [
{
"value": "angular",
"label": "Angular [ https://angular.io/ ]"
},
{
"value": "none",
"label": "Typescript [ https://www.typescriptlang.org/ ]"
}
]
}
},
"publishable": {
"type": "boolean",
"default": false,
@ -85,8 +105,7 @@
"module": {
"type": "boolean",
"default": true,
"description": "Include an NgModule in the library.",
"x-prompt": "Would you like to generate an NgModule within the library?"
"description": "[Deprecated]: Include an NgModule in the library."
},
"tags": {
"type": "string",

View File

@ -0,0 +1,4 @@
export const enum Framework {
Angular = 'angular',
None = 'none'
}