Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 507c1b6

Browse files
techgermFrozenPandaz
authored andcommittedOct 9, 2020
fix(core): add forwardAllArgs option to run-commands builder (#3559)
ISSUES CLOSED: #3335
1 parent 6eedf58 commit 507c1b6

File tree

7 files changed

+240
-17
lines changed

7 files changed

+240
-17
lines changed
 

Diff for: ‎docs/angular/api-workspace/builders/run-commands.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ or simply with:
6666
nx run frontend:create-script --name=example
6767
```
6868

69+
##### Arguments forwarding
70+
71+
When interpolation is not present in the command, all arguments are forwarded to the command by default.
72+
73+
This is useful when you need to pass raw argument strings to your command.
74+
75+
For example, when you run:
76+
77+
nx run frontend:webpack --args="--config=example.config.js"
78+
79+
```json
80+
"webpack": {
81+
"builder": "@nrwl/workspace:run-commands",
82+
"options": {
83+
"command": "webpack"
84+
}
85+
}
86+
```
87+
88+
The above command will execute: `webpack --config=example.config.js`
89+
90+
This functionality can be disabled by using `commands` and expanding each `command` into an object
91+
that sets the `forwardAllArgs` option to `false` as shown below:
92+
93+
```json
94+
"webpack": {
95+
"builder": "@nrwl/workspace:run-commands",
96+
"options": {
97+
"commands": [
98+
{
99+
"command": "webpack",
100+
"forwardAllArgs": false
101+
}
102+
]
103+
}
104+
}
105+
```
106+
69107
##### Custom **done** conditions
70108

71109
Normally, `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 command finished the moment the string appears in `stdout` or `stderr`:
@@ -74,9 +112,7 @@ Normally, `run-commands` considers the commands done when all of them have finis
74112
"finish-when-ready": {
75113
"builder": "@nrwl/workspace:run-commands",
76114
"options": {
77-
"command": [
78-
"echo 'READY' && sleep 5 && echo 'FINISHED'"
79-
],
115+
"command": "echo 'READY' && sleep 5 && echo 'FINISHED'",
80116
"readyWhen": "READY"
81117
}
82118
}

Diff for: ‎docs/node/api-workspace/builders/run-commands.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,44 @@ or simply with:
6767
nx run frontend:create-script --name=example
6868
```
6969

70+
##### Arguments forwarding
71+
72+
When interpolation is not present in the command, all arguments are forwarded to the command by default.
73+
74+
This is useful when you need to pass raw argument strings to your command.
75+
76+
For example, when you run:
77+
78+
nx run frontend:webpack --args="--config=example.config.js"
79+
80+
```json
81+
"webpack": {
82+
"builder": "@nrwl/workspace:run-commands",
83+
"options": {
84+
"command": "webpack"
85+
}
86+
}
87+
```
88+
89+
The above command will execute: `webpack --config=example.config.js`
90+
91+
This functionality can be disabled by using `commands` and expanding each `command` into an object
92+
that sets the `forwardAllArgs` option to `false` as shown below:
93+
94+
```json
95+
"webpack": {
96+
"builder": "@nrwl/workspace:run-commands",
97+
"options": {
98+
"commands": [
99+
{
100+
"command": "webpack",
101+
"forwardAllArgs": false
102+
}
103+
]
104+
}
105+
}
106+
```
107+
70108
##### Custom **done** conditions
71109

72110
Normally, `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 command finished the moment the string appears in `stdout` or `stderr`:
@@ -75,9 +113,7 @@ Normally, `run-commands` considers the commands done when all of them have finis
75113
"finish-when-ready": {
76114
"builder": "@nrwl/workspace:run-commands",
77115
"options": {
78-
"command": [
79-
"echo 'READY' && sleep 5 && echo 'FINISHED'"
80-
],
116+
"command": "echo 'READY' && sleep 5 && echo 'FINISHED'",
81117
"readyWhen": "READY"
82118
}
83119
}

Diff for: ‎docs/react/api-workspace/builders/run-commands.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,44 @@ or simply with:
6767
nx run frontend:create-script --name=example
6868
```
6969

70+
##### Arguments forwarding
71+
72+
When interpolation is not present in the command, all arguments are forwarded to the command by default.
73+
74+
This is useful when you need to pass raw argument strings to your command.
75+
76+
For example, when you run:
77+
78+
nx run frontend:webpack --args="--config=example.config.js"
79+
80+
```json
81+
"webpack": {
82+
"builder": "@nrwl/workspace:run-commands",
83+
"options": {
84+
"command": "webpack"
85+
}
86+
}
87+
```
88+
89+
The above command will execute: `webpack --config=example.config.js`
90+
91+
This functionality can be disabled by using `commands` and expanding each `command` into an object
92+
that sets the `forwardAllArgs` option to `false` as shown below:
93+
94+
```json
95+
"webpack": {
96+
"builder": "@nrwl/workspace:run-commands",
97+
"options": {
98+
"commands": [
99+
{
100+
"command": "webpack",
101+
"forwardAllArgs": false
102+
}
103+
]
104+
}
105+
}
106+
```
107+
70108
##### Custom **done** conditions
71109

72110
Normally, `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 command finished the moment the string appears in `stdout` or `stderr`:
@@ -75,9 +113,7 @@ Normally, `run-commands` considers the commands done when all of them have finis
75113
"finish-when-ready": {
76114
"builder": "@nrwl/workspace:run-commands",
77115
"options": {
78-
"command": [
79-
"echo 'READY' && sleep 5 && echo 'FINISHED'"
80-
],
116+
"command": "echo 'READY' && sleep 5 && echo 'FINISHED'",
81117
"readyWhen": "READY"
82118
}
83119
}

Diff for: ‎packages/workspace/docs/run-commands-examples.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,44 @@ or simply with:
5858
<%= cli %> run frontend:create-script --name=example
5959
```
6060

61+
##### Arguments forwarding
62+
63+
When interpolation is not present in the command, all arguments are forwarded to the command by default.
64+
65+
This is useful when you need to pass raw argument strings to your command.
66+
67+
For example, when you run:
68+
69+
<%= cli %> run frontend:webpack --args="--config=example.config.js"
70+
71+
```json
72+
"webpack": {
73+
"builder": "@nrwl/workspace:run-commands",
74+
"options": {
75+
"command": "webpack"
76+
}
77+
}
78+
```
79+
80+
The above command will execute: `webpack --config=example.config.js`
81+
82+
This functionality can be disabled by using `commands` and expanding each `command` into an object
83+
that sets the `forwardAllArgs` option to `false` as shown below:
84+
85+
```json
86+
"webpack": {
87+
"builder": "@nrwl/workspace:run-commands",
88+
"options": {
89+
"commands": [
90+
{
91+
"command": "webpack",
92+
"forwardAllArgs": false
93+
}
94+
]
95+
}
96+
}
97+
```
98+
6199
##### Custom **done** conditions
62100

63101
Normally, `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 command finished the moment the string appears in `stdout` or `stderr`:
@@ -66,9 +104,7 @@ Normally, `run-commands` considers the commands done when all of them have finis
66104
"finish-when-ready": {
67105
"builder": "@nrwl/workspace:run-commands",
68106
"options": {
69-
"command": [
70-
"echo 'READY' && sleep 5 && echo 'FINISHED'"
71-
],
107+
"command": "echo 'READY' && sleep 5 && echo 'FINISHED'",
72108
"readyWhen": "READY"
73109
}
74110
}

Diff for: ‎packages/workspace/src/builders/run-commands/run-commands.impl.spec.ts

+60
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,66 @@ describe('Command Runner Builder', () => {
9999
});
100100
});
101101

102+
it('should forward args by default when using commands (plural)', async () => {
103+
const exec = spyOn(require('child_process'), 'exec').and.callThrough();
104+
105+
const run = await architect.scheduleBuilder(
106+
'@nrwl/workspace:run-commands',
107+
{
108+
commands: [{ command: 'echo' }],
109+
a: 123,
110+
b: 456,
111+
}
112+
);
113+
114+
await run.result;
115+
116+
expect(exec).toHaveBeenCalledWith('echo --a=123 --b=456', {
117+
maxBuffer: LARGE_BUFFER,
118+
env: { ...process.env },
119+
});
120+
});
121+
122+
it('should forward args when forwardAllArgs is set to true', async () => {
123+
const exec = spyOn(require('child_process'), 'exec').and.callThrough();
124+
125+
const run = await architect.scheduleBuilder(
126+
'@nrwl/workspace:run-commands',
127+
{
128+
commands: [{ command: 'echo', forwardAllArgs: true }],
129+
a: 123,
130+
b: 456,
131+
}
132+
);
133+
134+
await run.result;
135+
136+
expect(exec).toHaveBeenCalledWith('echo --a=123 --b=456', {
137+
maxBuffer: LARGE_BUFFER,
138+
env: { ...process.env },
139+
});
140+
});
141+
142+
it('should not forward args when forwardAllArgs is set to false', async () => {
143+
const exec = spyOn(require('child_process'), 'exec').and.callThrough();
144+
145+
const run = await architect.scheduleBuilder(
146+
'@nrwl/workspace:run-commands',
147+
{
148+
commands: [{ command: 'echo', forwardAllArgs: false }],
149+
a: 123,
150+
b: 456,
151+
}
152+
);
153+
154+
await run.result;
155+
156+
expect(exec).toHaveBeenCalledWith('echo', {
157+
maxBuffer: LARGE_BUFFER,
158+
env: { ...process.env },
159+
});
160+
});
161+
102162
it('should throw when invalid args', async () => {
103163
try {
104164
const run = await architect.scheduleBuilder(

Diff for: ‎packages/workspace/src/builders/run-commands/run-commands.impl.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ function loadEnvVars(path?: string) {
2020

2121
export interface RunCommandsBuilderOptions extends JsonObject {
2222
command: string;
23-
commands: ({ command: string } | string)[];
23+
commands: (
24+
| {
25+
command: string;
26+
forwardAllArgs?: boolean;
27+
}
28+
| string
29+
)[];
2430
color?: boolean;
2531
parallel?: boolean;
2632
readyWhen?: string;
@@ -44,7 +50,10 @@ const propKeys = [
4450

4551
export interface NormalizedRunCommandsBuilderOptions
4652
extends RunCommandsBuilderOptions {
47-
commands: { command: string }[];
53+
commands: {
54+
command: string;
55+
forwardAllArgs?: boolean;
56+
}[];
4857
parsedArgs: { [k: string]: any };
4958
}
5059

@@ -131,7 +140,8 @@ function normalizeOptions(
131140
(options as NormalizedRunCommandsBuilderOptions).commands.forEach((c) => {
132141
c.command = transformCommand(
133142
c.command,
134-
(options as NormalizedRunCommandsBuilderOptions).parsedArgs
143+
(options as NormalizedRunCommandsBuilderOptions).parsedArgs,
144+
c.forwardAllArgs ?? true
135145
);
136146
});
137147
return options as any;
@@ -197,11 +207,15 @@ function processEnv(color: boolean) {
197207
return env;
198208
}
199209

200-
function transformCommand(command: string, args: { [key: string]: string }) {
210+
function transformCommand(
211+
command: string,
212+
args: { [key: string]: string },
213+
forwardAllArgs: boolean
214+
) {
201215
if (command.indexOf('{args.') > -1) {
202216
const regex = /{args\.([^}]+)}/g;
203217
return command.replace(regex, (_, group: string) => args[group]);
204-
} else if (Object.keys(args).length > 0) {
218+
} else if (Object.keys(args).length > 0 && forwardAllArgs) {
205219
const stringifiedArgs = Object.keys(args)
206220
.map((a) => `--${a}=${args[a]}`)
207221
.join(' ');

Diff for: ‎packages/workspace/src/builders/run-commands/schema.json

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"command": {
1414
"type": "string",
1515
"description": "Command to run in child process"
16+
},
17+
"forwardAllArgs": {
18+
"type": "boolean",
19+
"description": "Whether arguments should be forwarded when interpolation is not present",
20+
"default": true
1621
}
1722
},
1823
"additionalProperties": false,

0 commit comments

Comments
 (0)
Please sign in to comment.