diff --git a/packages/nx/src/command-line/yargs-utils/shared-options.spec.ts b/packages/nx/src/command-line/yargs-utils/shared-options.spec.ts new file mode 100644 index 0000000000000..63f8694415dd0 --- /dev/null +++ b/packages/nx/src/command-line/yargs-utils/shared-options.spec.ts @@ -0,0 +1,80 @@ +import * as yargs from 'yargs'; + +import { withAffectedOptions, withRunManyOptions } from './shared-options'; + +const argv = yargs.default([]); + +describe('shared-options', () => { + describe('withAffectedOptions', () => { + const command = withAffectedOptions(argv); + + it('should parse files to array', () => { + const result = command.parseSync([ + 'affected', + '--files', + 'file1', + '--files', + 'file2', + '--tag', + '81919e4', + '--parallel', + '3', + '--maxParallel', + '2', + ]); + expect(result).toEqual( + expect.objectContaining({ + _: ['affected', '--tag', '81919e4'], + files: ['file1', 'file2'], + parallel: '3', + maxParallel: 2, + }) + ); + }); + + it('should parse head and base', () => { + const result = command.parseSync([ + 'affected', + '--head', + 'head', + '--base', + 'base', + ]); + expect(result).toEqual( + expect.objectContaining({ + _: ['affected'], + head: 'head', + base: 'base', + }) + ); + }); + }); + + describe('withRunManyOptions', () => { + const command = withRunManyOptions(argv); + + it('should parse projects to array', () => { + const result = command.parseSync([ + 'run-many', + '--projects', + 'project1', + '--projects', + 'project2', + '--tag', + '81919e4', + '--parallel', + '3', + '--maxParallel', + '2', + ]); + expect(result).toEqual( + expect.objectContaining({ + _: ['run-many', '--tag', '81919e4'], + projects: ['project1', 'project2'], + parallel: '3', + maxParallel: 2, + }) + ); + }); + }); +}); diff --git a/packages/nx/src/command-line/yargs-utils/shared-options.ts b/packages/nx/src/command-line/yargs-utils/shared-options.ts index a47701c89bc81..57a9f8e0b2d03 100644 --- a/packages/nx/src/command-line/yargs-utils/shared-options.ts +++ b/packages/nx/src/command-line/yargs-utils/shared-options.ts @@ -1,9 +1,18 @@ -import { Argv } from 'yargs'; +import { Argv, ParserConfigurationOptions } from 'yargs'; interface ExcludeOptions { exclude: string[]; } +export const defaultYargsParserConfiguration: Partial = + { + 'strip-dashed': true, + 'unknown-options-as-args': true, + 'populate--': true, + 'parse-numbers': false, + 'parse-positional-numbers': false, + }; + export function withExcludeOption(yargs: Argv): Argv { return yargs.option('exclude', { describe: 'Exclude certain projects from being processed', @@ -151,11 +160,7 @@ export function withBatch(yargs: Argv) { export function withAffectedOptions(yargs: Argv) { return withExcludeOption(yargs) - .parserConfiguration({ - 'strip-dashed': true, - 'unknown-options-as-args': true, - 'populate--': true, - }) + .parserConfiguration(defaultYargsParserConfiguration) .option('files', { describe: 'Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces', @@ -210,11 +215,7 @@ export function withRunManyOptions( yargs: Argv ): Argv { return withRunOptions(yargs) - .parserConfiguration({ - 'strip-dashed': true, - 'unknown-options-as-args': true, - 'populate--': true, - }) + .parserConfiguration(defaultYargsParserConfiguration) .option('projects', { type: 'string', alias: 'p', @@ -287,11 +288,7 @@ export function withRunOneOptions(yargs: Argv) { const res = withRunOptions( withOutputStyleOption(withConfiguration(yargs), allOutputStyles) ) - .parserConfiguration({ - 'strip-dashed': true, - 'unknown-options-as-args': true, - 'populate--': true, - }) + .parserConfiguration(defaultYargsParserConfiguration) .option('project', { describe: 'Target project', type: 'string',