scripts.md
This file
Path
docs/scripts.md
831 lines · 13.8 KiB · Markdown
Last changed by
Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
History of this fileTake it
The walk stopped after 200 commits: lines older than 640ae39 point at it.
Fix `stdio` option (#634)ehmickybefore adoption640ae39
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).Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
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#cleanup), [interleaved output](#interleaved-output), [forceful termination](../readme.md#forcekillafterdelay), 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.Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 16–30why, one step back16console.log(name);1718const branch = await $`git branch --show-current`;19await $`dep deploy --branch=${branch}`;2021await Promise.all([22 $`sleep 1`,23 $`sleep 2`,24 $`sleep 3`,25]);2627const dirName = 'foo bar';28await $`mkdir /tmp/${dirName}`;29```30Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 31why, one step back31## ExamplesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 32–83why, one step back3233### Main binary3435```sh36# Bash37bash file.sh38```3940```js41// zx42zx file.js4344// or a shebang can be used:45// #!/usr/bin/env zx46```4748```js49// Execa scripts are just regular Node.js files50node file.js51```5253### Global variables5455```js56// zx57await $`echo example`;58```5960```js61// Execa62import {$} from 'execa';6364await $`echo example`;65```6667### Command execution6869```sh70# Bash71echo example72```7374```js75// zx76await $`echo example`;77```7879```js80// Execa81await $`echo example`;82```83Allow newlines in `$` (#843)ehmickybefore adoption1315486
lines 84–107why, one step back84### Multiline commands8586```sh87# Bash88npm run build \89 --example-flag-one \90 --example-flag-two91```9293```js94// zx95await $`npm run build ${[96 '--example-flag-one',97 '--example-flag-two',98]}`;99```100101```js102// Execa103await $`npm run build104 --example-flag-one105 --example-flag-two`106```107Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 108why, one step back108### ConcatenationFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 109–111why, one step back109110```sh111# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 112–113why, one step back112tmpDir="/tmp"113mkdir "$tmpDir/filename"Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 114–117why, one step back114```115116```js117// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 118–119why, one step back118const tmpDir = '/tmp'119await $`mkdir ${tmpDir}/filename`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 120–123why, one step back120```121122```js123// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 124–125why, one step back124const tmpDir = '/tmp'125await $`mkdir ${tmpDir}/filename`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 126–127why, one step back126```127Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 128why, one step back128### Variable substitutionFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 129–131why, one step back129130```sh131# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 132why, one step back132echo $LANGFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 133–136why, one step back133```134135```js136// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 137why, one step back137await $`echo $LANG`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 138–141why, one step back138```139140```js141// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 142why, one step back142await $`echo ${process.env.LANG}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 143–144why, one step back143```144Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 145why, one step back145### EscapingFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 146–148why, one step back146147```sh148# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 149why, one step back149echo 'one two'Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 150–153why, one step back150```151152```js153// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 154why, one step back154await $`echo ${'one two'}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 155–158why, one step back155```156157```js158// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 159why, one step back159await $`echo ${'one two'}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 160–161why, one step back160```161Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 162why, one step back162### Escaping multiple argumentsFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 163–165why, one step back163164```sh165# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 166why, one step back166echo 'one two' '$'Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 167–170why, one step back167```168169```js170// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 171why, one step back171await $`echo ${['one two', '$']}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 172–175why, one step back172```173174```js175// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 176why, one step back176await $`echo ${['one two', '$']}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 177–178why, one step back177```178Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 179why, one step back179### SubcommandsFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 180–182why, one step back180181```sh182# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 183why, one step back183echo "$(echo example)"Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 184–187why, one step back184```185186```js187// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 188–189why, one step back188const example = await $`echo example`;189await $`echo ${example}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 190–193why, one step back190```191192```js193// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 194–195why, one step back194const example = await $`echo example`;195await $`echo ${example}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 196–197why, one step back196```197Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 198why, one step back198### Serial commandsFix `stdio` option (#634)ehmickybefore adoption640ae39
line 199why, one step back199Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 200–204why, one step back200```sh201# Bash202echo one && echo two203```204Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 205–206why, one step back205```js206// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 207why, one step back207await $`echo one && echo two`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 208–211why, one step back208```209210```js211// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 212–214why, one step back212await $`echo one`;213await $`echo two`;214```Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 215why, one step back215Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 216–221why, one step back216### Parallel commands217218```sh219# Bash220echo one &221echo two &Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 222–223why, one step back222```223Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 224–227why, one step back224```js225// zx226await Promise.all([$`echo one`, $`echo two`]);227```Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 228why, one step back228Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 229–235why, one step back229```js230// Execa231await Promise.all([$`echo one`, $`echo two`]);232```233234### Global/shared options235Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 236–237why, one step back236```sh237# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 238–241why, one step back238options="timeout 5"239$options echo one240$options echo two241$options echo threeFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 242–245why, one step back242```243244```js245// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 246–249why, one step back246const timeout = '5s';247await $`echo one`.timeout(timeout);248await $`echo two`.timeout(timeout);249await $`echo three`.timeout(timeout);Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 250–253why, one step back250```251252```js253// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 254–260why, one step back254import {$ as $_} from 'execa';255256const $ = $_({timeout: 5000});257258await $`echo one`;259await $`echo two`;260await $`echo three`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 261–281why, one step back261```262263### Environment variables264265```sh266# Bash267EXAMPLE=1 example_command268```269270```js271// zx272$.env.EXAMPLE = '1';273await $`example_command`;274delete $.env.EXAMPLE;275```276277```js278// Execa279await $({env: {EXAMPLE: '1'}})`example_command`;280```281Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 282why, one step back282### Local binariesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 283–285why, one step back283284```sh285# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 286why, one step back286npx tsc --versionFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 287–290why, one step back287```288289```js290// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 291why, one step back291await $`npx tsc --version`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 292–295why, one step back292```293294```js295// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 296why, one step back296await $`tsc --version`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 297–298why, one step back297```298Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 299why, one step back299### Builtin utilitiesFix `stdio` option (#634)ehmickybefore adoption640ae39
line 300why, one step back300Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 301–314why, one step back301```js302// zx303const content = await stdin();304```305306```js307// Execa308import getStdin from 'get-stdin';309310const content = await getStdin();311```312313### Printing to stdout314Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 315–316why, one step back315```sh316# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 317why, one step back317echo exampleFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 318–321why, one step back318```319320```js321// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 322why, one step back322echo`example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 323–326why, one step back323```324325```js326// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 327why, one step back327console.log('example');Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 328–329why, one step back328```329Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 330why, one step back330### Silent stderrFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 331–333why, one step back331332```sh333# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 334why, one step back334echo example 2> /dev/nullFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 335–338why, one step back335```336337```js338// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 339why, one step back339await $`echo example`.stdio('inherit', 'pipe', 'ignore');Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 340–342why, one step back340```341342```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 343–344why, one step back343// Execa does not print stdout/stderr by default344await $`echo example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 345–364why, one step back345```346347### Verbose mode348349```sh350# Bash351set -v352echo example353```354355```js356// zx >=8357await $`echo example`.verbose();358359// or:360$.verbose = true;361```362363```js364// ExecaImprove documentation of global script options (#870)ehmickybefore adoption146fa07
lines 365–366why, one step back365import {$ as $_} from 'execa';366Print output with `verbose` option (#884)ehmickybefore adoption6fda30f
lines 367–368why, one step back367// `verbose: 'short'` is also available368const $ = $_({verbose: 'full'});Improve documentation of global script options (#870)ehmickybefore adoption146fa07
lines 369–370why, one step back369370await $`echo example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 371–378why, one step back371```372373Or:374375```376NODE_DEBUG=execa node file.js377```378Improve `verbose` option (#883)ehmickybefore adoption7d0943f
lines 379–381why, one step back379Which prints:380381```Print output with `verbose` option (#884)ehmickybefore adoption6fda30f
lines 382–383why, one step back382[19:49:00.360] [0] $ echo example383examplePrint process completion with verbose option (#887)ehmickybefore adoption7264bcf
line 384why, one step back384[19:49:00.383] [0] √ (done in 23ms)Improve `verbose` option (#883)ehmickybefore adoption7d0943f
lines 385–386why, one step back385```386Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 387why, one step back387### Piping stdout to another commandFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 388–390why, one step back388389```sh390# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 391why, one step back391echo npm run build | sort | head -n2Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 392–395why, one step back392```393394```js395// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 396why, one step back396await $`npm run build | sort | head -n2`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 397–400why, one step back397```398399```js400// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 401–403why, one step back401await $`npm run build`402 .pipe`sort`403 .pipe`head -n2`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 404–405why, one step back404```405Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 406why, one step back406### Piping stdout and stderr to another commandFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 407–409why, one step back407408```sh409# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 410why, one step back410echo example |& catFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 411–414why, one step back411```412413```js414// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 415–419why, one step back415const echo = $`echo example`;416const cat = $`cat`;417echo.pipe(cat)418echo.stderr.pipe(cat.stdin);419await Promise.all([echo, cat]);Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 420–423why, one step back420```421422```js423// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 424–425why, one step back424await $({all: true})`echo example`425 .pipe({from: 'all'})`cat`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 426–427why, one step back426```427Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 428why, one step back428### Piping stdout to a fileFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 429–431why, one step back429430```sh431# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 432why, one step back432echo example > file.txtFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 433–436why, one step back433```434435```js436// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 437why, one step back437await $`echo example`.pipe(fs.createWriteStream('file.txt'));Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 438–441why, one step back438```439440```js441// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 442why, one step back442await $({stdout: {file: 'file.txt'}})`echo example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 443–444why, one step back443```444Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 445why, one step back445### Piping stdin from a fileFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 446–448why, one step back446447```sh448# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 449why, one step back449echo example < file.txtFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 450–453why, one step back450```451452```js453// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 454–456why, one step back454const cat = $`cat`455fs.createReadStream('file.txt').pipe(cat.stdin)456await catFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 457–460why, one step back457```458459```js460// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 461why, one step back461await $({inputFile: 'file.txt'})`cat`Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 462–463why, one step back462```463Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9
lines 464–490why, one step back464### Iterate over output lines465466```sh467# Bash468while read469do470 if [[ "$REPLY" == *ERROR* ]]471 then472 echo "$REPLY"473 fi474done < <(npm run build)475```476477```js478// zx does not allow proper iteration.479// For example, the iteration does not handle subprocess errors.480```481482```js483// Execa484for await (const line of $`npm run build`) {485 if (line.includes('ERROR')) {486 console.log(line);487 }488}489```490Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 491–546why, one step back491### Errors492493```sh494# Bash communicates errors only through the exit code and stderr495timeout 1 sleep 2496echo $?497```498499```js500// zx501const {502 stdout,503 stderr,504 exitCode,505 signal,506} = await $`sleep 2`.timeout('1s');507// file:///home/me/Desktop/node_modules/zx/build/core.js:146508// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);509// ^510// ProcessOutput [Error]:511// at file:///home/me/Desktop/example.js:2:20512// exit code: null513// signal: SIGTERM514// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)515// at ChildProcess.emit (node:events:512:28)516// at maybeClose (node:internal/child_process:1098:16)517// at Socket.<anonymous> (node:internal/child_process:456:11)518// at Socket.emit (node:events:512:28)519// at Pipe.<anonymous> (node:net:316:12)520// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {521// _code: null,522// _signal: 'SIGTERM',523// _stdout: '',524// _stderr: '',525// _combined: ''526// }527```528529```js530// Execa531const {532 stdout,533 stderr,534 exitCode,535 signal,536 signalDescription,537 originalMessage,538 shortMessage,539 command,540 escapedCommand,541 failed,542 timedOut,543 isCanceled,544 isTerminated,545 // and other error-related properties: code, etc.546} = await $({timeout: 1})`sleep 2`;Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 547why, one step back547// ExecaError: Command timed out after 1 milliseconds: sleep 2Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 548why, one step back548// at file:///home/me/Desktop/example.js:2:20Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 549why, one step back549// at ... {Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 550why, one step back550// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 551why, one step back551// originalMessage: '',Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 552–553why, one step back552// command: 'sleep 2',553// escapedCommand: 'sleep 2',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 554–560why, one step back554// cwd: '/path/to/cwd',555// durationMs: 19.95693,556// failed: true,557// timedOut: true,558// isCanceled: false,559// isTerminated: true,560// signal: 'SIGTERM',Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 561–563why, one step back561// signalDescription: 'Termination',562// stdout: '',563// stderr: '',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 564–565why, one step back564// stdio: [undefined, '', ''],565// pipedFrom: []Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 566–568why, one step back566// }567```568Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 569why, one step back569### Exit codesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 570–572why, one step back570571```sh572# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 573–574why, one step back573false574echo $?Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 575–578why, one step back575```576577```js578// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 579–580why, one step back579const {exitCode} = await $`false`.nothrow();580echo`${exitCode}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 581–584why, one step back581```582583```js584// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 585–586why, one step back585const {exitCode} = await $({reject: false})`false`;586console.log(exitCode);Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 587–588why, one step back587```588Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 589why, one step back589### TimeoutsFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 590–592why, one step back590591```sh592# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 593why, one step back593timeout 5 echo exampleFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 594–596why, one step back594```595596```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 597–598why, one step back597// zx598await $`echo example`.timeout('5s');Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 599–602why, one step back599```600601```js602// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 603why, one step back603await $({timeout: 5000})`echo example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 604–605why, one step back604```605Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 606why, one step back606### Current filenameFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 607–609why, one step back607608```sh609# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 610why, one step back610echo "$(basename "$0")"Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 611–614why, one step back611```612613```js614// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 615why, one step back615await $`echo ${__filename}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 616–619why, one step back616```617618```js619// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 620–621why, one step back620import {fileURLToPath} from 'node:url';621import path from 'node:path';Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 622why, one step back622Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 623why, one step back623const __filename = path.basename(fileURLToPath(import.meta.url));Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 624why, one step back624Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 625why, one step back625await $`echo ${__filename}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 626–627why, one step back626```627Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 628why, one step back628### Current directoryFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 629–631why, one step back629630```sh631# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 632why, one step back632cd projectFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 633–636why, one step back633```634635```js636// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 637–640why, one step back637cd('project');638639// or:640$.cwd = 'project';Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 641–644why, one step back641```642643```js644// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 645why, one step back645const $$ = $({cwd: 'project'});Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 646–647why, one step back646```647Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 648why, one step back648### Multiple current directoriesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 649–651why, one step back649650```sh651# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 652–655why, one step back652pushd project653pwd654popd655pwdFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 656–659why, one step back656```657658```js659// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 660–665why, one step back660within(async () => {661 cd('project');662 await $`pwd`;663});664665await $`pwd`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 666–669why, one step back666```667668```js669// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 670–671why, one step back670await $({cwd: 'project'})`pwd`;671await $`pwd`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 672–673why, one step back672```673Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 674why, one step back674### Background subprocessesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 675–677why, one step back675676```sh677# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 678why, one step back678echo one &Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 679–681why, one step back679```680681```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 682why, one step back682// zx does not allow setting the `detached` optionFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 683–686why, one step back683```684685```js686// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 687why, one step back687await $({detached: true})`echo one`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 688–689why, one step back688```689Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 690–701why, one step back690### IPC691692```sh693# Bash does not allow simple IPC694```695696```js697// zx does not allow simple IPC698```699700```js701// ExecaRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 702why, one step back702const subprocess = $({ipc: true})`node script.js`;Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 703why, one step back703Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 704why, one step back704subprocess.on('message', message => {Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 705why, one step back705 if (message === 'ping') {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 706why, one step back706 subprocess.send('pong');Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 707–740why, one step back707 }708});709```710711### Transforms712713```sh714# Bash does not allow transforms715```716717```js718// zx does not allow transforms719```720721```js722// Execa723const transform = function * (line) {724 if (!line.includes('secret')) {725 yield line;726 }727};728729await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;730```731732### Cancellation733734```sh735# Bash736kill $PID737```738739```js740// zxRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 741why, one step back741subprocess.kill();Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 742–746why, one step back742```743744```js745// Execa746// Can specify an error message and stack traceRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 747why, one step back747subprocess.kill(error);Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 748–769why, one step back748749// Or use an `AbortSignal`750const controller = new AbortController();751await $({signal: controller.signal})`node long-script.js`;752```753754### Interleaved output755756```sh757# Bash prints stdout and stderr interleaved758```759760```js761// zx separates stdout and stderr762const {stdout, stderr} = await $`node example.js`;763```764765```js766// Execa can interleave stdout and stderr767const {all} = await $({all: true})`node example.js`;768```769Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 770why, one step back770### PIDFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 771–773why, one step back771772```sh773# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 774–775why, one step back774echo example &775echo $!Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 776–778why, one step back776```777778```jsRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 779why, one step back779// zx does not return `subprocess.pid`Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 780–782why, one step back780```781782```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 783–784why, one step back783// Execa784const {pid} = $`echo example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 785why, one step back785```Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 786–810why, one step back786787## Differences with Bash and zx788789This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).790791### Flexibility792793Unlike 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).794795### Shell796797The 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.798799This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.800801Also, there is no shell syntax to remember: everything is just plain JavaScript.802803If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.804805### Simplicity806807Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).808809[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.810Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 811why, one step back811Execa 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 812–826why, one step back812813### Modularity814815zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.816817Execa 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.818819### Performance820821Spawning a shell for every command comes at a performance cost, which Execa avoids.822823Also, [local binaries](#local-binaries) can be directly executed without using `npx`.824825### Debugging826Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 827why, one step back827Subprocesses can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 828why, one step back828Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 829why, one step back829Also, 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#killerror).Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 830–831why, one step back830831Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.