Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): handle positional args correctly for run command #15193

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions e2e/nx-run/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ describe('Nx Running Tests', () => {
runCLI(`build ${myapp}`);
}, 10000);

it('should support project name positional arg non-consecutive to target', () => {
const myapp = uniq('app');
runCLI(`generate @nrwl/web:app ${myapp}`);

runCLI(`build --verbose ${myapp}`);
}, 10000);

it('should run targets from package json', () => {
const myapp = uniq('app');
const target = uniq('script');
Expand Down
30 changes: 25 additions & 5 deletions packages/nx/bin/init-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,35 @@ export function rewriteTargetsAndProjects(args: string[]) {
}

function rewritePositionalArguments(args: string[]) {
if (!args[3] || args[3].startsWith('-')) {
return ['run', `${wrapIntoQuotesIfNeeded(args[2])}`, ...args.slice(3)];
} else {
const relevantPositionalArgs = [];
const rest = [];
for (let i = 2; i < args.length; i++) {
if (!args[i].startsWith('-')) {
relevantPositionalArgs.push(args[i]);
if (relevantPositionalArgs.length === 2) {
rest.push(...args.slice(i + 1));
break;
}
} else {
rest.push(args[i]);
}
}

if (relevantPositionalArgs.length === 1) {
return [
'run',
`${args[3]}:${wrapIntoQuotesIfNeeded(args[2])}`,
...args.slice(4),
`${wrapIntoQuotesIfNeeded(relevantPositionalArgs[0])}`,
...rest,
];
}

return [
'run',
`${relevantPositionalArgs[1]}:${wrapIntoQuotesIfNeeded(
relevantPositionalArgs[0]
)}`,
...rest,
];
}

function wrapIntoQuotesIfNeeded(arg: string) {
Expand Down