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'; } from '../../utils/cli-config-utils';
import { formatFiles } from '../../utils/rules/format-files'; import { formatFiles } from '../../utils/rules/format-files';
import { updateKarmaConf } from '../../utils/rules/update-karma-conf'; import { updateKarmaConf } from '../../utils/rules/update-karma-conf';
import { Framework } from '../../utils/frameworks';
interface NormalizedSchema extends Schema { interface NormalizedSchema extends Schema {
name: string; name: string;
@ -260,7 +261,7 @@ function updateProject(options: NormalizedSchema): Rule {
host.delete(path.join(options.projectRoot, 'tsconfig.spec.json')); 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.delete(path.join(libRoot, `${options.name}.module.ts`));
host.create( host.create(
path.join(libRoot, `${options.fileName}.module.ts`), path.join(libRoot, `${options.fileName}.module.ts`),
@ -472,7 +473,7 @@ function addModule(options: NormalizedSchema): Rule {
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, context, schema);
if (!options.routing && options.lazy) { if (!options.routing && options.lazy) {
throw new Error(`routing must be set`); throw new Error(`routing must be set`);
} }
@ -493,19 +494,23 @@ export default function(schema: Schema): Rule {
options.unitTestRunner === 'jest' options.unitTestRunner === 'jest'
? schematic('jest-project', { ? schematic('jest-project', {
project: options.name, project: options.name,
skipSetupFile: !options.module, skipSetupFile: options.framework !== Framework.Angular,
skipSerializers: !options.module skipSerializers: options.framework !== Framework.Angular
}) })
: noop(), : noop(),
options.publishable ? updateLibPackageNpmScope(options) : noop(), options.publishable ? updateLibPackageNpmScope(options) : noop(),
options.module ? addModule(options) : noop(), options.framework === Framework.Angular ? addModule(options) : noop(),
formatFiles(options) formatFiles(options)
])(host, context); ])(host, context);
}; };
} }
function normalizeOptions(host: Tree, options: Schema): NormalizedSchema { function normalizeOptions(
host: Tree,
context: SchematicContext,
options: Schema
): NormalizedSchema {
const name = toFileName(options.name); const name = toFileName(options.name);
const projectDirectory = options.directory const projectDirectory = options.directory
? `${toFileName(options.directory)}/${name}` ? `${toFileName(options.directory)}/${name}`
@ -522,6 +527,13 @@ function normalizeOptions(host: Tree, options: Schema): NormalizedSchema {
const modulePath = `${projectRoot}/src/lib/${fileName}.module.ts`; const modulePath = `${projectRoot}/src/lib/${fileName}.module.ts`;
const defaultPrefix = getNpmScope(host); const defaultPrefix = getNpmScope(host);
if (!options.module) {
context.logger.warn(
'Deprecated: --module is deprecated in favor of --framework'
);
options.framework = Framework.None;
}
return { return {
...options, ...options,
prefix: options.prefix ? options.prefix : defaultPrefix, 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 { Tree, VirtualTree } from '@angular-devkit/schematics';
import { import {
createApp, createApp,
@ -10,6 +8,7 @@ import { getFileContent } from '@schematics/angular/utility/test';
import * as stripJsonComments from 'strip-json-comments'; import * as stripJsonComments from 'strip-json-comments';
import { readJsonInTree } from '../../utils/ast-utils'; import { readJsonInTree } from '../../utils/ast-utils';
import { NxJson } from '../../command-line/shared'; import { NxJson } from '../../command-line/shared';
import { UnitTestTree } from '@angular-devkit/schematics/testing';
describe('lib', () => { describe('lib', () => {
let appTree: Tree; let appTree: Tree;
@ -224,6 +223,33 @@ describe('lib', () => {
].prefix ].prefix
).toEqual('custom'); ).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', () => { describe('nested', () => {

View File

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

View File

@ -18,6 +18,26 @@
"description": "A directory where the app is placed", "description": "A directory where the app is placed",
"x-prompt": "In which directory should the library be generated?" "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": { "publishable": {
"type": "boolean", "type": "boolean",
"default": false, "default": false,
@ -85,8 +105,7 @@
"module": { "module": {
"type": "boolean", "type": "boolean",
"default": true, "default": true,
"description": "Include an NgModule in the library.", "description": "[Deprecated]: Include an NgModule in the library."
"x-prompt": "Would you like to generate an NgModule within the library?"
}, },
"tags": { "tags": {
"type": "string", "type": "string",

View File

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