execution.md
This file
Path
docs/execution.md
137 lines · 4.7 KiB · Markdown
Last changed by
Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
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–70why, 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```6869## Options70Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 71why, one step back71[Options](api.md#options) can be passed to influence the execution's behavior.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 72–97why, one step back7273### Array syntax7475```js76await execa('npm', ['run', 'build'], {timeout: 5000});77```7879### Template string syntax8081```js82await execa({timeout: 5000})`npm run build`;83```8485### Global/shared options8687```js88const timedExeca = execa({timeout: 5000});8990await timedExeca('npm', ['run', 'build']);91await timedExeca`npm run test`;92```9394## Return value9596### Subprocess97Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 98why, one step back98The 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 99–106why, one step back99100```js101const subprocess = execa`npm run build`;102console.log(subprocess.pid);103```104105### Result106Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 107why, one step back107The subprocess is also a `Promise` that resolves with the [`result`](api.md#result).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 108–114why, one step back108109```js110const {stdout} = await execa`npm run build`;111```112113### Synchronous execution114Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 115why, one step back115[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 116–123why, one step back116117```js118import {execaSync} from 'execa';119120const {stdout} = execaSync`npm run build`;121```122123Synchronous 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 124–127why, one step back124- 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).125- 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.126- 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.127- Piping multiple subprocesses: [`subprocess.pipe()`](api.md#subprocesspipefile-arguments-options).Add user guides (#989)ehmickybefore adoptionbe91ecf
line 128why, one step back128- [`subprocess.iterable()`](lines.md#progressive-splitting).Add `exchangeMessage()` method (#1077)ehmickybefore adoption7a0a1c6
line 129why, one step back129- [IPC](ipc.md): [`sendMessage()`](api.md#sendmessagemessage), [`getOneMessage()`](api.md#getonemessagegetonemessageoptions), [`exchangeMessage()`](api.md#exchangemessagemessage-getonemessageoptions), [`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 130–132why, one step back130- [`result.all`](api.md#resultall) is not interleaved.131- [`detached`](api.md#optionsdetached) option.132- 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 133–137why, one step back133134<hr>135136[**Next**: 💬 Escaping/quoting](escaping.md)\137[**Top**: Table of contents](../readme.md#documentation)