diff --git a/packages/tao/src/commands/generate.ts b/packages/tao/src/commands/generate.ts index cba8079278..0ab5046844 100644 --- a/packages/tao/src/commands/generate.ts +++ b/packages/tao/src/commands/generate.ts @@ -27,7 +27,10 @@ import { convertToCamelCase, handleErrors, Schema, - validateOptions, + Options, + lookupUnmatched, + convertAliases, + coerceTypes, } from '../shared/params'; import { commandName, printHelp } from '../shared/print-help'; // @ts-ignore @@ -317,7 +320,7 @@ async function runSchematic( const record = { loggingQueue: [] as string[], error: false }; workflow.reporter.subscribe(createRecorder(record, logger)); - const schematicOptions = validateOptions( + const schematicOptions = normalizeOptions( opts.schematicOptions, flattenedSchema ); @@ -421,6 +424,13 @@ export async function taoNew( }); } +function normalizeOptions(opts: Options, schema: Schema): Options { + return lookupUnmatched( + convertAliases(coerceTypes(opts, schema), schema, true), + schema + ); +} + function isTTY(): boolean { return !!process.stdout.isTTY && process.env['CI'] !== 'true'; } diff --git a/packages/tao/src/commands/run.ts b/packages/tao/src/commands/run.ts index 416d4af7b8..a348e4c0ca 100644 --- a/packages/tao/src/commands/run.ts +++ b/packages/tao/src/commands/run.ts @@ -10,9 +10,11 @@ import { import { NodeJsSyncHost } from '@angular-devkit/core/node'; import { getLogger } from '../shared/logger'; import { - coerceTypesAndNormalizeAliases, + coerceTypes, + convertAliases, convertToCamelCase, handleErrors, + Options, Schema, } from '../shared/params'; import { commandName, printHelp } from '../shared/print-help'; @@ -175,7 +177,7 @@ export async function run(root: string, args: string[], isVerbose: boolean) { return 0; } - const runOptions = coerceTypesAndNormalizeAliases( + const runOptions = normalizeOptions( opts.runOptions, flattenedSchema as any ); @@ -193,3 +195,7 @@ export async function run(root: string, args: string[], isVerbose: boolean) { return result.success ? 0 : 1; }); } + +function normalizeOptions(opts: Options, schema: Schema): Options { + return convertAliases(coerceTypes(opts, schema), schema, false); +} diff --git a/packages/tao/src/shared/params.spec.ts b/packages/tao/src/shared/params.spec.ts index b8bd6438c2..8b1d7c59f8 100644 --- a/packages/tao/src/shared/params.spec.ts +++ b/packages/tao/src/shared/params.spec.ts @@ -97,12 +97,13 @@ describe('params', () => { properties: { directory: { type: 'string', alias: 'd' } }, required: [], description: '', - } + }, + true ) ).toEqual({ directory: 'test' }); }); - it('should filter unknown keys into the leftovers field', () => { + it('should filter unknown keys into the leftovers field when excludeUnmatched is true', () => { expect( convertAliases( { d: 'test' }, @@ -110,7 +111,8 @@ describe('params', () => { properties: { directory: { type: 'string' } }, required: [], description: '', - } + }, + true ) ).toEqual({ '--': [ @@ -121,6 +123,22 @@ describe('params', () => { ], }); }); + + it('should not filter unknown keys into the leftovers field when excludeUnmatched is false', () => { + expect( + convertAliases( + { d: 'test' }, + { + properties: { directory: { type: 'string' } }, + required: [], + description: '', + }, + false + ) + ).toEqual({ + d: 'test', + }); + }); }); describe('lookupUnmatched', () => { diff --git a/packages/tao/src/shared/params.ts b/packages/tao/src/shared/params.ts index d4b000bb3d..b5ded896b9 100644 --- a/packages/tao/src/shared/params.ts +++ b/packages/tao/src/shared/params.ts @@ -83,7 +83,11 @@ export function coerceTypes(opts: Options, schema: Schema): Options { * @param opts The options passed in by the user * @param schema The schema definition to check against */ -export function convertAliases(opts: Options, schema: Schema): Options { +export function convertAliases( + opts: Options, + schema: Schema, + excludeUnmatched: boolean +): Options { return Object.keys(opts).reduce((acc, k) => { if (schema.properties[k]) { acc[k] = opts[k]; @@ -93,7 +97,7 @@ export function convertAliases(opts: Options, schema: Schema): Options { ); if (found) { acc[found[0]] = opts[k]; - } else { + } else if (excludeUnmatched) { if (!acc['--']) { acc['--'] = []; } @@ -101,6 +105,8 @@ export function convertAliases(opts: Options, schema: Schema): Options { name: k, possible: [], }); + } else { + acc[k] = opts[k]; } } return acc; @@ -126,26 +132,3 @@ export function lookupUnmatched(opts: Options, schema: Schema): Options { } return opts; } - -export function coerceTypesAndNormalizeAliases( - opts: Options, - schema: Schema -): Options { - return convertAliases(coerceTypes(opts, schema), schema); -} - -/** - * Converts aliases and coerces types according to the schema - * - * @param opts The options to check - * @param schema The schema definition to validate against - * - * @remarks - * - * Unmatched options are added to opts['--'] - * and listed along with possible schema matches - * - */ -export function validateOptions(opts: Options, schema: Schema): Options { - return lookupUnmatched(coerceTypesAndNormalizeAliases(opts, schema), schema); -}