Skip to content
Myr

execution.md

This file

Path

docs/execution.md

169 lines · 5.4 KiB · Markdown

Last changed by

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–18why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# ▶️ Basic execution89## Array syntax1011```js12import {execa} from 'execa';1314await execa('npm', ['run', 'build']);15```1617## Template string syntax18

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 19why, one step back
19All [available methods](api.md#methods) can use either the [array syntax](#array-syntax) or the template string syntax, which are equivalent.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 20–57why, one step back
20
21```js22await execa`npm run build`;23```2425### String argument2627```js28await execa`npm run ${'task with space'}`;29```3031### Number argument3233```js34await execa`npm run build --concurrency ${2}`;35```3637### Subcommands3839```js40const result = await execa`get-concurrency`;4142// Uses `result.stdout`43await execa`npm run build --concurrency ${result}`;44```4546### Concatenation4748```js49const tmpDirectory = '/tmp';50await execa`mkdir ${tmpDirectory}/filename`;51```5253### Multiple arguments5455```js56const result = await execa`get-concurrency`;57

Document passing `[]` or `''` to template string syntax (#1113)ehmickybefore adoption97f0e01

lines 58–81why, one step back
58await execa`npm ${['run', 'build', '--concurrency', result]}`;
59```6061### No arguments6263```js64await execa`npm run build ${[]}`;65// Same as:66await execa('npm', ['run', 'build']);67```6869### Empty string argument7071```js72await execa`npm run build ${''}`;73// Same as:74await execa('npm', ['run', 'build', '']);75```7677### Conditional argument7879```js80const commandArguments = failFast ? ['--fail-fast'] : [];81await execa`npm run build ${commandArguments}`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 82–91why, one step back
82```
8384### Multiple lines8586```js87await execa`npm run build88 --concurrency 289 --fail-fast`;90```91

Improve documentation of shell escaping (#1096)ehmickybefore adoptionb959030

lines 92–100why, one step back
92### Shells
9394By default, any shell-specific syntax has no special meaning and does not need to be escaped. This prevents [shell injections](https://en.wikipedia.org/wiki/Code_injection#Shell_injection). [More info.](escaping.md#shells)9596```js97// This prints `$TASK_NAME`, not `build`98await execa({env: {TASK_NAME: 'build'}})`echo $TASK_NAME`;99```100

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 101–102why, one step back
101## Options
102

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

line 103why, one step back
103[Options](api.md#options-1) can be passed to influence the execution's behavior.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 104–129why, one step back
104
105### Array syntax106107```js108await execa('npm', ['run', 'build'], {timeout: 5000});109```110111### Template string syntax112113```js114await execa({timeout: 5000})`npm run build`;115```116117### Global/shared options118119```js120const timedExeca = execa({timeout: 5000});121122await timedExeca('npm', ['run', 'build']);123await timedExeca`npm run test`;124```125126## Return value127128### Subprocess129

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 130why, one step back
130The subprocess is returned as soon as it is spawned. It is a `Promise` with [Execa-specific methods and properties](api.md#subprocess). The underlying Node.js [`ChildProcess`](https://nodejs.org/api/child_process.html#class-childprocess) is available as [`subprocess.nodeChildProcess`](api.md#subprocessnodechildprocess).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 131–138why, one step back
131
132```js133const subprocess = execa`npm run build`;134console.log(subprocess.pid);135```136137### Result138

Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf

line 139why, one step back
139The subprocess resolves with the [`result`](api.md#result).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 140–146why, one step back
140
141```js142const {stdout} = await execa`npm run build`;143```144145### Synchronous execution146

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

line 147why, one step back
147[`execaSync()`](api.md#execasyncfile-arguments-options) and [`$.sync()`](api.md#syncfile-arguments-options) return the [`result`](api.md#result) without needing to `await`. The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 148–155why, one step back
148
149```js150import {execaSync} from 'execa';151152const {stdout} = execaSync`npm run build`;153```154155Synchronous execution is generally discouraged as it holds the CPU and prevents parallelization. Also, the following features cannot be used:

Move API reference to its own page (#996)ehmickybefore adoption7cde496

lines 156–159why, one step back
156- Streams: [`subprocess.stdin`](api.md#subprocessstdin), [`subprocess.stdout`](api.md#subprocessstdout), [`subprocess.stderr`](api.md#subprocessstderr), [`subprocess.readable()`](api.md#subprocessreadablereadableoptions), [`subprocess.writable()`](api.md#subprocesswritablewritableoptions), [`subprocess.duplex()`](api.md#subprocessduplexduplexoptions).
157- The [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout), [`stderr`](api.md#optionsstderr) and [`stdio`](api.md#optionsstdio) options cannot be [`'overlapped'`](api.md#optionsstdout), an [async iterable](lines.md#progressive-splitting), an async [transform](transform.md), a [`Duplex`](transform.md#duplextransform-streams), nor a [web stream](streams.md#web-streams). Node.js streams can be passed but only if either they [have a file descriptor](streams.md#file-descriptors), or the [`input`](api.md#optionsinput) option is used.158- Signal termination: [`subprocess.kill()`](api.md#subprocesskillerror), [`subprocess.pid`](api.md#subprocesspid), [`cleanup`](api.md#optionscleanup) option, [`cancelSignal`](api.md#optionscancelsignal) option, [`forceKillAfterDelay`](api.md#optionsforcekillafterdelay) option.159- Piping multiple subprocesses: [`subprocess.pipe()`](api.md#subprocesspipefile-arguments-options).

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 160why, one step back
160- [`subprocess.iterable()`](lines.md#progressive-splitting).

Fix broken Markdown links (#1119)ehmickybefore adoption8572eb1

line 161why, one step back
161- [IPC](ipc.md): [`sendMessage()`](api.md#sendmessagemessage-sendmessageoptions), [`getOneMessage()`](api.md#getonemessagegetonemessageoptions), [`getEachMessage()`](api.md#geteachmessagegeteachmessageoptions), [`result.ipcOutput`](output.md#any-output-type), [`ipc`](api.md#optionsipc) option, [`serialization`](api.md#optionsserialization) option, [`ipcInput`](input.md#any-input-type) option.

Move API reference to its own page (#996)ehmickybefore adoption7cde496

lines 162–164why, one step back
162- [`result.all`](api.md#resultall) is not interleaved.
163- [`detached`](api.md#optionsdetached) option.164- The [`maxBuffer`](api.md#optionsmaxbuffer) option is always measured in bytes, not in characters, [lines](api.md#optionslines) nor [objects](transform.md#object-mode). Also, it ignores transforms and the [`encoding`](api.md#optionsencoding) option.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 165–169why, one step back
165
166<hr>167168[**Next**: 💬 Escaping/quoting](escaping.md)\169[**Top**: Table of contents](../readme.md#documentation)