fix(core): Leave passthrough parameters untouched

Previously when the passing through complex parameters nx would alter
the original parameters in the following  scenarios:
- When passing through parameters, dot notation parameters would be
parsed and no longer would be passthrough the the following process
correctly ( --cucumberOpts.timeout=10000" would instead be passed as
"--cucumberOpts=[Object]")
- strings with spaces (delimited by quotes) would be rendered as
multiple words instead ('--tags="not @exclude"' would be passed as
'--tags=not @exclude' where '@exclude' no longer is part of the tags
param)

This would that when running nx  cli to run a command the final command
would not get the correct parameters
This commit is contained in:
Luís Miguel Cabral 2021-09-20 15:13:34 +02:00 committed by Victor Savkin
parent 3e0e30eceb
commit bc2d9c0cc6
6 changed files with 43 additions and 2 deletions

View File

@ -84,6 +84,7 @@ export function parseRunOneOptions(
},
configuration: {
'strip-dashed': true,
'dot-notation': false,
},
});

View File

@ -3,7 +3,12 @@ import { dirname, join } from 'path';
import { existsSync } from 'fs-extra';
import * as yargsParser from 'yargs-parser';
const argv = yargsParser(process.argv.slice(2));
const argv = yargsParser(process.argv.slice(2), {
configuration: {
'strip-dashed': true,
'dot-notation': false,
},
});
export async function invokeCommand(
command: string,

View File

@ -58,6 +58,10 @@ function parseRunOpts(
alias: {
c: 'configuration',
},
configuration: {
'strip-dashed': false,
'dot-notation': false,
},
})
);
const help = runOptions.help as boolean;

View File

@ -82,6 +82,7 @@ export function splitArgsIntoNxArgsAndOverrides(
const overrides = yargsParser(args._ as string[], {
configuration: {
'strip-dashed': true,
'dot-notation': false,
},
});
// This removes the overrides from the nxArgs._

View File

@ -73,6 +73,32 @@ describe('Command Runner Builder', () => {
});
});
it('should add args containing spaces to in the command', async () => {
const exec = jest.spyOn(require('child_process'), 'execSync');
await runCommands(
{
command: `echo`,
a: 123,
b: '4 5 6',
c: '4 "5" 6',
},
context
);
expect(exec).toHaveBeenCalledWith(
`echo --a=123 --b="4 5 6" --c="4 \"5\" 6"`,
{
stdio: [0, 1, 2],
cwd: undefined,
env: {
...process.env,
...env(),
},
maxBuffer: LARGE_BUFFER,
}
);
});
it('should forward args by default when using commands (plural)', async () => {
const exec = jest.spyOn(require('child_process'), 'exec');

View File

@ -249,7 +249,11 @@ function transformCommand(
return command.replace(regex, (_, group: string) => args[camelCase(group)]);
} else if (Object.keys(args).length > 0 && forwardAllArgs) {
const stringifiedArgs = Object.keys(args)
.map((a) => `--${a}=${args[a]}`)
.map((a) =>
typeof args[a] === 'string' && args[a].includes(' ')
? `--${a}="${args[a].replace(/"/g, '"')}"`
: `--${a}=${args[a]}`
)
.join(' ');
return `${command} ${stringifiedArgs}`;
} else {