From e360cdf58a5631a348ad94ec49d83b5358f0be44 Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Mon, 9 Oct 2017 10:28:58 -0700 Subject: [PATCH] refactor: move all CLI-specific exceptions to different options This is supposed to be released at the same time as https://github.com/angular/angular-cli/pull/7981 --- .../schematics/angular/application/index.ts | 4 ++ .../angular/application/schema.d.ts | 9 +++ .../angular/application/schema.json | 5 ++ packages/schematics/angular/class/index.ts | 3 + packages/schematics/angular/class/schema.d.ts | 9 +++ packages/schematics/angular/class/schema.json | 5 ++ .../schematics/angular/component/index.ts | 3 + .../schematics/angular/component/schema.d.ts | 9 +++ .../schematics/angular/component/schema.json | 5 ++ .../schematics/angular/directive/index.ts | 3 + .../schematics/angular/directive/schema.d.ts | 9 +++ .../schematics/angular/directive/schema.json | 5 ++ packages/schematics/angular/enum/index.ts | 3 + packages/schematics/angular/enum/schema.d.ts | 9 +++ packages/schematics/angular/enum/schema.json | 5 ++ packages/schematics/angular/guard/index.ts | 3 + packages/schematics/angular/guard/schema.d.ts | 9 +++ packages/schematics/angular/guard/schema.json | 5 ++ .../schematics/angular/interface/index.ts | 3 + .../schematics/angular/interface/schema.d.ts | 9 +++ .../schematics/angular/interface/schema.json | 5 ++ packages/schematics/angular/module/index.ts | 3 + .../schematics/angular/module/schema.d.ts | 9 +++ .../schematics/angular/module/schema.json | 5 ++ packages/schematics/angular/pipe/index.ts | 3 + packages/schematics/angular/pipe/schema.d.ts | 9 +++ packages/schematics/angular/pipe/schema.json | 5 ++ packages/schematics/angular/service/index.ts | 3 + .../schematics/angular/service/schema.d.ts | 9 +++ .../schematics/angular/service/schema.json | 5 ++ packages/schematics/angular/utility/args.ts | 56 +++++++++++++++++++ 31 files changed, 227 insertions(+) create mode 100644 packages/schematics/angular/utility/args.ts diff --git a/packages/schematics/angular/application/index.ts b/packages/schematics/angular/application/index.ts index ca68477b3c..5eb293bbde 100644 --- a/packages/schematics/angular/application/index.ts +++ b/packages/schematics/angular/application/index.ts @@ -73,6 +73,10 @@ function minimalPathFilter(path: string): boolean { return !toRemoveList.some(re => re.test(path)); } export default function (options: ApplicationOptions): Rule { + if (!options.name) { + options.name = options._[0]; + } + return (host: Tree, context: SchematicContext) => { const appRootSelector = `${options.prefix}-root`; const componentOptions = !options.minimal ? diff --git a/packages/schematics/angular/application/schema.d.ts b/packages/schematics/angular/application/schema.d.ts index 1492b62157..1bd6233941 100644 --- a/packages/schematics/angular/application/schema.d.ts +++ b/packages/schematics/angular/application/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + /** * The directory name to create the app in. */ diff --git a/packages/schematics/angular/application/schema.json b/packages/schematics/angular/application/schema.json index 4218bc2099..f200bf0052 100644 --- a/packages/schematics/angular/application/schema.json +++ b/packages/schematics/angular/application/schema.json @@ -4,6 +4,11 @@ "title": "Angular Application Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "directory": { "type": "string", "description": "The directory name to create the app in.", diff --git a/packages/schematics/angular/class/index.ts b/packages/schematics/angular/class/index.ts index f07335365c..a30ab266b7 100644 --- a/packages/schematics/angular/class/index.ts +++ b/packages/schematics/angular/class/index.ts @@ -20,10 +20,13 @@ import { url, } from '@angular-devkit/schematics'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { Schema as ClassOptions } from './schema'; export default function (options: ClassOptions): Rule { + parseOptions('class', options); + options.type = !!options.type ? `.${options.type}` : ''; options.path = options.path ? normalize(options.path) : options.path; const sourceDir = options.sourceDir; diff --git a/packages/schematics/angular/class/schema.d.ts b/packages/schematics/angular/class/schema.d.ts index 6668692550..900499e0fb 100644 --- a/packages/schematics/angular/class/schema.d.ts +++ b/packages/schematics/angular/class/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + name: string; appRoot?: string; path?: string; diff --git a/packages/schematics/angular/class/schema.json b/packages/schematics/angular/class/schema.json index c1ba53d608..a1d828e6da 100644 --- a/packages/schematics/angular/class/schema.json +++ b/packages/schematics/angular/class/schema.json @@ -4,6 +4,11 @@ "title": "Angular Class Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "name": { "type": "string" }, diff --git a/packages/schematics/angular/component/index.ts b/packages/schematics/angular/component/index.ts index a67bf613cc..0aaf736657 100644 --- a/packages/schematics/angular/component/index.ts +++ b/packages/schematics/angular/component/index.ts @@ -24,6 +24,7 @@ import { import 'rxjs/add/operator/merge'; import * as ts from 'typescript'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { addDeclarationToModule, addExportToModule } from '../utility/ast-utils'; import { InsertChange } from '../utility/change'; import { buildRelativePath, findModuleFromOptions } from '../utility/find-module'; @@ -102,6 +103,8 @@ function buildSelector(options: ComponentOptions) { export default function(options: ComponentOptions): Rule { + parseOptions('component', options); + const sourceDir = options.sourceDir; if (!sourceDir) { throw new SchematicsException(`sourceDir option is required.`); diff --git a/packages/schematics/angular/component/schema.d.ts b/packages/schematics/angular/component/schema.d.ts index 864d6882b5..8c863b9af8 100644 --- a/packages/schematics/angular/component/schema.d.ts +++ b/packages/schematics/angular/component/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + path?: string; appRoot?: string; sourceDir?: string; diff --git a/packages/schematics/angular/component/schema.json b/packages/schematics/angular/component/schema.json index 6ecaa37e15..2ed5441982 100644 --- a/packages/schematics/angular/component/schema.json +++ b/packages/schematics/angular/component/schema.json @@ -4,6 +4,11 @@ "title": "Angular Application Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "path": { "type": "string", "default": "app" diff --git a/packages/schematics/angular/directive/index.ts b/packages/schematics/angular/directive/index.ts index a043661c29..50225c8fb5 100644 --- a/packages/schematics/angular/directive/index.ts +++ b/packages/schematics/angular/directive/index.ts @@ -24,6 +24,7 @@ import { import 'rxjs/add/operator/merge'; import * as ts from 'typescript'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { addDeclarationToModule, addExportToModule } from '../utility/ast-utils'; import { InsertChange } from '../utility/change'; import { buildRelativePath, findModuleFromOptions } from '../utility/find-module'; @@ -99,6 +100,8 @@ function buildSelector(options: DirectiveOptions) { } export default function (options: DirectiveOptions): Rule { + parseOptions('directive', options); + options.selector = options.selector || buildSelector(options); options.path = options.path ? normalize(options.path) : options.path; const sourceDir = options.sourceDir; diff --git a/packages/schematics/angular/directive/schema.d.ts b/packages/schematics/angular/directive/schema.d.ts index eb5436aade..cfc320fb47 100644 --- a/packages/schematics/angular/directive/schema.d.ts +++ b/packages/schematics/angular/directive/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + name: string; path?: string; appRoot?: string; diff --git a/packages/schematics/angular/directive/schema.json b/packages/schematics/angular/directive/schema.json index ccf145b978..831d099015 100644 --- a/packages/schematics/angular/directive/schema.json +++ b/packages/schematics/angular/directive/schema.json @@ -4,6 +4,11 @@ "title": "Angular Class Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "name": { "type": "string" }, diff --git a/packages/schematics/angular/enum/index.ts b/packages/schematics/angular/enum/index.ts index 5f46c1dd41..3167bfb63b 100644 --- a/packages/schematics/angular/enum/index.ts +++ b/packages/schematics/angular/enum/index.ts @@ -18,10 +18,13 @@ import { url, } from '@angular-devkit/schematics'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { Schema as EnumOptions } from './schema'; export default function (options: EnumOptions): Rule { + parseOptions('enum', options); + options.path = options.path ? normalize(options.path) : options.path; const sourceDir = options.sourceDir; if (!sourceDir) { diff --git a/packages/schematics/angular/enum/schema.d.ts b/packages/schematics/angular/enum/schema.d.ts index 9d7ef0fc2c..ccf7375ec0 100644 --- a/packages/schematics/angular/enum/schema.d.ts +++ b/packages/schematics/angular/enum/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + name: string; path?: string; appRoot?: string; diff --git a/packages/schematics/angular/enum/schema.json b/packages/schematics/angular/enum/schema.json index a4d4a9a2e3..43605631a3 100644 --- a/packages/schematics/angular/enum/schema.json +++ b/packages/schematics/angular/enum/schema.json @@ -4,6 +4,11 @@ "title": "Angular Enum Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "name": { "type": "string" }, diff --git a/packages/schematics/angular/guard/index.ts b/packages/schematics/angular/guard/index.ts index cd1c3634ae..f70371b08e 100644 --- a/packages/schematics/angular/guard/index.ts +++ b/packages/schematics/angular/guard/index.ts @@ -24,6 +24,7 @@ import { import 'rxjs/add/operator/merge'; import * as ts from 'typescript'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { addProviderToModule } from '../utility/ast-utils'; import { InsertChange } from '../utility/change'; import { buildRelativePath, findModuleFromOptions } from '../utility/find-module'; @@ -66,6 +67,8 @@ function addDeclarationToNgModule(options: GuardOptions): Rule { } export default function (options: GuardOptions): Rule { + parseOptions('guard', options); + options.path = options.path ? normalize(options.path) : options.path; const sourceDir = options.sourceDir; if (!sourceDir) { diff --git a/packages/schematics/angular/guard/schema.d.ts b/packages/schematics/angular/guard/schema.d.ts index e1730bb937..df1a4eafd0 100644 --- a/packages/schematics/angular/guard/schema.d.ts +++ b/packages/schematics/angular/guard/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + name: string; spec?: boolean; flat?: boolean; diff --git a/packages/schematics/angular/guard/schema.json b/packages/schematics/angular/guard/schema.json index 18c65012f8..b0ebaf9305 100644 --- a/packages/schematics/angular/guard/schema.json +++ b/packages/schematics/angular/guard/schema.json @@ -4,6 +4,11 @@ "title": "Angular Guard Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "name": { "type": "string" }, diff --git a/packages/schematics/angular/interface/index.ts b/packages/schematics/angular/interface/index.ts index c1e466d205..aa449df681 100644 --- a/packages/schematics/angular/interface/index.ts +++ b/packages/schematics/angular/interface/index.ts @@ -18,10 +18,13 @@ import { url, } from '@angular-devkit/schematics'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { Schema as InterfaceOptions } from './schema'; export default function (options: InterfaceOptions): Rule { + parseOptions('interface', options); + options.prefix = options.prefix ? options.prefix : ''; options.type = !!options.type ? `.${options.type}` : ''; options.path = options.path ? normalize(options.path) : options.path; diff --git a/packages/schematics/angular/interface/schema.d.ts b/packages/schematics/angular/interface/schema.d.ts index 1091c0d3c5..b350b44130 100644 --- a/packages/schematics/angular/interface/schema.d.ts +++ b/packages/schematics/angular/interface/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + name: string; path?: string; appRoot?: string; diff --git a/packages/schematics/angular/interface/schema.json b/packages/schematics/angular/interface/schema.json index 6d82532e23..d1de111a42 100644 --- a/packages/schematics/angular/interface/schema.json +++ b/packages/schematics/angular/interface/schema.json @@ -4,6 +4,11 @@ "title": "Angular Interface Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "name": { "type": "string" }, diff --git a/packages/schematics/angular/module/index.ts b/packages/schematics/angular/module/index.ts index 3dcc550df8..1202924a0c 100644 --- a/packages/schematics/angular/module/index.ts +++ b/packages/schematics/angular/module/index.ts @@ -23,6 +23,7 @@ import { } from '@angular-devkit/schematics'; import * as ts from 'typescript'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { addImportToModule } from '../utility/ast-utils'; import { InsertChange } from '../utility/change'; import { findModuleFromOptions } from '../utility/find-module'; @@ -70,6 +71,8 @@ function addDeclarationToNgModule(options: ModuleOptions): Rule { } export default function (options: ModuleOptions): Rule { + parseOptions('module', options); + options.path = options.path ? normalize(options.path) : options.path; const sourceDir = options.sourceDir; if (!sourceDir) { diff --git a/packages/schematics/angular/module/schema.d.ts b/packages/schematics/angular/module/schema.d.ts index d5e6d89ba5..19b86eeafb 100644 --- a/packages/schematics/angular/module/schema.d.ts +++ b/packages/schematics/angular/module/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + name: string; path?: string; appRoot?: string; diff --git a/packages/schematics/angular/module/schema.json b/packages/schematics/angular/module/schema.json index 303c9088c6..c73faf6bfe 100644 --- a/packages/schematics/angular/module/schema.json +++ b/packages/schematics/angular/module/schema.json @@ -4,6 +4,11 @@ "title": "Angular Module Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "name": { "type": "string" }, diff --git a/packages/schematics/angular/pipe/index.ts b/packages/schematics/angular/pipe/index.ts index 37af3b5b28..99d7720170 100644 --- a/packages/schematics/angular/pipe/index.ts +++ b/packages/schematics/angular/pipe/index.ts @@ -24,6 +24,7 @@ import { import 'rxjs/add/operator/merge'; import * as ts from 'typescript'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { addDeclarationToModule, addExportToModule } from '../utility/ast-utils'; import { InsertChange } from '../utility/change'; import { buildRelativePath, findModuleFromOptions } from '../utility/find-module'; @@ -86,6 +87,8 @@ function addDeclarationToNgModule(options: PipeOptions): Rule { } export default function (options: PipeOptions): Rule { + parseOptions('pipe', options); + options.path = options.path ? normalize(options.path) : options.path; const sourceDir = options.sourceDir; if (!sourceDir) { diff --git a/packages/schematics/angular/pipe/schema.d.ts b/packages/schematics/angular/pipe/schema.d.ts index f8e849e7ef..efb28f6034 100644 --- a/packages/schematics/angular/pipe/schema.d.ts +++ b/packages/schematics/angular/pipe/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + name: string; path?: string; appRoot?: string; diff --git a/packages/schematics/angular/pipe/schema.json b/packages/schematics/angular/pipe/schema.json index 2c5ae12562..259c8abd4d 100644 --- a/packages/schematics/angular/pipe/schema.json +++ b/packages/schematics/angular/pipe/schema.json @@ -4,6 +4,11 @@ "title": "Angular Pipe Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "name": { "type": "string" }, diff --git a/packages/schematics/angular/service/index.ts b/packages/schematics/angular/service/index.ts index b8800f9905..4024b778e5 100644 --- a/packages/schematics/angular/service/index.ts +++ b/packages/schematics/angular/service/index.ts @@ -24,6 +24,7 @@ import { import 'rxjs/add/operator/merge'; import * as ts from 'typescript'; import * as stringUtils from '../strings'; +import { parseOptions } from '../utility/args'; import { addProviderToModule } from '../utility/ast-utils'; import { InsertChange } from '../utility/change'; import { buildRelativePath, findModuleFromOptions } from '../utility/find-module'; @@ -70,6 +71,8 @@ function addProviderToNgModule(options: ServiceOptions): Rule { } export default function (options: ServiceOptions): Rule { + parseOptions('service', options); + options.path = options.path ? normalize(options.path) : options.path; const sourceDir = options.sourceDir; if (!sourceDir) { diff --git a/packages/schematics/angular/service/schema.d.ts b/packages/schematics/angular/service/schema.d.ts index 2cab9fc88f..328fe146a2 100644 --- a/packages/schematics/angular/service/schema.d.ts +++ b/packages/schematics/angular/service/schema.d.ts @@ -7,6 +7,15 @@ */ export interface Schema { + // tslint:disable-next-line:no-any + _?: any; + // tslint:disable-next-line:no-any + _angularCliConfig?: any; + // tslint:disable-next-line:no-any + _angularCliAppConfig?: any; + // tslint:disable-next-line:no-any + _angularCliParsedPath?: any; + name: string; path?: string; appRoot?: string; diff --git a/packages/schematics/angular/service/schema.json b/packages/schematics/angular/service/schema.json index 6585669a18..f70b373ab9 100644 --- a/packages/schematics/angular/service/schema.json +++ b/packages/schematics/angular/service/schema.json @@ -4,6 +4,11 @@ "title": "Angular Service Options Schema", "type": "object", "properties": { + "_": {}, + "_angularCliConfig": {}, + "_angularCliAppConfig": {}, + "_angularCliParsedPath": {}, + "name": { "type": "string" }, diff --git a/packages/schematics/angular/utility/args.ts b/packages/schematics/angular/utility/args.ts new file mode 100644 index 0000000000..9fc0b62493 --- /dev/null +++ b/packages/schematics/angular/utility/args.ts @@ -0,0 +1,56 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ +import { dasherize } from '../strings'; + + +// tslint:disable-next-line:no-any +export function parseOptions(schematicName: string, commandOptions: any) { + const { + _, + _angularCliConfig: cliConfig, + _angularCliAppConfig: appConfig, + _angularCliParsedPath: parsedPath, + } = commandOptions; + + // If name is passed in, just ignore it. It's either backward compatible or being called by + // another schematic. + if (commandOptions.name) { + return; + } + + commandOptions.name = dasherize(_[0].split(/[\\/]/g).pop()); + commandOptions.sourceDir = appConfig.root; + + const root = appConfig.root + '/'; + commandOptions.appRoot = parsedPath.appRoot === appConfig.root ? '' : + parsedPath.appRoot.startsWith(root) + ? parsedPath.appRoot.substr(root.length) + : parsedPath.appRoot; + + commandOptions.path = parsedPath.dir; + commandOptions.path = parsedPath.dir === appConfig.root ? '' : + parsedPath.dir.startsWith(root) + ? commandOptions.path.substr(root.length) + : commandOptions.path; + if (['component', 'directive'].indexOf(schematicName) !== -1) { + const root = commandOptions.$$originalRoot ? commandOptions.$$originalRoot() : commandOptions; + if (root.prefix === undefined) { + commandOptions.prefix = appConfig.prefix; + } + + if (schematicName === 'component') { + if (root.styleext === undefined) { + commandOptions.styleext = cliConfig.defaults && cliConfig.defaults.styleExt; + } + } + } + + if (schematicName === 'interface' && _[1]) { + commandOptions.type = _[1]; + } +}