fix(core): handle multiple & hyphen command arguments through --args
Closes #2846
This commit is contained in:
parent
1eda4dca18
commit
bf599d14cd
@ -51,14 +51,31 @@ describe('Run Commands', () => {
|
||||
config.projects[myapp].targets.echo = {
|
||||
executor: '@nrwl/workspace:run-commands',
|
||||
options: {
|
||||
commands: [`echo 'print: {args.var1}'`, `echo 'print: {args.var2}'`],
|
||||
commands: [
|
||||
`echo 'var1: {args.var1}'`,
|
||||
`echo 'var2: {args.var2}'`,
|
||||
`echo 'hyphen: {args.var-hyphen}'`,
|
||||
`echo 'camel: {args.varCamelCase}'`,
|
||||
],
|
||||
},
|
||||
};
|
||||
updateFile(workspaceConfigName(), JSON.stringify(config));
|
||||
|
||||
const result = runCLI(`run ${myapp}:echo --var1=x --var2=y`);
|
||||
expect(result).toContain('print: x');
|
||||
expect(result).toContain('print: y');
|
||||
const result = runCLI(
|
||||
`run ${myapp}:echo --var1=a --var2=b --var-hyphen=c --varCamelCase=d`
|
||||
);
|
||||
expect(result).toContain('var1: a');
|
||||
expect(result).toContain('var2: b');
|
||||
expect(result).toContain('hyphen: c');
|
||||
expect(result).toContain('camel: d');
|
||||
|
||||
const resultArgs = runCLI(
|
||||
`run ${myapp}:echo --args="--var1=a --var2=b --var-hyphen=c --varCamelCase=d"`
|
||||
);
|
||||
expect(resultArgs).toContain('var1: a');
|
||||
expect(resultArgs).toContain('var2: b');
|
||||
expect(resultArgs).toContain('hyphen: c');
|
||||
expect(resultArgs).toContain('camel: d');
|
||||
done();
|
||||
}, 120000);
|
||||
|
||||
|
||||
@ -218,7 +218,7 @@ function transformCommand(
|
||||
) {
|
||||
if (command.indexOf('{args.') > -1) {
|
||||
const regex = /{args\.([^}]+)}/g;
|
||||
return command.replace(regex, (_, group: string) => args[group]);
|
||||
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]}`)
|
||||
@ -234,21 +234,20 @@ function parseArgs(options: RunCommandsBuilderOptions) {
|
||||
if (!args) {
|
||||
const unknownOptionsTreatedAsArgs = Object.keys(options)
|
||||
.filter((p) => propKeys.indexOf(p) === -1)
|
||||
.reduce((m, c) => ((m[c] = options[c]), m), {});
|
||||
.reduce((m, c) => ((m[camelCase(c)] = options[c]), m), {});
|
||||
return unknownOptionsTreatedAsArgs;
|
||||
}
|
||||
return args
|
||||
.split(' ')
|
||||
.map((t) => t.trim())
|
||||
.reduce((m, c) => {
|
||||
if (!c.startsWith('--')) {
|
||||
throw new Error(`Invalid args: ${args}`);
|
||||
return yargsParser(args.replace(/(^"|"$)/g, ''), {
|
||||
configuration: { 'camel-case-expansion': true },
|
||||
});
|
||||
}
|
||||
const [key, value] = c.substring(2).split('=');
|
||||
if (!key || !value) {
|
||||
throw new Error(`Invalid args: ${args}`);
|
||||
|
||||
function camelCase(input) {
|
||||
if (input.indexOf('-') > 1) {
|
||||
return input
|
||||
.toLowerCase()
|
||||
.replace(/-(.)/g, (match, group1) => group1.toUpperCase());
|
||||
} else {
|
||||
return input;
|
||||
}
|
||||
m[key] = value;
|
||||
return m;
|
||||
}, {});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user