From a097997be2b28e5f4c4452022ebdde419732aa39 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Wed, 15 Jun 2022 10:25:47 -0400 Subject: [PATCH] fix(misc): handle missing __unparsed__ --- docs/generated/packages/nx.json | 2 +- .../run-commands/run-commands.impl.spec.ts | 18 ++++++++++++++++++ .../run-commands/run-commands.impl.ts | 9 +++++---- .../nx/src/executors/run-commands/schema.json | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/docs/generated/packages/nx.json b/docs/generated/packages/nx.json index ad0e01f74a..f980c6ccb7 100644 --- a/docs/generated/packages/nx.json +++ b/docs/generated/packages/nx.json @@ -148,7 +148,7 @@ } }, "additionalProperties": true, - "required": ["__unparsed__"], + "required": [], "examplesFile": "`workspace.json`:\n\n```json\n//...\n\"frontend\": {\n \"targets\": {\n //...\n \"ls-project-root\": {\n \"executor\": \"nx:run-commands\",\n \"options\": {\n \"command\": \"ls apps/frontend/src\"\n }\n }\n }\n}\n```\n\n```bash\nnx run frontend:ls-project-root\n```\n\n##### Chaining commands, interpolating args and setting the cwd\n\nLet's say each of our workspace projects has some custom bash scripts in a `scripts` folder.\nWe want a simple way to create empty bash script files for a given project, that have the execute permissions already set.\n\nGiven that Nx knows our workspace structure, we should be able to give it a project and the name of our script, and it should take care of the rest.\n\nThe `commands` option accepts as many commands as you want. By default, they all run in parallel.\nYou can run them sequentially by setting `parallel: false`:\n\n```json\n\"create-script\": {\n \"executor\": \"nx:run-commands\",\n \"options\": {\n \"commands\": [\n \"mkdir -p scripts\",\n \"touch scripts/{args.name}.sh\",\n \"chmod +x scripts/{args.name}.sh\"\n ],\n \"cwd\": \"apps/frontend\",\n \"parallel\": false\n }\n}\n```\n\nBy setting the `cwd` option, each command will run in the `apps/frontend` folder.\n\nWe run the above with:\n\n```bash\nnx run frontend:create-script --args=\"--name=example\"\n```\n\nor simply with:\n\n```bash\nnx run frontend:create-script --name=example\n```\n\n##### Arguments forwarding\n\nWhen interpolation is not present in the command, all arguments are forwarded to the command by default.\n\nThis is useful when you need to pass raw argument strings to your command.\n\nFor example, when you run:\n\nnx run frontend:webpack --args=\"--config=example.config.js\"\n\n```json\n\"webpack\": {\n \"executor\": \"nx:run-commands\",\n \"options\": {\n \"command\": \"webpack\"\n }\n}\n```\n\nThe above command will execute: `webpack --config=example.config.js`\n\nThis functionality can be disabled by using `commands` and expanding each `command` into an object\nthat sets the `forwardAllArgs` option to `false` as shown below:\n\n```json\n\"webpack\": {\n \"executor\": \"nx:run-commands\",\n \"options\": {\n \"commands\": [\n {\n \"command\": \"webpack\",\n \"forwardAllArgs\": false\n }\n ]\n }\n}\n```\n\n##### Custom **done** conditions\n\nNormally, `run-commands` considers the commands done when all of them have finished running. If you don't need to wait until they're all done, you can set a special string that considers the commands finished the moment the string appears in `stdout` or `stderr`:\n\n```json\n\"finish-when-ready\": {\n \"executor\": \"nx:run-commands\",\n \"options\": {\n \"commands\": [\n \"sleep 5 && echo 'FINISHED'\",\n \"echo 'READY'\"\n ],\n \"readyWhen\": \"READY\",\n \"parallel\": true\n }\n}\n```\n\n```bash\nnx run frontend:finish-when-ready\n```\n\nThe above commands will finish immediately, instead of waiting for 5 seconds.\n\n##### Nx Affected\n\nThe true power of `run-commands` comes from the fact that it runs through `nx`, which knows about your project graph. So you can run **custom commands** only for the projects that have been affected by a change.\n\nWe can create some configurations to generate docs, and if run using `nx affected`, it will only generate documentation for the projects that have been changed:\n\n```bash\nnx affected --target=generate-docs\n```\n\n```json\n//...\n\"frontend\": {\n \"targets\": {\n //...\n \"generate-docs\": {\n \"executor\": \"nx:run-commands\",\n \"options\": {\n \"command\": \"npx compodoc -p apps/frontend/tsconfig.app.json\"\n }\n }\n }\n},\n\"api\": {\n \"targets\": {\n //...\n \"generate-docs\": {\n \"executor\": \"nx:run-commands\",\n \"options\": {\n \"command\": \"npx compodoc -p apps/api/tsconfig.app.json\"\n }\n }\n }\n}\n```\n" }, "description": "Run any custom commands with Nx.", diff --git a/packages/nx/src/executors/run-commands/run-commands.impl.spec.ts b/packages/nx/src/executors/run-commands/run-commands.impl.spec.ts index e962b263a0..51f7c9465b 100644 --- a/packages/nx/src/executors/run-commands/run-commands.impl.spec.ts +++ b/packages/nx/src/executors/run-commands/run-commands.impl.spec.ts @@ -383,4 +383,22 @@ describe('Run Commands', () => { } }); }); + + it('should not fail if __unparsed__ is not given', async () => { + const root = dirSync().name; + + const result = await runCommands( + { + commands: [ + { + command: `echo Hello World`, + }, + ], + cwd: root, + }, + { root } as any + ); + + expect(result).toEqual(expect.objectContaining({ success: true })); + }); }); diff --git a/packages/nx/src/executors/run-commands/run-commands.impl.ts b/packages/nx/src/executors/run-commands/run-commands.impl.ts index aed6030894..b8e9dbc7f6 100644 --- a/packages/nx/src/executors/run-commands/run-commands.impl.ts +++ b/packages/nx/src/executors/run-commands/run-commands.impl.ts @@ -46,7 +46,7 @@ export interface RunCommandsOptions extends Json { args?: string; envFile?: string; outputPath?: string; - __unparsed__: string[]; + __unparsed__?: string[]; } const propKeys = [ @@ -292,9 +292,10 @@ export function interpolateArgsIntoCommand( const regex = /{args\.([^}]+)}/g; return command.replace(regex, (_, group: string) => opts.parsedArgs[group]); } else if (forwardAllArgs) { - return `${command}${ - opts.__unparsed__.length > 0 ? ' ' + opts.__unparsed__.join(' ') : '' - }`; + if (!opts.__unparsed__ || opts.__unparsed__.length === 0) { + return command; + } + return command + ' ' + opts.__unparsed__.join(' '); } else { return command; } diff --git a/packages/nx/src/executors/run-commands/schema.json b/packages/nx/src/executors/run-commands/schema.json index 53c294e239..d15ad9201b 100644 --- a/packages/nx/src/executors/run-commands/schema.json +++ b/packages/nx/src/executors/run-commands/schema.json @@ -137,6 +137,6 @@ } }, "additionalProperties": true, - "required": ["__unparsed__"], + "required": [], "examplesFile": "../../../docs/run-commands-examples.md" }