You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Executes a command using `file ...arguments`. `arguments` are specified as an array of strings. Returns a `childProcess`.
508
508
509
-
Think of this as a mix of `child_process.execFile` and `child_process.spawn`.
509
+
Arguments are automatically escaped. They can contain any character, including spaces.
510
+
511
+
This is the preferred method when executing single commands.
510
512
511
513
@param file - The program/script to execute.
512
514
@param arguments - Arguments to pass to `file` on execution.
@@ -700,11 +702,11 @@ export function execaSync(
700
702
): ExecaSyncReturnValue<Buffer>;
701
703
702
704
/**
703
-
Same as `execa()` (including its return value) except both file and arguments are specified in a single `command` string. For example, `execa('echo', ['unicorns'])` is the same as `execaCommand('echo unicorns')`.
705
+
Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a `childProcess`.
704
706
705
-
If the file or an argument contains spaces, they must be escaped with backslashes. This matters especially if `command` is not a constant but a variable, for example with `__dirname` or `process.cwd()`. Except for spaces, no escaping/quoting is needed.
707
+
Arguments are automatically escaped. They can contain any character, but spaces must be escaped with a backslash like `execaCommand('echo has\\ space')`.
706
708
707
-
The `shell` option must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.
709
+
This is the preferred method when executing a user-supplied `command` string, such as in a REPL.
708
710
709
711
@param command - The program/script to execute and its arguments.
Binds options to the `$` API. For example, you can use `$(options)` to create a new `$` instance with specific default options, which are then bound to both the asynchronous `` $`command` `` and synchronous `` $.sync`command` `` APIs.
757
+
Returns a new instance of `$` but with different default `options`. Consecutive calls are merged to previous ones.
756
758
757
-
> **Note:** Consecutive calls to this API will shallow merge the options.
759
+
This can be used to either:
760
+
- Set options for a specific command: `` $(options)`command` ``
Same as `execa()` except both file and arguments are specified in a single tagged template string. For example, `` $`echo unicorns` `` is the same as `execa('echo', ['unicorns'])`.
839
+
Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a `childProcess`.
836
840
837
-
It's important to note that quotes, backslashes, and spaces are automatically escaped and have no special meaning unless the `shell` option is used. This escaping behavior also applies to interpolated expressions such as strings (`` $`echo ${'string'}` ``), arrays of strings (`` $`echo ${['array', 'of strings']}` ``), and so on.
841
+
Arguments are automatically escaped. They can contain any character, but spaces must use `${}` like `` $`echo ${'has space'}` ``.
838
842
839
-
The `shell` option must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.
843
+
This is the preferred method when executing multiple commands in a script file.
840
844
841
-
As a convenience, the result from previous `` $`command` `` or `` $.sync`command` `` calls can be used as template expressions in subsequent commands and `$`/`$.sync` will use the `stdout` value. See the example below `` with results from `$` or `$.sync` `` for more details.
845
+
The `command` string can inject any `${value}` with the following types: string, number, `childProcess` or an array of those types. For example: `` $`echo one ${'two'} ${3} ${['four', 'five']}` ``. For `${childProcess}`, the process's `stdout` is used.
842
846
843
847
@returns An `ExecaChildProcess` that is both:
844
848
- a `Promise` resolving or rejecting with a `childProcessResult`.
@@ -889,10 +893,14 @@ export const $: Execa$;
889
893
/**
890
894
Execute a Node.js script as a child process.
891
895
892
-
Same as `execa('node', [scriptPath, ...arguments], options)` except (like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options)):
893
-
- the current Node version and options are used. This can be overridden using the `nodePath` and `nodeArguments` options.
894
-
- the `shell` option cannot be used
895
-
- an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to [`stdio`](#stdio)
896
+
Arguments are automatically escaped. They can contain any character, including spaces.
897
+
898
+
This is the preferred method when executing Node.js files.
899
+
900
+
Like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options):
901
+
- the current Node version and options are used. This can be overridden using the `nodePath` and `nodeOptions` options.
902
+
- the `shell` option cannot be used
903
+
- an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to `stdio`
896
904
897
905
@param scriptPath - Node.js script to execute.
898
906
@param arguments - Arguments to pass to `scriptPath` on execution.
Copy file name to clipboardexpand all lines: readme.md
+54-42
Original file line number
Diff line number
Diff line change
@@ -204,52 +204,82 @@ setTimeout(() => {
204
204
205
205
## API
206
206
207
-
### execa(file, arguments, options?)
207
+
### Methods
208
208
209
-
Execute a file. Think of this as a mix of [`child_process.execFile()`](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback) and [`child_process.spawn()`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options). This returns a [`childProcess`](#childprocess).
209
+
#### execa(file, arguments?, options?)
210
210
211
-
No escaping/quoting is needed.
211
+
Executes a command using `file ...arguments`. `arguments` are specified as an array of strings. Returns a [`childProcess`](#childprocess).
212
212
213
-
Unless the [`shell`](#shell) option is used, no shell interpreter (Bash, `cmd.exe`, etc.) is used, so shell features such as variables substitution (`echo $PATH`) are not allowed.
213
+
Arguments are [automatically escaped](#shell-syntax). They can contain any character, including spaces.
214
214
215
-
### $\`command\`
215
+
This is the preferred method when executing single commands.
216
216
217
-
Same as [`execa()`](#execafile-arguments-options) (including its [return value](#childprocess)) except both file and arguments are specified in a single tagged template string. For example, `` $`echo unicorns` `` is the same as `execa('echo', ['unicorns'])`.
217
+
#### execaNode(scriptPath, arguments?, options?)
218
218
219
-
It's important to note that quotes, backslashes, and spaces are automatically escaped and have no special meaning unless the [`shell` option](#shell) is used. This escaping behavior also applies to interpolated expressions such as strings (`` $`echo ${'string'}` ``), arrays of strings (`` $`echo ${['array', 'of strings']}` ``), and so on.
219
+
Executes a Node.js file using `node scriptPath ...arguments`. `arguments` are specified as an array of strings. Returns a [`childProcess`](#childprocess).
220
220
221
-
The [`shell` option](#shell) must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.
221
+
Arguments are [automatically escaped](#shell-syntax). They can contain any character, including spaces.
222
222
223
-
As a convenience, the result from previous [`` $`command` ``](#command) or [`` $.sync`command` ``](#synccommand) calls can be used as template expressions in subsequent commands and `$`/`$.sync` will use the `stdout` value. See the example above [with results from `$` or `$.sync`](#with-results-from--or-sync) for more details.
223
+
This is the preferred method when executing Node.js files.
224
224
225
-
For more information, please see [this page](docs/scripts.md).
225
+
Like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options):
226
+
- the current Node version and options are used. This can be overridden using the [`nodePath`](#nodepath-for-node-only) and [`nodeOptions`](#nodeoptions-for-node-only) options.
227
+
- the [`shell`](#shell) option cannot be used
228
+
- an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to [`stdio`](#stdio)
226
229
227
-
###$(options)
230
+
#### $\`command\`
228
231
229
-
Binds options to the [`$`](#command) API. For example, you can use `$(options)` to create a new `$` instance with specific default options, which are then bound to both the asynchronous [`` $`command` ``](#command) and synchronous[`` $.sync`command` ``](#synccommand) APIs.
232
+
Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a[`childProcess`](#childprocess).
230
233
231
-
> **Note:** Consecutive calls to this API will shallow merge the options.
234
+
Arguments are [automatically escaped](#shell-syntax). They can contain any character, but spaces must use `${}` like `` $`echo ${'has space'}` ``.
232
235
233
-
### execaCommand(command, options?)
236
+
This is the preferred method when executing multiple commands in a script file.
234
237
235
-
Same as [`execa()`](#execafile-arguments-options) (including its [return value](#childprocess)) except both file and arguments are specified in a single `command` string. For example, `execa('echo', ['unicorns'])` is the same as `execaCommand('echo unicorns')`.
238
+
The `command` string can inject any `${value}` with the following types: string, number, [`childProcess`](#childprocess) or an array of those types. For example: `` $`echo one ${'two'} ${3} ${['four', 'five']}` ``. For `${childProcess}`, the process's `stdout` is used.
236
239
237
-
If the file or an argument contains spaces, they must be escaped with backslashes. This matters especially if `command` is not a constant but a variable, for example with `__dirname` or `process.cwd()`. Except for spaces, no escaping/quoting is needed.
240
+
For more information, please see [this section](#scripts-interface) and [this page](docs/scripts.md).
238
241
239
-
The [`shell` option](#shell) must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.
242
+
#### $(options)
240
243
241
-
### execaNode(scriptPath, arguments?, options?)
244
+
Returns a new instance of [`$`](#command) but with different default `options`. Consecutive calls are merged to previous ones.
242
245
243
-
Execute a Node.js script as a child process.
246
+
This can be used to either:
247
+
- Set options for a specific command: `` $(options)`command` ``
Same as `execa('node', [scriptPath, ...arguments], options)` except (like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options)):
246
-
- the current Node version and options are used. This can be overridden using the [`nodePath`](#nodepath-for-node-only) and [`nodeOptions`](#nodeoptions-for-node-only) options.
247
-
- the [`shell`](#shell) option cannot be used
248
-
- an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to [`stdio`](#stdio)
250
+
#### execaCommand(command, options?)
251
+
252
+
Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a [`childProcess`](#childprocess).
253
+
254
+
Arguments are [automatically escaped](#shell-syntax). They can contain any character, but spaces must be escaped with a backslash like `execaCommand('echo has\\ space')`.
255
+
256
+
This is the preferred method when executing a user-supplied `command` string, such as in a REPL.
257
+
258
+
### execaSync(file, arguments?, options?)
259
+
260
+
Same as [`execa()`](#execacommandcommand-options) but synchronous.
261
+
262
+
Returns or throws a [`childProcessResult`](#childProcessResult).
263
+
264
+
### $.sync\`command\`
265
+
266
+
Same as [$\`command\`](#command) but synchronous.
267
+
268
+
Returns or throws a [`childProcessResult`](#childProcessResult).
269
+
270
+
### execaCommandSync(command, options?)
271
+
272
+
Same as [`execaCommand()`](#execacommand-command-options) but synchronous.
273
+
274
+
Returns or throws a [`childProcessResult`](#childProcessResult).
275
+
276
+
### Shell syntax
277
+
278
+
For all the [methods above](#methods), no shell interpreter (Bash, cmd.exe, etc.) is used unless the [`shell` option](#shell) is set. This means shell-specific characters and expressions (`$variable`, `&&`, `||`, `;`, `|`, etc.) have no special meaning and do not need to be escaped.
249
279
250
280
### childProcess
251
281
252
-
This is both:
282
+
The return value of all [asynchronous methods](#methods) is both:
253
283
- a `Promise` resolving or rejecting with a [`childProcessResult`](#childProcessResult).
254
284
- a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with the following additional methods and properties.
255
285
@@ -299,24 +329,6 @@ Combines both [`pipeStdout()`](#pipestdouttarget) and [`pipeStderr()`](#pipestde
299
329
300
330
Either the [`stdout` option](#stdout-1) or the [`stderr` option](#stderr-1) must be kept as `pipe`, their default value. Also, the [`all` option](#all-2) must be set to `true`.
301
331
302
-
### execaSync(file, arguments?, options?)
303
-
304
-
Same as [`execa()`](#execacommandcommand-options) but synchronous.
305
-
306
-
Returns or throws a [`childProcessResult`](#childProcessResult).
307
-
308
-
### $.sync\`command\`
309
-
310
-
Same as [$\`command\`](#command) but synchronous.
311
-
312
-
Returns or throws a [`childProcessResult`](#childProcessResult).
313
-
314
-
### execaCommandSync(command, options?)
315
-
316
-
Same as [`execaCommand()`](#execacommand-command-options) but synchronous.
317
-
318
-
Returns or throws a [`childProcessResult`](#childProcessResult).
0 commit comments