execution.md
This file
Path
docs/execution.md
146 lines · 5.0 KiB · Markdown
Last changed by
Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
History of this fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–18why, one step back1<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 syntax18Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 19why, one step back19All [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–68why, one step back2021```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`;5758await execa`npm ${['run', 'build', '--concurrency', 2]}`;59```6061### Multiple lines6263```js64await execa`npm run build65 --concurrency 266 --fail-fast`;67```68Improve documentation of shell escaping (#1096)ehmickybefore adoptionb959030
lines 69–77why, one step back69### Shells7071By 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)7273```js74// This prints `$TASK_NAME`, not `build`75await execa({env: {TASK_NAME: 'build'}})`echo $TASK_NAME`;76```77Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 78–79why, one step back78## Options79Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 80why, one step back80[Options](api.md#options) can be passed to influence the execution's behavior.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 81–106why, one step back8182### Array syntax8384```js85await execa('npm', ['run', 'build'], {timeout: 5000});86```8788### Template string syntax8990```js91await execa({timeout: 5000})`npm run build`;92```9394### Global/shared options9596```js97const timedExeca = execa({timeout: 5000});9899await timedExeca('npm', ['run', 'build']);100await timedExeca`npm run test`;101```102103## Return value104105### Subprocess106Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 107why, one step back107The subprocess is returned as soon as it is spawned. It is a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with [additional methods and properties](api.md#subprocess).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 108–115why, one step back108109```js110const subprocess = execa`npm run build`;111console.log(subprocess.pid);112```113114### Result115Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 116why, one step back116The subprocess is also a `Promise` that resolves with the [`result`](api.md#result).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 117–123why, one step back117118```js119const {stdout} = await execa`npm run build`;120```121122### Synchronous execution123Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 124why, one step back124[Every method](api.md#methods) can be called synchronously by appending `Sync` to the method's name. The [`result`](api.md#result) is returned without needing to `await`. The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 125–132why, one step back125126```js127import {execaSync} from 'execa';128129const {stdout} = execaSync`npm run build`;130```131132Synchronous 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 133–136why, one step back133- 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).134- 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.135- 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.136- Piping multiple subprocesses: [`subprocess.pipe()`](api.md#subprocesspipefile-arguments-options).Add user guides (#989)ehmickybefore adoptionbe91ecf
line 137why, one step back137- [`subprocess.iterable()`](lines.md#progressive-splitting).Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10
line 138why, one step back138- [IPC](ipc.md): [`sendMessage()`](api.md#sendmessagemessage-sendmessageoptions), [`getOneMessage()`](api.md#getonemessagegetonemessageoptions), [`getEachMessage()`](api.md#geteachmessage), [`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 139–141why, one step back139- [`result.all`](api.md#resultall) is not interleaved.140- [`detached`](api.md#optionsdetached) option.141- 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 142–146why, one step back142143<hr>144145[**Next**: 💬 Escaping/quoting](escaping.md)\146[**Top**: Table of contents](../readme.md#documentation)