scripts.md
This file
Path
docs/scripts.md
846 lines · 14.6 KiB · Markdown
Last changed by
Allow template strings with all methods (#933)ehmickybefore adoption8074d85
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```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## ExamplesFix `stdio` option (#634)ehmickybefore adoption640ae39
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–122why, one step back99### Multiline commands100101```sh102# Bash103npm run build \104 --example-flag-one \105 --example-flag-two106```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### ConcatenationFix `stdio` option (#634)ehmickybefore adoption640ae39
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"Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 141–142why, one step back141```142Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 143why, one step back143### Variable substitutionFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 144–146why, one step back144145```sh146# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 147why, one step back147echo $LANGFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 148–151why, one step back148```149150```js151// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 152why, one step back152await $`echo $LANG`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 158–159why, one step back158```159Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 160why, one step back160### EscapingFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 161–163why, one step back161162```sh163# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 164why, one step back164echo 'one two'Fix `stdio` option (#634)ehmickybefore adoption640ae39
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'}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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'}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 175–176why, one step back175```176Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 177why, one step back177### Escaping multiple argumentsFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 178–180why, one step back178179```sh180# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 181why, one step back181echo 'one two' '$'Fix `stdio` option (#634)ehmickybefore adoption640ae39
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', '$']}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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', '$']}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 192–193why, one step back192```193Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 194why, one step back194### SubcommandsFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 195–197why, one step back195196```sh197# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 198why, one step back198echo "$(echo example)"Fix `stdio` option (#634)ehmickybefore adoption640ae39
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}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 211–212why, one step back211```212Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 213why, one step back213### Serial commandsFix `stdio` option (#634)ehmickybefore adoption640ae39
line 214why, one step back214Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 215–219why, one step back215```sh216# Bash217echo one && echo two218```219Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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```Fix `stdio` option (#634)ehmickybefore adoption640ae39
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 &Fix `stdio` option (#634)ehmickybefore adoption640ae39
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```Fix `stdio` option (#634)ehmickybefore adoption640ae39
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 options250Fix `stdio` option (#634)ehmickybefore adoption640ae39
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 threeFix `stdio` option (#634)ehmickybefore adoption640ae39
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);Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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 binariesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 298–300why, one step back298299```sh300# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 301why, one step back301npx tsc --versionFix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 307–310why, one step back307```308309```js310// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 311why, one step back311await $`tsc --version`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 312–313why, one step back312```313Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 314why, one step back314### Builtin utilitiesFix `stdio` option (#634)ehmickybefore adoption640ae39
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 stdout329Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 330–331why, one step back330```sh331# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 332why, one step back332echo exampleFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 333–336why, one step back333```334335```js336// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 337why, one step back337echo`example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 338–341why, one step back338```339340```js341// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 342why, one step back342console.log('example');Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 343–344why, one step back343```344Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 345why, one step back345### Silent stderrFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 346–348why, one step back346347```sh348# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 349why, one step back349echo example 2> /dev/nullFix `stdio` option (#634)ehmickybefore adoption640ae39
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');Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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 commandFix `stdio` option (#634)ehmickybefore adoption640ae39
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 -n2Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
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 commandFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 422–424why, one step back422423```sh424# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 425why, one step back425echo example |& catFix `stdio` option (#634)ehmickybefore adoption640ae39
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]);Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 441–442why, one step back441```442Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 443why, one step back443### Piping stdout to a fileFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 444–446why, one step back444445```sh446# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 447why, one step back447echo example > file.txtFix `stdio` option (#634)ehmickybefore adoption640ae39
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'));Fix `stdio` option (#634)ehmickybefore adoption640ae39
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`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 458–459why, one step back458```459Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 460why, one step back460### Piping stdin from a fileFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 461–463why, one step back461462```sh463# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 464why, one step back464echo example < file.txtFix `stdio` option (#634)ehmickybefore adoption640ae39
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 catFix `stdio` option (#634)ehmickybefore adoption640ae39
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`Fix `stdio` option (#634)ehmickybefore adoption640ae39
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```505Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 506–561why, 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 {547 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,560 // and other error-related properties: code, etc.561} = await $({timeout: 1})`sleep 2`;Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 562why, one step back562// ExecaError: Command timed out after 1 milliseconds: sleep 2Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 563why, one step back563// at file:///home/me/Desktop/example.js:2:20Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 564why, one step back564// at ... {Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 565why, one step back565// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
line 566why, one step back566// originalMessage: '',Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 567–568why, one step back567// command: 'sleep 2',568// escapedCommand: 'sleep 2',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 569–575why, one step back569// cwd: '/path/to/cwd',570// durationMs: 19.95693,571// failed: true,572// timedOut: true,573// isCanceled: false,574// isTerminated: true,575// signal: 'SIGTERM',Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 576–578why, one step back576// signalDescription: 'Termination',577// stdout: '',578// stderr: '',Add `ExecaError` and `ExecaSyncError` classes (#911)ehmickybefore adoptionfc01815
lines 579–580why, one step back579// stdio: [undefined, '', ''],580// pipedFrom: []Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 581–583why, one step back581// }582```583Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 584why, one step back584### Exit codesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 585–587why, one step back585586```sh587# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 588–589why, one step back588false589echo $?Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 590–593why, one step back590```591592```js593// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 594–595why, one step back594const {exitCode} = await $`false`.nothrow();595echo`${exitCode}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 596–599why, one step back596```597598```js599// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 600–601why, one step back600const {exitCode} = await $({reject: false})`false`;601console.log(exitCode);Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 602–603why, one step back602```603Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 604why, one step back604### TimeoutsFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 605–607why, one step back605606```sh607# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 608why, one step back608timeout 5 echo exampleFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 609–611why, one step back609```610611```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 612–613why, one step back612// zx613await $`echo example`.timeout('5s');Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 614–617why, one step back614```615616```js617// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 618why, one step back618await $({timeout: 5000})`echo example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 619–620why, one step back619```620Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 621why, one step back621### Current filenameFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 622–624why, one step back622623```sh624# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 625why, one step back625echo "$(basename "$0")"Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 626–629why, one step back626```627628```js629// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 630why, one step back630await $`echo ${__filename}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 631–634why, one step back631```632633```js634// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 635–636why, one step back635import {fileURLToPath} from 'node:url';636import path from 'node:path';Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 637why, one step back637Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 638why, one step back638const __filename = path.basename(fileURLToPath(import.meta.url));Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 639why, one step back639Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 640why, one step back640await $`echo ${__filename}`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 641–642why, one step back641```642Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 643why, one step back643### Current directoryFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 644–646why, one step back644645```sh646# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 647why, one step back647cd projectFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 648–651why, one step back648```649650```js651// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 652–655why, one step back652cd('project');653654// or:655$.cwd = 'project';Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 656–659why, one step back656```657658```js659// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 660why, one step back660const $$ = $({cwd: 'project'});Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 661–662why, one step back661```662Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 663why, one step back663### Multiple current directoriesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 664–666why, one step back664665```sh666# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 667–670why, one step back667pushd project668pwd669popd670pwdFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 671–674why, one step back671```672673```js674// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 675–680why, one step back675within(async () => {676 cd('project');677 await $`pwd`;678});679680await $`pwd`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 681–684why, one step back681```682683```js684// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 685–686why, one step back685await $({cwd: 'project'})`pwd`;686await $`pwd`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 687–688why, one step back687```688Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 689why, one step back689### Background subprocessesFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 690–692why, one step back690691```sh692# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 693why, one step back693echo one &Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 694–696why, one step back694```695696```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 697why, one step back697// zx does not allow setting the `detached` optionFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 698–701why, one step back698```699700```js701// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 702why, one step back702await $({detached: true})`echo one`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 703–704why, one step back703```704Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 705–716why, one step back705### IPC706707```sh708# Bash does not allow simple IPC709```710711```js712// zx does not allow simple IPC713```714715```js716// ExecaRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 717why, one step back717const subprocess = $({ipc: true})`node script.js`;Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 718why, one step back718Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 719why, one step back719subprocess.on('message', message => {Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 720why, one step back720 if (message === 'ping') {Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 721why, one step back721 subprocess.send('pong');Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 722–755why, one step back722 }723});724```725726### Transforms727728```sh729# Bash does not allow transforms730```731732```js733// zx does not allow transforms734```735736```js737// Execa738const transform = function * (line) {739 if (!line.includes('secret')) {740 yield line;741 }742};743744await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;745```746747### Cancellation748749```sh750# Bash751kill $PID752```753754```js755// zxRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 756why, one step back756subprocess.kill();Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 757–761why, one step back757```758759```js760// Execa761// Can specify an error message and stack traceRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 762why, one step back762subprocess.kill(error);Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 763–784why, one step back763764// Or use an `AbortSignal`765const controller = new AbortController();766await $({signal: controller.signal})`node long-script.js`;767```768769### Interleaved output770771```sh772# Bash prints stdout and stderr interleaved773```774775```js776// zx separates stdout and stderr777const {stdout, stderr} = await $`node example.js`;778```779780```js781// Execa can interleave stdout and stderr782const {all} = await $({all: true})`node example.js`;783```784Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 785why, one step back785### PIDFix `stdio` option (#634)ehmickybefore adoption640ae39
lines 786–788why, one step back786787```sh788# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 789–790why, one step back789echo example &790echo $!Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 791–793why, one step back791```792793```jsRename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 794why, one step back794// zx does not return `subprocess.pid`Fix `stdio` option (#634)ehmickybefore adoption640ae39
lines 795–797why, one step back795```796797```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 798–799why, one step back798// Execa799const {pid} = $`echo example`;Fix `stdio` option (#634)ehmickybefore adoption640ae39
line 800why, one step back800```Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 801–821why, one step back801802## Differences with Bash and zx803804This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).805806### Flexibility807808Unlike 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).809810### Shell811812The 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.813814This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.815816Also, there is no shell syntax to remember: everything is just plain JavaScript.817818If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.819820### Simplicity821Allow template strings with all methods (#933)ehmickybefore adoption8074d85
line 822why, one step back822Execa'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 823–825why, one step back823824[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.825Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 826why, one step back826Execa 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 827–841why, one step back827828### Modularity829830zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.831832Execa 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.833834### Performance835836Spawning a shell for every command comes at a performance cost, which Execa avoids.837838Also, [local binaries](#local-binaries) can be directly executed without using `npx`.839840### Debugging841Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 842why, one step back842Subprocesses can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
line 843why, one step back843Rename `spawned` and `child process` to `subprocess` (#897)ehmickybefore adoption8fe2343
line 844why, one step back844Also, 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 845–846why, one step back845846Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.