From db0fa65b329ed6d41734dc4e070d311dd460916b Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Fri, 1 Feb 2019 13:45:20 -0500 Subject: [PATCH] feat(schematics): introduce framework option for lib schematic --- .../src/collection/library/index.ts | 24 +++++++++++---- .../src/collection/library/library.spec.ts | 30 +++++++++++++++++-- .../src/collection/library/schema.d.ts | 3 ++ .../src/collection/library/schema.json | 23 ++++++++++++-- packages/schematics/src/utils/frameworks.ts | 4 +++ 5 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 packages/schematics/src/utils/frameworks.ts diff --git a/packages/schematics/src/collection/library/index.ts b/packages/schematics/src/collection/library/index.ts index bce0c06b69..32e57e215e 100644 --- a/packages/schematics/src/collection/library/index.ts +++ b/packages/schematics/src/collection/library/index.ts @@ -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, diff --git a/packages/schematics/src/collection/library/library.spec.ts b/packages/schematics/src/collection/library/library.spec.ts index 783bc36ffa..c1d27b7631 100644 --- a/packages/schematics/src/collection/library/library.spec.ts +++ b/packages/schematics/src/collection/library/library.spec.ts @@ -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', () => { diff --git a/packages/schematics/src/collection/library/schema.d.ts b/packages/schematics/src/collection/library/schema.d.ts index 00d3eccda9..2163442863 100644 --- a/packages/schematics/src/collection/library/schema.d.ts +++ b/packages/schematics/src/collection/library/schema.d.ts @@ -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; } diff --git a/packages/schematics/src/collection/library/schema.json b/packages/schematics/src/collection/library/schema.json index 3551e7d69a..28298bc198 100644 --- a/packages/schematics/src/collection/library/schema.json +++ b/packages/schematics/src/collection/library/schema.json @@ -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", diff --git a/packages/schematics/src/utils/frameworks.ts b/packages/schematics/src/utils/frameworks.ts new file mode 100644 index 0000000000..2622281416 --- /dev/null +++ b/packages/schematics/src/utils/frameworks.ts @@ -0,0 +1,4 @@ +export const enum Framework { + Angular = 'angular', + None = 'none' +}