From c3f0cef1fbdaefe96b896a9ed9cb54a1a2143b9e Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Tue, 27 Dec 2022 12:05:05 -0500 Subject: [PATCH] fix(core): fix the parsing of projects and targets --- docs/generated/cli/affected-apps.md | 8 +-- docs/generated/cli/affected-graph.md | 8 +-- docs/generated/cli/affected-libs.md | 8 +-- docs/generated/cli/affected.md | 10 ++-- docs/generated/cli/exec.md | 6 +- docs/generated/cli/format-check.md | 12 ++-- docs/generated/cli/format-write.md | 12 ++-- docs/generated/cli/graph.md | 2 +- docs/generated/cli/print-affected.md | 10 ++-- docs/generated/cli/run-many.md | 8 +-- docs/generated/cli/watch.md | 2 +- .../packages/nx/documents/affected-apps.md | 8 +-- .../nx/documents/affected-dep-graph.md | 8 +-- .../packages/nx/documents/affected-libs.md | 8 +-- .../packages/nx/documents/affected.md | 10 ++-- .../packages/nx/documents/dep-graph.md | 2 +- docs/generated/packages/nx/documents/exec.md | 6 +- .../packages/nx/documents/format-check.md | 12 ++-- .../packages/nx/documents/format-write.md | 12 ++-- .../packages/nx/documents/print-affected.md | 10 ++-- .../packages/nx/documents/run-many.md | 8 +-- docs/generated/packages/nx/documents/watch.md | 2 +- e2e/nx-run/src/run.test.ts | 4 +- packages/nx/bin/init-local.ts | 56 +++++++++++++++---- packages/nx/bin/rewrite-args.spec.ts | 37 ++++++++++++ packages/nx/src/command-line/nx-commands.ts | 34 +++++------ 26 files changed, 167 insertions(+), 136 deletions(-) create mode 100644 packages/nx/bin/rewrite-args.spec.ts diff --git a/docs/generated/cli/affected-apps.md b/docs/generated/cli/affected-apps.md index 3a012aa50e8d7..91d76a523594d 100644 --- a/docs/generated/cli/affected-apps.md +++ b/docs/generated/cli/affected-apps.md @@ -53,17 +53,15 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head diff --git a/docs/generated/cli/affected-graph.md b/docs/generated/cli/affected-graph.md index e7d73c28ce78f..6fdbdcd9e34c6 100644 --- a/docs/generated/cli/affected-graph.md +++ b/docs/generated/cli/affected-graph.md @@ -69,9 +69,7 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed @@ -83,9 +81,9 @@ Output file (e.g. --file=output.json or --file=dep-graph.html) ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### focus diff --git a/docs/generated/cli/affected-libs.md b/docs/generated/cli/affected-libs.md index 5a2d6cd60b67e..b42214555a88a 100644 --- a/docs/generated/cli/affected-libs.md +++ b/docs/generated/cli/affected-libs.md @@ -53,17 +53,15 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head diff --git a/docs/generated/cli/affected.md b/docs/generated/cli/affected.md index bf0bb37696138..1eee1057d3a05 100644 --- a/docs/generated/cli/affected.md +++ b/docs/generated/cli/affected.md @@ -81,17 +81,15 @@ This is the configuration to use when performing tasks on projects ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head @@ -151,7 +149,7 @@ Rerun the tasks even when the results are available in the cache ### targets -Type: `array` +Type: `string` Tasks to run for affected projects diff --git a/docs/generated/cli/exec.md b/docs/generated/cli/exec.md index d73b6ef78ee3c..804de6fd12b0b 100644 --- a/docs/generated/cli/exec.md +++ b/docs/generated/cli/exec.md @@ -25,9 +25,7 @@ This is the configuration to use when performing tasks on projects ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed @@ -83,7 +81,7 @@ Rerun the tasks even when the results are available in the cache ### targets -Type: `array` +Type: `string` Tasks to run for affected projects diff --git a/docs/generated/cli/format-check.md b/docs/generated/cli/format-check.md index 4c5d2795cbb59..d8517f03dcc3a 100644 --- a/docs/generated/cli/format-check.md +++ b/docs/generated/cli/format-check.md @@ -31,17 +31,15 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head @@ -63,9 +61,9 @@ Format only libraries and applications files. ### projects -Type: `array` +Type: `string` -Projects to format (comma delimited) +Projects to format (comma/space delimited) ### uncommitted diff --git a/docs/generated/cli/format-write.md b/docs/generated/cli/format-write.md index 80df4c283724b..730e4125f0999 100644 --- a/docs/generated/cli/format-write.md +++ b/docs/generated/cli/format-write.md @@ -31,17 +31,15 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head @@ -63,9 +61,9 @@ Format only libraries and applications files. ### projects -Type: `array` +Type: `string` -Projects to format (comma delimited) +Projects to format (comma/space delimited) ### uncommitted diff --git a/docs/generated/cli/graph.md b/docs/generated/cli/graph.md index 4152b0323e001..df1441fd25bcf 100644 --- a/docs/generated/cli/graph.md +++ b/docs/generated/cli/graph.md @@ -69,7 +69,7 @@ Watch for changes to project graph and update in-browser: ### exclude -Type: `array` +Type: `string` List of projects delimited by commas to exclude from the project graph. diff --git a/docs/generated/cli/print-affected.md b/docs/generated/cli/print-affected.md index c0badf29ad47b..5a1fce8598972 100644 --- a/docs/generated/cli/print-affected.md +++ b/docs/generated/cli/print-affected.md @@ -69,17 +69,15 @@ This is the configuration to use when performing tasks on projects ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head @@ -101,7 +99,7 @@ Select the subset of the returned json document (e.g., --select=projects) ### targets -Type: `array` +Type: `string` Tasks to run for affected projects diff --git a/docs/generated/cli/run-many.md b/docs/generated/cli/run-many.md index 853ec978981bc..676b14b2038d5 100644 --- a/docs/generated/cli/run-many.md +++ b/docs/generated/cli/run-many.md @@ -59,9 +59,7 @@ This is the configuration to use when performing tasks on projects ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed @@ -105,7 +103,7 @@ Max number of parallel processes [default is 3] Type: `string` -Projects to run. (comma delimited project names and/or patterns) +Projects to run. (comma/space delimited project names and/or patterns) ### runner @@ -123,7 +121,7 @@ Rerun the tasks even when the results are available in the cache ### targets -Type: `array` +Type: `string` Tasks to run for affected projects diff --git a/docs/generated/cli/watch.md b/docs/generated/cli/watch.md index 6d608b5f0ea72..e35e2fccdc1af 100644 --- a/docs/generated/cli/watch.md +++ b/docs/generated/cli/watch.md @@ -59,7 +59,7 @@ When watching selected projects, include dependent projects as well. Type: `string` -Projects to watch (comma delimited). +Projects to watch (comma/space delimited). ### verbose diff --git a/docs/generated/packages/nx/documents/affected-apps.md b/docs/generated/packages/nx/documents/affected-apps.md index 3a012aa50e8d7..91d76a523594d 100644 --- a/docs/generated/packages/nx/documents/affected-apps.md +++ b/docs/generated/packages/nx/documents/affected-apps.md @@ -53,17 +53,15 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head diff --git a/docs/generated/packages/nx/documents/affected-dep-graph.md b/docs/generated/packages/nx/documents/affected-dep-graph.md index e7d73c28ce78f..6fdbdcd9e34c6 100644 --- a/docs/generated/packages/nx/documents/affected-dep-graph.md +++ b/docs/generated/packages/nx/documents/affected-dep-graph.md @@ -69,9 +69,7 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed @@ -83,9 +81,9 @@ Output file (e.g. --file=output.json or --file=dep-graph.html) ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### focus diff --git a/docs/generated/packages/nx/documents/affected-libs.md b/docs/generated/packages/nx/documents/affected-libs.md index 5a2d6cd60b67e..b42214555a88a 100644 --- a/docs/generated/packages/nx/documents/affected-libs.md +++ b/docs/generated/packages/nx/documents/affected-libs.md @@ -53,17 +53,15 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head diff --git a/docs/generated/packages/nx/documents/affected.md b/docs/generated/packages/nx/documents/affected.md index bf0bb37696138..1eee1057d3a05 100644 --- a/docs/generated/packages/nx/documents/affected.md +++ b/docs/generated/packages/nx/documents/affected.md @@ -81,17 +81,15 @@ This is the configuration to use when performing tasks on projects ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head @@ -151,7 +149,7 @@ Rerun the tasks even when the results are available in the cache ### targets -Type: `array` +Type: `string` Tasks to run for affected projects diff --git a/docs/generated/packages/nx/documents/dep-graph.md b/docs/generated/packages/nx/documents/dep-graph.md index 4152b0323e001..df1441fd25bcf 100644 --- a/docs/generated/packages/nx/documents/dep-graph.md +++ b/docs/generated/packages/nx/documents/dep-graph.md @@ -69,7 +69,7 @@ Watch for changes to project graph and update in-browser: ### exclude -Type: `array` +Type: `string` List of projects delimited by commas to exclude from the project graph. diff --git a/docs/generated/packages/nx/documents/exec.md b/docs/generated/packages/nx/documents/exec.md index d73b6ef78ee3c..804de6fd12b0b 100644 --- a/docs/generated/packages/nx/documents/exec.md +++ b/docs/generated/packages/nx/documents/exec.md @@ -25,9 +25,7 @@ This is the configuration to use when performing tasks on projects ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed @@ -83,7 +81,7 @@ Rerun the tasks even when the results are available in the cache ### targets -Type: `array` +Type: `string` Tasks to run for affected projects diff --git a/docs/generated/packages/nx/documents/format-check.md b/docs/generated/packages/nx/documents/format-check.md index 4c5d2795cbb59..d8517f03dcc3a 100644 --- a/docs/generated/packages/nx/documents/format-check.md +++ b/docs/generated/packages/nx/documents/format-check.md @@ -31,17 +31,15 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head @@ -63,9 +61,9 @@ Format only libraries and applications files. ### projects -Type: `array` +Type: `string` -Projects to format (comma delimited) +Projects to format (comma/space delimited) ### uncommitted diff --git a/docs/generated/packages/nx/documents/format-write.md b/docs/generated/packages/nx/documents/format-write.md index 80df4c283724b..730e4125f0999 100644 --- a/docs/generated/packages/nx/documents/format-write.md +++ b/docs/generated/packages/nx/documents/format-write.md @@ -31,17 +31,15 @@ Base of the current branch (usually main) ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head @@ -63,9 +61,9 @@ Format only libraries and applications files. ### projects -Type: `array` +Type: `string` -Projects to format (comma delimited) +Projects to format (comma/space delimited) ### uncommitted diff --git a/docs/generated/packages/nx/documents/print-affected.md b/docs/generated/packages/nx/documents/print-affected.md index c0badf29ad47b..5a1fce8598972 100644 --- a/docs/generated/packages/nx/documents/print-affected.md +++ b/docs/generated/packages/nx/documents/print-affected.md @@ -69,17 +69,15 @@ This is the configuration to use when performing tasks on projects ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed ### files -Type: `array` +Type: `string` -Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas +Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces ### head @@ -101,7 +99,7 @@ Select the subset of the returned json document (e.g., --select=projects) ### targets -Type: `array` +Type: `string` Tasks to run for affected projects diff --git a/docs/generated/packages/nx/documents/run-many.md b/docs/generated/packages/nx/documents/run-many.md index 853ec978981bc..676b14b2038d5 100644 --- a/docs/generated/packages/nx/documents/run-many.md +++ b/docs/generated/packages/nx/documents/run-many.md @@ -59,9 +59,7 @@ This is the configuration to use when performing tasks on projects ### exclude -Type: `array` - -Default: `[]` +Type: `string` Exclude certain projects from being processed @@ -105,7 +103,7 @@ Max number of parallel processes [default is 3] Type: `string` -Projects to run. (comma delimited project names and/or patterns) +Projects to run. (comma/space delimited project names and/or patterns) ### runner @@ -123,7 +121,7 @@ Rerun the tasks even when the results are available in the cache ### targets -Type: `array` +Type: `string` Tasks to run for affected projects diff --git a/docs/generated/packages/nx/documents/watch.md b/docs/generated/packages/nx/documents/watch.md index 6d608b5f0ea72..e35e2fccdc1af 100644 --- a/docs/generated/packages/nx/documents/watch.md +++ b/docs/generated/packages/nx/documents/watch.md @@ -59,7 +59,7 @@ When watching selected projects, include dependent projects as well. Type: `string` -Projects to watch (comma delimited). +Projects to watch (comma/space delimited). ### verbose diff --git a/e2e/nx-run/src/run.test.ts b/e2e/nx-run/src/run.test.ts index f8410d7db46ca..d2ad53f80b2a2 100644 --- a/e2e/nx-run/src/run.test.ts +++ b/e2e/nx-run/src/run.test.ts @@ -448,7 +448,9 @@ describe('Nx Running Tests', () => { runCLI(`generate @nrwl/web:app ${myapp1}`); runCLI(`generate @nrwl/web:app ${myapp2}`); - let outputs = runCLI(`run-many -t build test -p ${myapp1} ${myapp2}`); + let outputs = runCLI( + `run-many -t build test -p ${myapp1} ${myapp2} --ci` + ); expect(outputs).toContain('Running targets build, test for 2 projects:'); outputs = runCLI(`run-many -t build test -p=${myapp1},${myapp2}`); diff --git a/packages/nx/bin/init-local.ts b/packages/nx/bin/init-local.ts index d116f4c289701..750ebe8f78dd6 100644 --- a/packages/nx/bin/init-local.ts +++ b/packages/nx/bin/init-local.ts @@ -24,9 +24,16 @@ export function initLocal(workspace: WorkspaceTypeAndRoot) { } if (isKnownCommand()) { - commandsObject.argv; + const newArgs = rewriteTargetsAndProjects(process.argv); + const help = newArgs.indexOf('--help'); + const split = newArgs.indexOf('--'); + if (help > -1 && (split === -1 || split > help)) { + commandsObject.showHelp(); + } else { + commandsObject.parse(newArgs); + } } else { - const newArgs = rewritePositionalArguments(); + const newArgs = rewritePositionalArguments(process.argv); commandsObject.parse(newArgs); } } catch (e) { @@ -35,18 +42,45 @@ export function initLocal(workspace: WorkspaceTypeAndRoot) { } } -function rewritePositionalArguments() { - if (!process.argv[3] || process.argv[3].startsWith('-')) { - return [ - 'run', - `${wrapIntoQuotesIfNeeded(process.argv[2])}`, - ...process.argv.slice(3), - ]; +export function rewriteTargetsAndProjects(args: string[]) { + const newArgs = [args[2]]; + let i = 3; + while (i < args.length) { + if (args[i] === '--') { + return [...newArgs, ...args.slice(i)]; + } else if ( + args[i] === '-p' || + args[i] === '--projects' || + args[i] === '--exclude' || + args[i] === '--files' || + args[i] === '-t' || + args[i] === '--target' || + args[i] === '--targets' + ) { + newArgs.push(args[i]); + i++; + const items = []; + while (i < args.length && !args[i].startsWith('-')) { + items.push(args[i]); + i++; + } + newArgs.push(items.join(',')); + } else { + newArgs.push(args[i]); + ++i; + } + } + return newArgs; +} + +function rewritePositionalArguments(args: string[]) { + if (!args[3] || args[3].startsWith('-')) { + return ['run', `${wrapIntoQuotesIfNeeded(args[2])}`, ...args.slice(3)]; } else { return [ 'run', - `${process.argv[3]}:${wrapIntoQuotesIfNeeded(process.argv[2])}`, - ...process.argv.slice(4), + `${args[3]}:${wrapIntoQuotesIfNeeded(args[2])}`, + ...args.slice(4), ]; } } diff --git a/packages/nx/bin/rewrite-args.spec.ts b/packages/nx/bin/rewrite-args.spec.ts new file mode 100644 index 0000000000000..368e2e063cf52 --- /dev/null +++ b/packages/nx/bin/rewrite-args.spec.ts @@ -0,0 +1,37 @@ +import { rewriteTargetsAndProjects } from './init-local'; + +describe('rewriteTargetsAndProjects', () => { + it('should rewrite projects, exclude and targets', () => { + const res = rewriteTargetsAndProjects([ + 'node', + 'nx', + 'run-many', + '--before', + '-p', + 'one', + 'two', + '--exclude', + 'one', + 'two', + '-t', + 'test', + 'lint', + '--ci', + '--', + 'some-arg', + ]); + expect(res).toEqual([ + 'run-many', + '--before', + '-p', + 'one,two', + '--exclude', + 'one,two', + '-t', + 'test,lint', + '--ci', + '--', + 'some-arg', + ]); + }); +}); diff --git a/packages/nx/src/command-line/nx-commands.ts b/packages/nx/src/command-line/nx-commands.ts index 39fd437ef62ba..31e9e0b453296 100644 --- a/packages/nx/src/command-line/nx-commands.ts +++ b/packages/nx/src/command-line/nx-commands.ts @@ -404,8 +404,8 @@ function withFormatOptions(yargs: yargs.Argv): yargs.Argv { type: 'boolean', }) .option('projects', { - describe: 'Projects to format (comma delimited)', - type: 'array', + describe: 'Projects to format (comma/space delimited)', + type: 'string', coerce: parseCSV, }) .conflicts({ @@ -448,9 +448,9 @@ function withPlainOption(yargs: yargs.Argv): yargs.Argv { function withExcludeOption(yargs: yargs.Argv): yargs.Argv { return yargs.option('exclude', { describe: 'Exclude certain projects from being processed', - type: 'array', + type: 'string', coerce: parseCSV, - default: [], + default: '', }); } @@ -515,8 +515,8 @@ function withAffectedOptions(yargs: yargs.Argv): yargs.Argv { }) .option('files', { describe: - 'Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas', - type: 'array', + 'Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces', + type: 'string', requiresArg: true, coerce: parseCSV, }) @@ -571,12 +571,11 @@ function withRunManyOptions(yargs: yargs.Argv): yargs.Argv { 'populate--': true, }) .option('projects', { - type: 'array', - string: true, + type: 'string', alias: 'p', coerce: parseCSV, describe: - 'Projects to run. (comma delimited project names and/or patterns)', + 'Projects to run. (comma/space delimited project names and/or patterns)', }) .option('all', { describe: '[deprecated] Run the target on all projects in the workspace', @@ -600,7 +599,7 @@ function withDepGraphOptions(yargs: yargs.Argv): yargs.Argv { .option('exclude', { describe: 'List of projects delimited by commas to exclude from the project graph.', - type: 'array', + type: 'string', coerce: parseCSV, }) @@ -654,7 +653,7 @@ function withTargetAndConfigurationOption( ): yargs.Argv { return withConfiguration(yargs).option('targets', { describe: 'Tasks to run for affected projects', - type: 'array', + type: 'string', alias: ['target', 't'], requiresArg: true, coerce: parseCSV, @@ -949,11 +948,10 @@ function withWatchOptions(yargs: yargs.Argv) { 'populate--': true, }) .option('projects', { - type: 'array', - string: true, + type: 'string', alias: 'p', coerce: parseCSV, - description: 'Projects to watch (comma delimited).', + description: 'Projects to watch (comma/space delimited).', }) .option('all', { type: 'boolean', @@ -998,15 +996,11 @@ function withWatchOptions(yargs: yargs.Argv) { }, true); } -function parseCSV(args: string[]) { +function parseCSV(args: string) { if (!args) { return args; } - return args - .map((arg) => arg.split(',')) - .reduce((acc, value) => { - return [...acc, ...value]; - }, [] as string[]); + return args.split(','); } function linkToNxDevAndExamples(yargs: yargs.Argv, command: string) {