scripts.md
This file
Path
docs/scripts.md
804 lines · 13.5 KiB · Markdown
Last changed by
Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
History of this fileTake it
The walk stopped after 200 commits: lines older than b719fb0 point at it.
Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 1–2why, one step back1# Node.js scripts2Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 3–9why, 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).8 - [featureful](#simplicity): all Execa features are available ([process piping](#piping-stdout-to-another-command), [IPC](#ipc), [transforms](#transforms), [background processes](#background-processes), [cancellation](#cancellation), [local binaries](#local-binaries), [cleanup on exit](../readme.md#cleanup), [interleaved output](#interleaved-output), [forceful termination](../readme.md#forcekillafterdelay), etc.).9 - [easy to debug](#debugging): [verbose mode](#verbose-mode), [detailed errors](#errors), [messages and stack traces](#cancellation), stateless API.Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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## ExamplesAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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### ConcatenationAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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"Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 126–127why, one step back126```127Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 128why, one step back128### Variable substitutionAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 129–131why, one step back129130```sh131# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 132why, one step back132echo $LANGAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 133–136why, one step back133```134135```js136// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 137why, one step back137await $`echo $LANG`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 143–144why, one step back143```144Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 145why, one step back145### EscapingAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 146–148why, one step back146147```sh148# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 149why, one step back149echo 'one two'Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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'}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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'}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 160–161why, one step back160```161Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 162why, one step back162### Escaping multiple argumentsAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 163–165why, one step back163164```sh165# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 166why, one step back166echo 'one two' '$'Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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', '$']}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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', '$']}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 177–178why, one step back177```178Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 179why, one step back179### SubcommandsAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 180–182why, one step back180181```sh182# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 183why, one step back183echo "$(echo example)"Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 196–197why, one step back196```197Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 198why, one step back198### Serial commandsAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
line 199why, one step back199Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 200–204why, one step back200```sh201# Bash202echo one && echo two203```204Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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```Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 &Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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```Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 options235Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 threeAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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);Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 binariesAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 283–285why, one step back283284```sh285# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 286why, one step back286npx tsc --versionAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 292–295why, one step back292```293294```js295// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 296why, one step back296await $`tsc --version`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 297–298why, one step back297```298Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 299why, one step back299### Builtin utilitiesAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 stdout314Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 315–316why, one step back315```sh316# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 317why, one step back317echo exampleAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 318–321why, one step back318```319320```js321// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 322why, one step back322echo`example`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 323–326why, one step back323```324325```js326// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 327why, one step back327console.log('example');Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 328–329why, one step back328```329Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 330why, one step back330### Silent stderrAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 331–333why, one step back331332```sh333# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 334why, one step back334echo example 2> /dev/nullAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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');Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 commandAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 -n2Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 commandAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 407–409why, one step back407408```sh409# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 410why, one step back410echo example |& catAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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]);Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 426–427why, one step back426```427Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 428why, one step back428### Piping stdout to a fileAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 429–431why, one step back429430```sh431# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 432why, one step back432echo example > file.txtAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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'));Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 443–444why, one step back443```444Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 445why, one step back445### Piping stdin from a fileAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 446–448why, one step back446447```sh448# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 449why, one step back449echo example < file.txtAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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 catAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
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`Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 462–516why, one step back462```463464### Errors465466```sh467# Bash communicates errors only through the exit code and stderr468timeout 1 sleep 2469echo $?470```471472```js473// zx474const {475 stdout,476 stderr,477 exitCode,478 signal,479} = await $`sleep 2`.timeout('1s');480// file:///home/me/Desktop/node_modules/zx/build/core.js:146481// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);482// ^483// ProcessOutput [Error]:484// at file:///home/me/Desktop/example.js:2:20485// exit code: null486// signal: SIGTERM487// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)488// at ChildProcess.emit (node:events:512:28)489// at maybeClose (node:internal/child_process:1098:16)490// at Socket.<anonymous> (node:internal/child_process:456:11)491// at Socket.emit (node:events:512:28)492// at Pipe.<anonymous> (node:net:316:12)493// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {494// _code: null,495// _signal: 'SIGTERM',496// _stdout: '',497// _stderr: '',498// _combined: ''499// }500```501502```js503// Execa504const {505 stdout,506 stderr,507 exitCode,508 signal,509 signalDescription,510 originalMessage,511 shortMessage,512 command,513 escapedCommand,514 failed,515 timedOut,516 isCanceled,Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c
line 517why, one step back517 isTerminated,Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 518–537why, one step back518 // and other error-related properties: code, etc.519} = await $({timeout: 1})`sleep 2`;520// file:///home/me/code/execa/lib/kill.js:60521// reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));522// ^523// Error: Command timed out after 1 milliseconds: sleep 2524// Timed out525// at file:///home/me/Desktop/example.js:2:20526// timedOut: true,527// signal: 'SIGTERM',528// originalMessage: 'Timed out',529// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',530// command: 'sleep 2',531// escapedCommand: 'sleep 2',532// exitCode: undefined,533// signalDescription: 'Termination',534// stdout: '',535// stderr: '',536// failed: true,537// isCanceled: false,Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c
line 538why, one step back538// isTerminated: falseAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 539–541why, one step back539// }540```541Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 542why, one step back542### Exit codesAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 543–545why, one step back543544```sh545# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 546–547why, one step back546false547echo $?Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 548–551why, one step back548```549550```js551// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 552–553why, one step back552const {exitCode} = await $`false`.nothrow();553echo`${exitCode}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 554–557why, one step back554```555556```js557// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 558–559why, one step back558const {exitCode} = await $({reject: false})`false`;559console.log(exitCode);Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 560–561why, one step back560```561Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 562why, one step back562### TimeoutsAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 563–565why, one step back563564```sh565# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 566why, one step back566timeout 5 echo exampleAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 567–569why, one step back567```568569```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 570–571why, one step back570// zx571await $`echo example`.timeout('5s');Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 572–575why, one step back572```573574```js575// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 576why, one step back576await $({timeout: 5000})`echo example`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 577–578why, one step back577```578Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 579why, one step back579### Current filenameAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 580–582why, one step back580581```sh582# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 583why, one step back583echo "$(basename "$0")"Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 584–587why, one step back584```585586```js587// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 588why, one step back588await $`echo ${__filename}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 589–592why, one step back589```590591```js592// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 593–594why, one step back593import {fileURLToPath} from 'node:url';594import path from 'node:path';Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
line 595why, one step back595Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 596why, one step back596const __filename = path.basename(fileURLToPath(import.meta.url));Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
line 597why, one step back597Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 598why, one step back598await $`echo ${__filename}`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 599–600why, one step back599```600Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 601why, one step back601### Current directoryAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 602–604why, one step back602603```sh604# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 605why, one step back605cd projectAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 606–609why, one step back606```607608```js609// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 610–613why, one step back610cd('project');611612// or:613$.cwd = 'project';Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 614–617why, one step back614```615616```js617// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 618why, one step back618const $$ = $({cwd: 'project'});Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 619–620why, one step back619```620Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 621why, one step back621### Multiple current directoriesAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 622–624why, one step back622623```sh624# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 625–628why, one step back625pushd project626pwd627popd628pwdAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 629–632why, one step back629```630631```js632// zxReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 633–638why, one step back633within(async () => {634 cd('project');635 await $`pwd`;636});637638await $`pwd`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 639–642why, one step back639```640641```js642// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 643–644why, one step back643await $({cwd: 'project'})`pwd`;644await $`pwd`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 645–646why, one step back645```646Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 647why, one step back647### Background processesAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 648–650why, one step back648649```sh650# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 651why, one step back651echo one &Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 652–654why, one step back652```653654```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 655why, one step back655// zx does not allow setting the `detached` optionAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 656–659why, one step back656```657658```js659// ExecaReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 660why, one step back660await $({detached: true})`echo one`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 661–662why, one step back661```662Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 663–742why, one step back663### IPC664665```sh666# Bash does not allow simple IPC667```668669```js670// zx does not allow simple IPC671```672673```js674// Execa675const childProcess = $({ipc: true})`node script.js`;676677childProcess.on('message', message => {678 if (message === 'ping') {679 childProcess.send('pong');680 }681});682```683684### Transforms685686```sh687# Bash does not allow transforms688```689690```js691// zx does not allow transforms692```693694```js695// Execa696const transform = function * (line) {697 if (!line.includes('secret')) {698 yield line;699 }700};701702await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;703```704705### Cancellation706707```sh708# Bash709kill $PID710```711712```js713// zx714childProcess.kill();715```716717```js718// Execa719// Can specify an error message and stack trace720childProcess.kill(error);721722// Or use an `AbortSignal`723const controller = new AbortController();724await $({signal: controller.signal})`node long-script.js`;725```726727### Interleaved output728729```sh730# Bash prints stdout and stderr interleaved731```732733```js734// zx separates stdout and stderr735const {stdout, stderr} = await $`node example.js`;736```737738```js739// Execa can interleave stdout and stderr740const {all} = await $({all: true})`node example.js`;741```742Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 743why, one step back743### PIDAdd `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 744–746why, one step back744745```sh746# BashReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 747–748why, one step back747echo example &748echo $!Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 749–751why, one step back749```750751```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
line 752why, one step back752// zx does not return `childProcess.pid`Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
lines 753–755why, one step back753```754755```jsReorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef
lines 756–757why, one step back756// Execa757const {pid} = $`echo example`;Add `$.s` as an alias for `$.sync` (#594)ehmickybefore adoptionb719fb0
line 758why, one step back758```Improve documentation of `$` (#888)ehmickybefore adoptiona7f8598
lines 759–804why, one step back759760## Differences with Bash and zx761762This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).763764### Flexibility765766Unlike 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).767768### Shell769770The 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.771772This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.773774Also, there is no shell syntax to remember: everything is just plain JavaScript.775776If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.777778### Simplicity779780Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).781782[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.783784Execa 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-processes): [`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.785786### Modularity787788zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.789790Execa 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.791792### Performance793794Spawning a shell for every command comes at a performance cost, which Execa avoids.795796Also, [local binaries](#local-binaries) can be directly executed without using `npx`.797798### Debugging799800Child processes can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).801802Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a process failed. Error messages and stack traces can be set with [`childProcess.kill(error)`](../readme.md#killerror).803804Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.