fix(core): fix the parsing of projects and targets

This commit is contained in:
Victor Savkin 2022-12-27 12:05:05 -05:00
parent e91179e2b2
commit c3f0cef1fb
26 changed files with 167 additions and 136 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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}`);

View File

@ -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),
];
}
}

View File

@ -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',
]);
});
});

View File

@ -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) {