execution.md
This file
Path
docs/execution.md
169 lines · 5.7 KiB · Markdown
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 32eb757 point at it.
Upgrade Codecov (#1017)ehmickybefore adoption32eb757
lines 1–57why, 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 syntax1819All [available methods](api.md#methods) can use either the [array syntax](#array-syntax) or the template string syntax, which are equivalent.2021```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`;57Document passing `[]` or `''` to template string syntax (#1113)ehmickybefore adoption97f0e01
lines 58–81why, one step back58await 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}`;Upgrade Codecov (#1017)ehmickybefore adoption32eb757
lines 82–91why, one step back82```8384### Multiple lines8586```js87await execa`npm run build88 --concurrency 289 --fail-fast`;90```91Improve documentation of shell escaping (#1096)ehmickybefore adoptionb959030
lines 92–100why, one step back92### Shells9394By 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```100Upgrade Codecov (#1017)ehmickybefore adoption32eb757
lines 101–102why, one step back101## Options102Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
line 103why, one step back103[Options](api.md#options-1) can be passed to influence the execution's behavior.Upgrade Codecov (#1017)ehmickybefore adoption32eb757
lines 104–129why, one step back104105### 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### Subprocess129Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 130why, one step back130The 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).Upgrade Codecov (#1017)ehmickybefore adoption32eb757
lines 131–138why, one step back131132```js133const subprocess = execa`npm run build`;134console.log(subprocess.pid);135```136137### Result138Add `nodeChildProcess` to subprocess promises and make the subprocess a normal promise (#1255)Sindre Sorhusbefore adoptionade74bf
line 139why, one step back139The subprocess resolves with the [`result`](api.md#result).Upgrade Codecov (#1017)ehmickybefore adoption32eb757
lines 140–146why, one step back140141```js142const {stdout} = await execa`npm run build`;143```144145### Synchronous execution146Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813
line 147why, one step back147[`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.Upgrade Codecov (#1017)ehmickybefore adoption32eb757
lines 148–160why, one step back148149```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: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).160- [`subprocess.iterable()`](lines.md#progressive-splitting).Fix broken Markdown links (#1119)ehmickybefore adoption8572eb1
line 161why, one step back161- [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.TweaksSindre Sorhusbefore adoption698a1b8
line 162why, one step back162- [`result.all`](api.md#resultall) is not interleaved. Also, with the [`lines`](api.md#optionslines) option, a file descriptor that uses it keeps its own [`stripFinalNewline`](api.md#optionsstripfinalnewline) value in `result.all`.Upgrade Codecov (#1017)ehmickybefore adoption32eb757
line 163why, one step back163- [`detached`](api.md#optionsdetached) option.Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51
line 164why, one step back164- 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, and the `maxBuffer.stdout` value limits the total output of all file descriptors.Upgrade Codecov (#1017)ehmickybefore adoption32eb757
lines 165–169why, one step back165166<hr>167168[**Next**: 💬 Escaping/quoting](escaping.md)\169[**Top**: Table of contents](../readme.md#documentation)