scripts.md
This file
Path
docs/scripts.md
848 lines · 14.6 KiB · Markdown
Last changed by
Improve headers in `readme.md` (#967)ehmickybefore adoptiona5dcf42
History of this fileTake it
The walk stopped after 200 commits: lines older than 65742c6 point at it.
Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 1–2why, one step back1# Node.js scripts2Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 3–7why, one step back3With Execa, you can write scripts with Node.js instead of a shell language. [Compared to Bash and zx](#differences-with-bash-and-zx), this is more:4 - [performant](#performance)5 - [cross-platform](#shell): [no shell](../readme.md#shell-syntax) is used, only JavaScript.6 - [secure](#escaping): no shell injection.7 - [simple](#simplicity): minimalistic API, no [globals](#global-variables), no [binary](#main-binary), no [builtin CLI utilities](#builtin-utilities).Improve headers in `readme.md` (#967)ehmickybefore adoptiona5dcf42
line 8why, one step back8 - [featureful](#simplicity): all Execa features are available ([subprocess piping](#piping-stdout-to-another-command), [IPC](#ipc), [transforms](#transforms), [background subprocesses](#background-subprocesses), [cancellation](#cancellation), [local binaries](#local-binaries), [cleanup on exit](../readme.md#optionscleanup), [interleaved output](#interleaved-output), [forceful termination](../readme.md#optionsforcekillafterdelay), etc.).Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 9why, one step back9 - [easy to debug](#debugging): [verbose mode](#verbose-mode), [detailed errors](#errors), [messages and stack traces](#cancellation), stateless API.Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 10–14why, one step back1011```js12import {$} from 'execa';1314const {stdout: name} = await $`cat package.json``.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562
line 15why, one step back15 .pipe`grep name`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 16–21why, one step back16console.log(name);1718const branch = await $`git branch --show-current`;19await $`dep deploy --branch=${branch}`;2021await Promise.all([Fix indentationSindre Sorhusbefore adoptionf3cb72b
lines 22–24why, one step back22 $`sleep 1`,23 $`sleep 2`,24 $`sleep 3`,Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 25–30why, one step back25]);2627const dirName = 'foo bar';28await $`mkdir /tmp/${dirName}`;29```30Allow template strings with all methods (#933)ehmickybefore adoption8074d85
lines 31–45why, one step back31## Template string syntax3233The examples below use the [template string syntax](../readme.md#template-string-syntax). However, the other syntax using [an array of arguments](../readme.md#execafile-arguments-options) is also available as `$(file, arguments?, options?)`.3435Also, the template string syntax can be used outside of script files: `$` is not required to use that syntax. For example, `execa` can use it too.3637The only difference between `$` and `execa` is that the former includes [script-friendly default options](../readme.md#file-arguments-options).3839```js40import {execa, $} from 'execa';4142const branch = await execa`git branch --show-current`;43await $('dep', ['deploy', `--branch=${branch}`]);44```45Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 46why, one step back46## ExamplesValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 47–98why, one step back4748### Main binary4950```sh51# Bash52bash file.sh53```5455```js56// zx57zx file.js5859// or a shebang can be used:60// #!/usr/bin/env zx61```6263```js64// Execa scripts are just regular Node.js files65node file.js66```6768### Global variables6970```js71// zx72await $`echo example`;73```7475```js76// Execa77import {$} from 'execa';7879await $`echo example`;80```8182### Command execution8384```sh85# Bash86echo example87```8889```js90// zx91await $`echo example`;92```9394```js95// Execa96await $`echo example`;97```98Allow newlines in `$` (#843)ehmickybefore adoption1315486
lines 99–103why, one step back99### Multiline commands100101```sh102# Bash103npm run build \Fix indentationSindre Sorhusbefore adoptionf3cb72b
lines 104–105why, one step back104 --example-flag-one \105 --example-flag-twoAllow newlines in `$` (#843)ehmickybefore adoption1315486
lines 106–122why, one step back106```107108```js109// zx110await $`npm run build ${[111 '--example-flag-one',112 '--example-flag-two',113]}`;114```115116```js117// Execa118await $`npm run build119 --example-flag-one120 --example-flag-two`121```122Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 123why, one step back123### ConcatenationValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 124–126why, one step back124125```sh126# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 127–128why, one step back127tmpDir="/tmp"128mkdir "$tmpDir/filename"Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 129–132why, one step back129```130131```js132// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 133–134why, one step back133const tmpDir = '/tmp'134await $`mkdir ${tmpDir}/filename`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 135–138why, one step back135```136137```js138// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 139–140why, one step back139const tmpDir = '/tmp'140await $`mkdir ${tmpDir}/filename`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 141–142why, one step back141```142Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 143why, one step back143### Variable substitutionValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 144–146why, one step back144145```sh146# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 147why, one step back147echo $LANGValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 148–151why, one step back148```149150```js151// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 152why, one step back152await $`echo $LANG`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 153–156why, one step back153```154155```js156// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 157why, one step back157await $`echo ${process.env.LANG}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 158–159why, one step back158```159Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 160why, one step back160### EscapingValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 161–163why, one step back161162```sh163# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 164why, one step back164echo 'one two'Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 165–168why, one step back165```166167```js168// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 169why, one step back169await $`echo ${'one two'}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 170–173why, one step back170```171172```js173// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 174why, one step back174await $`echo ${'one two'}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 175–176why, one step back175```176Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 177why, one step back177### Escaping multiple argumentsValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 178–180why, one step back178179```sh180# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 181why, one step back181echo 'one two' '$'Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 182–185why, one step back182```183184```js185// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 186why, one step back186await $`echo ${['one two', '$']}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 187–190why, one step back187```188189```js190// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 191why, one step back191await $`echo ${['one two', '$']}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 192–193why, one step back192```193Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 194why, one step back194### SubcommandsValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 195–197why, one step back195196```sh197# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 198why, one step back198echo "$(echo example)"Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 199–202why, one step back199```200201```js202// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 203–204why, one step back203const example = await $`echo example`;204await $`echo ${example}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 205–208why, one step back205```206207```js208// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 209–210why, one step back209const example = await $`echo example`;210await $`echo ${example}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 211–212why, one step back211```212Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 213why, one step back213### Serial commandsValidate `input` option's type (#668)ehmickybefore adoption65742c6
line 214why, one step back214Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 215–219why, one step back215```sh216# Bash217echo one && echo two218```219Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 220–221why, one step back220```js221// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 222why, one step back222await $`echo one && echo two`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 223–226why, one step back223```224225```js226// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 227–229why, one step back227await $`echo one`;228await $`echo two`;229```Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 230why, one step back230Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 231–236why, one step back231### Parallel commands232233```sh234# Bash235echo one &236echo two &Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 237–238why, one step back237```238Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 239–242why, one step back239```js240// zx241await Promise.all([$`echo one`, $`echo two`]);242```Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 243why, one step back243Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 244–250why, one step back244```js245// Execa246await Promise.all([$`echo one`, $`echo two`]);247```248249### Global/shared options250Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 251–252why, one step back251```sh252# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 253–256why, one step back253options="timeout 5"254$options echo one255$options echo two256$options echo threeValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 257–260why, one step back257```258259```js260// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 261–264why, one step back261const timeout = '5s';262await $`echo one`.timeout(timeout);263await $`echo two`.timeout(timeout);264await $`echo three`.timeout(timeout);Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 265–268why, one step back265```266267```js268// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 269–275why, one step back269import {$ as $_} from 'execa';270271const $ = $_({timeout: 5000});272273await $`echo one`;274await $`echo two`;275await $`echo three`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 276–296why, one step back276```277278### Environment variables279280```sh281# Bash282EXAMPLE=1 example_command283```284285```js286// zx287$.env.EXAMPLE = '1';288await $`example_command`;289delete $.env.EXAMPLE;290```291292```js293// Execa294await $({env: {EXAMPLE: '1'}})`example_command`;295```296Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 297why, one step back297### Local binariesValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 298–300why, one step back298299```sh300# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 301why, one step back301npx tsc --versionValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 302–305why, one step back302```303304```js305// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 306why, one step back306await $`npx tsc --version`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 307–310why, one step back307```308309```js310// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 311why, one step back311await $`tsc --version`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 312–313why, one step back312```313Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 314why, one step back314### Builtin utilitiesValidate `input` option's type (#668)ehmickybefore adoption65742c6
line 315why, one step back315Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 316–329why, one step back316```js317// zx318const content = await stdin();319```320321```js322// Execa323import getStdin from 'get-stdin';324325const content = await getStdin();326```327328### Printing to stdout329Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 330–331why, one step back330```sh331# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 332why, one step back332echo exampleValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 333–336why, one step back333```334335```js336// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 337why, one step back337echo`example`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 338–341why, one step back338```339340```js341// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 342why, one step back342console.log('example');Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 343–344why, one step back343```344Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 345why, one step back345### Silent stderrValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 346–348why, one step back346347```sh348# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 349why, one step back349echo example 2> /dev/nullValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 350–353why, one step back350```351352```js353// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 354why, one step back354await $`echo example`.stdio('inherit', 'pipe', 'ignore');Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 355–357why, one step back355```356357```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 358–359why, one step back358// Execa does not print stdout/stderr by default359await $`echo example`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 360–379why, one step back360```361362### Verbose mode363364```sh365# Bash366set -v367echo example368```369370```js371// zx >=8372await $`echo example`.verbose();373374// or:375$.verbose = true;376```377378```js379// ExecaImprove documentation of global script options (#870)ehmickybefore adoption146fa07
lines 380–381why, one step back380import {$ as $_} from 'execa';381Print output with `verbose` option (#884)ehmickybefore adoption6fda30f
lines 382–383why, one step back382// `verbose: 'short'` is also available383const $ = $_({verbose: 'full'});Improve documentation of global script options (#870)ehmickybefore adoption146fa07
lines 384–385why, one step back384385await $`echo example`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 386–393why, one step back386```387388Or:389390```391NODE_DEBUG=execa node file.js392```393Improve `verbose` option (#883)ehmickybefore adoption7d0943f
lines 394–396why, one step back394Which prints:395396```Print output with `verbose` option (#884)ehmickybefore adoption6fda30f
lines 397–398why, one step back397[19:49:00.360] [0] $ echo example398examplePrint process completion with verbose option (#887)ehmickybefore adoption7264bcf
line 399why, one step back399[19:49:00.383] [0] √ (done in 23ms)Improve `verbose` option (#883)ehmickybefore adoption7d0943f
lines 400–401why, one step back400```401Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 402why, one step back402### Piping stdout to another commandValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 403–405why, one step back403404```sh405# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 406why, one step back406echo npm run build | sort | head -n2Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 407–410why, one step back407```408409```js410// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 411why, one step back411await $`npm run build | sort | head -n2`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 412–415why, one step back412```413414```js415// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 416–418why, one step back416await $`npm run build`417 .pipe`sort`418 .pipe`head -n2`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 419–420why, one step back419```420Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 421why, one step back421### Piping stdout and stderr to another commandValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 422–424why, one step back422423```sh424# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 425why, one step back425echo example |& catValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 426–429why, one step back426```427428```js429// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 430–434why, one step back430const echo = $`echo example`;431const cat = $`cat`;432echo.pipe(cat)433echo.stderr.pipe(cat.stdin);434await Promise.all([echo, cat]);Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 435–438why, one step back435```436437```js438// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 439–440why, one step back439await $({all: true})`echo example`440 .pipe({from: 'all'})`cat`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 441–442why, one step back441```442Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 443why, one step back443### Piping stdout to a fileValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 444–446why, one step back444445```sh446# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 447why, one step back447echo example > file.txtValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 448–451why, one step back448```449450```js451// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 452why, one step back452await $`echo example`.pipe(fs.createWriteStream('file.txt'));Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 453–456why, one step back453```454455```js456// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 457why, one step back457await $({stdout: {file: 'file.txt'}})`echo example`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 458–459why, one step back458```459Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 460why, one step back460### Piping stdin from a fileValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 461–463why, one step back461462```sh463# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 464why, one step back464echo example < file.txtValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 465–468why, one step back465```466467```js468// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 469–471why, one step back469const cat = $`cat`470fs.createReadStream('file.txt').pipe(cat.stdin)471await catValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 472–475why, one step back472```473474```js475// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 476why, one step back476await $({inputFile: 'file.txt'})`cat`Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 477–478why, one step back477```478Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 479–505why, one step back479### Iterate over output lines480481```sh482# Bash483while read484do485 if [[ "$REPLY" == *ERROR* ]]486 then487 echo "$REPLY"488 fi489done < <(npm run build)490```491492```js493// zx does not allow proper iteration.494// For example, the iteration does not handle subprocess errors.495```496497```js498// Execa499for await (const line of $`npm run build`) {500 if (line.includes('ERROR')) {501 console.log(line);502 }503}504```505Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 506–546why, one step back506### Errors507508```sh509# Bash communicates errors only through the exit code and stderr510timeout 1 sleep 2511echo $?512```513514```js515// zx516const {517 stdout,518 stderr,519 exitCode,520 signal,521} = await $`sleep 2`.timeout('1s');522// file:///home/me/Desktop/node_modules/zx/build/core.js:146523// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);524// ^525// ProcessOutput [Error]:526// at file:///home/me/Desktop/example.js:2:20527// exit code: null528// signal: SIGTERM529// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)530// at ChildProcess.emit (node:events:512:28)531// at maybeClose (node:internal/child_process:1098:16)532// at Socket.<anonymous> (node:internal/child_process:456:11)533// at Socket.emit (node:events:512:28)534// at Pipe.<anonymous> (node:net:316:12)535// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {536// _code: null,537// _signal: 'SIGTERM',538// _stdout: '',539// _stderr: '',540// _combined: ''541// }542```543544```js545// Execa546const {Fix indentationSindre Sorhusbefore adoptionf3cb72b
lines 547–559why, one step back547 stdout,548 stderr,549 exitCode,550 signal,551 signalDescription,552 originalMessage,553 shortMessage,554 command,555 escapedCommand,556 failed,557 timedOut,558 isCanceled,559 isTerminated,Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e
line 560why, one step back560 isMaxBuffer,Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 561–562why, one step back561 // and other error-related properties: code, etc.562} = await $({timeout: 1})`sleep 2`;Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 563why, one step back563// ExecaError: Command timed out after 1 milliseconds: sleep 2Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 564why, one step back564// at file:///home/me/Desktop/example.js:2:20Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 565why, one step back565// at ... {Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 566why, one step back566// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 567why, one step back567// originalMessage: '',Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 568–569why, one step back568// command: 'sleep 2',569// escapedCommand: 'sleep 2',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 570–575why, one step back570// cwd: '/path/to/cwd',571// durationMs: 19.95693,572// failed: true,573// timedOut: true,574// isCanceled: false,575// isTerminated: true,Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e
line 576why, one step back576// isMaxBuffer: false,Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 577why, one step back577// signal: 'SIGTERM',Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 578–580why, one step back578// signalDescription: 'Termination',579// stdout: '',580// stderr: '',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 581–582why, one step back581// stdio: [undefined, '', ''],582// pipedFrom: []Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 583–585why, one step back583// }584```585Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 586why, one step back586### Exit codesValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 587–589why, one step back587588```sh589# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 590–591why, one step back590false591echo $?Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 592–595why, one step back592```593594```js595// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 596–597why, one step back596const {exitCode} = await $`false`.nothrow();597echo`${exitCode}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 598–601why, one step back598```599600```js601// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 602–603why, one step back602const {exitCode} = await $({reject: false})`false`;603console.log(exitCode);Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 604–605why, one step back604```605Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 606why, one step back606### TimeoutsValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 607–609why, one step back607608```sh609# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 610why, one step back610timeout 5 echo exampleValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 611–613why, one step back611```612613```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 614–615why, one step back614// zx615await $`echo example`.timeout('5s');Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 616–619why, one step back616```617618```js619// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 620why, one step back620await $({timeout: 5000})`echo example`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 621–622why, one step back621```622Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 623why, one step back623### Current filenameValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 624–626why, one step back624625```sh626# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 627why, one step back627echo "$(basename "$0")"Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 628–631why, one step back628```629630```js631// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 632why, one step back632await $`echo ${__filename}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 633–636why, one step back633```634635```js636// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 637–638why, one step back637import {fileURLToPath} from 'node:url';638import path from 'node:path';Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 639why, one step back639Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 640why, one step back640const __filename = path.basename(fileURLToPath(import.meta.url));Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 641why, one step back641Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 642why, one step back642await $`echo ${__filename}`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 643–644why, one step back643```644Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 645why, one step back645### Current directoryValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 646–648why, one step back646647```sh648# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 649why, one step back649cd projectValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 650–653why, one step back650```651652```js653// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 654–657why, one step back654cd('project');655656// or:657$.cwd = 'project';Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 658–661why, one step back658```659660```js661// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 662why, one step back662const $$ = $({cwd: 'project'});Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 663–664why, one step back663```664Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 665why, one step back665### Multiple current directoriesValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 666–668why, one step back666667```sh668# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 669–672why, one step back669pushd project670pwd671popd672pwdValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 673–676why, one step back673```674675```js676// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 677–682why, one step back677within(async () => {678 cd('project');679 await $`pwd`;680});681682await $`pwd`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 683–686why, one step back683```684685```js686// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 687–688why, one step back687await $({cwd: 'project'})`pwd`;688await $`pwd`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 689–690why, one step back689```690Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 691why, one step back691### Background subprocessesValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 692–694why, one step back692693```sh694# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 695why, one step back695echo one &Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 696–698why, one step back696```697698```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 699why, one step back699// zx does not allow setting the `detached` optionValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 700–703why, one step back700```701702```js703// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 704why, one step back704await $({detached: true})`echo one`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 705–706why, one step back705```706Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 707–718why, one step back707### IPC708709```sh710# Bash does not allow simple IPC711```712713```js714// zx does not allow simple IPC715```716717```js718// ExecaRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 719why, one step back719const subprocess = $({ipc: true})`node script.js`;Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 720why, one step back720Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 721why, one step back721subprocess.on('message', message => {Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 722why, one step back722 if (message === 'ping') {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 723why, one step back723 subprocess.send('pong');Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 724–757why, one step back724 }725});726```727728### Transforms729730```sh731# Bash does not allow transforms732```733734```js735// zx does not allow transforms736```737738```js739// Execa740const transform = function * (line) {741 if (!line.includes('secret')) {742 yield line;743 }744};745746await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;747```748749### Cancellation750751```sh752# Bash753kill $PID754```755756```js757// zxRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 758why, one step back758subprocess.kill();Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 759–763why, one step back759```760761```js762// Execa763// Can specify an error message and stack traceRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 764why, one step back764subprocess.kill(error);Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 765–786why, one step back765766// Or use an `AbortSignal`767const controller = new AbortController();768await $({signal: controller.signal})`node long-script.js`;769```770771### Interleaved output772773```sh774# Bash prints stdout and stderr interleaved775```776777```js778// zx separates stdout and stderr779const {stdout, stderr} = await $`node example.js`;780```781782```js783// Execa can interleave stdout and stderr784const {all} = await $({all: true})`node example.js`;785```786Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 787why, one step back787### PIDValidate `input` option's type (#668)ehmickybefore adoption65742c6
lines 788–790why, one step back788789```sh790# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 791–792why, one step back791echo example &792echo $!Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 793–795why, one step back793```794795```jsRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 796why, one step back796// zx does not return `subprocess.pid`Validate `input` option's type (#668)ehmickybefore adoption65742c6
lines 797–799why, one step back797```798799```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 800–801why, one step back800// Execa801const {pid} = $`echo example`;Validate `input` option's type (#668)ehmickybefore adoption65742c6
line 802why, one step back802```Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 803–819why, one step back803804## Differences with Bash and zx805806This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).807808### Flexibility809810Unlike shell languages like Bash, libraries like Execa and zx enable you to write scripts with a more featureful programming language (JavaScript). This allows complex logic (such as [parallel execution](#parallel-commands)) to be expressed easily. This also lets you use [any Node.js package](#builtin-utilities).811812### Shell813814The main difference between Execa and zx is that Execa does not require any shell. Shell-specific keywords and features are [written in JavaScript](#variable-substitution) instead.815816This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.817818Also, there is no shell syntax to remember: everything is just plain JavaScript.819Improve headers in `readme.md` (#967)ehmickybefore adoptiona5dcf42
line 820why, one step back820If you really need a shell though, the [`shell`](../readme.md#optionsshell) option can be used.Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 821–823why, one step back821822### Simplicity823Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 824why, one step back824Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#file-arguments-options) and [`$(options)`](../readme.md#execaoptions).Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 825–827why, one step back825826[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.827Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 828why, one step back828Execa is a thin wrapper around the core Node.js [`child_process` module](https://nodejs.org/api/child_process.html). Unlike zx, it lets you use [any of its native features](#background-subprocesses): [`pid`](#pid), [IPC](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback), [`unref()`](https://nodejs.org/api/child_process.html#subprocessunref), [`detached`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`uid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`gid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`cancelSignal`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), etc.Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 829–843why, one step back829830### Modularity831832zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.833834Execa does not include [any utility](#builtin-utilities): it focuses on being small and modular instead. Any Node.js package can be used in your scripts.835836### Performance837838Spawning a shell for every command comes at a performance cost, which Execa avoids.839840Also, [local binaries](#local-binaries) can be directly executed without using `npx`.841842### Debugging843Improve headers in `readme.md` (#967)ehmickybefore adoptiona5dcf42
line 844why, one step back844Subprocesses can be hard to debug, which is why Execa includes a [`verbose`](#verbose-mode) option.Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 845why, one step back845Improve headers in `readme.md` (#967)ehmickybefore adoptiona5dcf42
line 846why, one step back846Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a subprocess failed. Error messages and stack traces can be set with [`subprocess.kill(error)`](../readme.md#subprocesskillerror).Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 847–848why, one step back847848Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.