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 = {
|
config.projects[myapp].targets.echo = {
|
||||||
executor: '@nrwl/workspace:run-commands',
|
executor: '@nrwl/workspace:run-commands',
|
||||||
options: {
|
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));
|
updateFile(workspaceConfigName(), JSON.stringify(config));
|
||||||
|
|
||||||
const result = runCLI(`run ${myapp}:echo --var1=x --var2=y`);
|
const result = runCLI(
|
||||||
expect(result).toContain('print: x');
|
`run ${myapp}:echo --var1=a --var2=b --var-hyphen=c --varCamelCase=d`
|
||||||
expect(result).toContain('print: y');
|
);
|
||||||
|
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();
|
done();
|
||||||
}, 120000);
|
}, 120000);
|
||||||
|
|
||||||
|
|||||||
@ -218,7 +218,7 @@ function transformCommand(
|
|||||||
) {
|
) {
|
||||||
if (command.indexOf('{args.') > -1) {
|
if (command.indexOf('{args.') > -1) {
|
||||||
const regex = /{args\.([^}]+)}/g;
|
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) {
|
} else if (Object.keys(args).length > 0 && forwardAllArgs) {
|
||||||
const stringifiedArgs = Object.keys(args)
|
const stringifiedArgs = Object.keys(args)
|
||||||
.map((a) => `--${a}=${args[a]}`)
|
.map((a) => `--${a}=${args[a]}`)
|
||||||
@ -234,21 +234,20 @@ function parseArgs(options: RunCommandsBuilderOptions) {
|
|||||||
if (!args) {
|
if (!args) {
|
||||||
const unknownOptionsTreatedAsArgs = Object.keys(options)
|
const unknownOptionsTreatedAsArgs = Object.keys(options)
|
||||||
.filter((p) => propKeys.indexOf(p) === -1)
|
.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 unknownOptionsTreatedAsArgs;
|
||||||
}
|
}
|
||||||
return args
|
return yargsParser(args.replace(/(^"|"$)/g, ''), {
|
||||||
.split(' ')
|
configuration: { 'camel-case-expansion': true },
|
||||||
.map((t) => t.trim())
|
});
|
||||||
.reduce((m, c) => {
|
|
||||||
if (!c.startsWith('--')) {
|
|
||||||
throw new Error(`Invalid args: ${args}`);
|
|
||||||
}
|
}
|
||||||
const [key, value] = c.substring(2).split('=');
|
|
||||||
if (!key || !value) {
|
function camelCase(input) {
|
||||||
throw new Error(`Invalid args: ${args}`);
|
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