Skip to content

Commit

Permalink
fix(misc): don't interpolate arg as undefined (#18666)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Oct 5, 2023
1 parent 7214791 commit 466dba0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
28 changes: 28 additions & 0 deletions packages/nx/src/executors/run-commands/run-commands.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,34 @@ describe('Run Commands', () => {
)
).toEqual('echo one -a=b');
});

it("shouldn't add literal `undefined` if arg is not provided", () => {
expect(
interpolateArgsIntoCommand(
'echo {args.someValue}',
{
parsedArgs: {},
__unparsed__: [],
},
false
)
).not.toContain('undefined');
});

it('should interpolate provided values', () => {
expect(
interpolateArgsIntoCommand(
'echo {args.someValue}',
{
parsedArgs: {
someValue: '"hello world"',
},
__unparsed__: [],
},
false
)
).toEqual('echo "hello world"');
});
});

describe('--color', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/nx/src/executors/run-commands/run-commands.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,14 @@ function processEnv(color: boolean, cwd: string) {

export function interpolateArgsIntoCommand(
command: string,
opts: NormalizedRunCommandsOptions,
opts: Pick<NormalizedRunCommandsOptions, 'parsedArgs' | '__unparsed__'>,
forwardAllArgs: boolean
) {
if (command.indexOf('{args.') > -1) {
const regex = /{args\.([^}]+)}/g;
return command.replace(regex, (_, group: string) => opts.parsedArgs[group]);
return command.replace(regex, (_, group: string) =>
opts.parsedArgs[group] !== undefined ? opts.parsedArgs[group] : ''
);
} else if (forwardAllArgs) {
return `${command}${
opts.__unparsed__.length > 0 ? ' ' + opts.__unparsed__.join(' ') : ''
Expand Down

0 comments on commit 466dba0

Please sign in to comment.