Skip to content
Myr

scripts.md

This file

Path

docs/scripts.md

718 lines · 11.5 KiB · Markdown

Last changed by

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

History of this file

Take it

The walk stopped after 200 commits: lines older than db2ad9a point at it.

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 1–8why, one step back
1# Node.js scripts
23With Execa, you can write scripts with Node.js instead of a shell language. It is [secure](#escaping), [performant](#performance), [simple](#simplicity) and [cross-platform](#shell).45```js6import {$} from 'execa';78const {stdout: name} = await $`cat package.json`

`.pipe()` shortcut for `$` (#840)ehmickybefore adoption316f562

line 9why, one step back
9 .pipe`grep name`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 10–48why, one step back
10console.log(name);
1112const branch = await $`git branch --show-current`;13await $`dep deploy --branch=${branch}`;1415await Promise.all([16 $`sleep 1`,17 $`sleep 2`,18 $`sleep 3`,19]);2021const dirName = 'foo bar';22await $`mkdir /tmp/${dirName}`;23```2425## Summary2627This file describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).2829### Flexibility3031Unlike 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).3233### Shell3435The 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.3637This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.3839Also, there is no shell syntax to remember: everything is just plain JavaScript.4041If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.4243### Simplicity4445Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).4647[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.48

Rename `signal` option to `cancelSignal` (#880)ehmickybefore adoption231beaf

line 49why, one step back
49Execa 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.

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 50–123why, one step back
50
51### Modularity5253zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.5455Execa 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.5657### Performance5859Spawning a shell for every command comes at a performance cost, which Execa avoids.6061Also, [local binaries](#local-binaries) can be directly executed without using `npx`.6263### Debugging6465Child processes can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).6667Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a process failed.6869Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.7071## Details7273### Main binary7475```sh76# Bash77bash file.sh78```7980```js81// zx82zx file.js8384// or a shebang can be used:85// #!/usr/bin/env zx86```8788```js89// Execa scripts are just regular Node.js files90node file.js91```9293### Global variables9495```js96// zx97await $`echo example`;98```99100```js101// Execa102import {$} from 'execa';103104await $`echo example`;105```106107### Command execution108109```sh110# Bash111echo example112```113114```js115// zx116await $`echo example`;117```118119```js120// Execa121await $`echo example`;122```123

Allow newlines in `$` (#843)ehmickybefore adoption1315486

lines 124–147why, one step back
124### Multiline commands
125126```sh127# Bash128npm run build \129 --example-flag-one \130 --example-flag-two131```132133```js134// zx135await $`npm run build ${[136 '--example-flag-one',137 '--example-flag-two',138]}`;139```140141```js142// Execa143await $`npm run build144 --example-flag-one145 --example-flag-two`146```147

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 148why, one step back
148### Concatenation

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 149–151why, one step back
149
150```sh151# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 152–153why, one step back
152tmpDir="/tmp"
153mkdir "$tmpDir/filename"

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 154–157why, one step back
154```
155156```js157// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 158–159why, one step back
158const tmpDir = '/tmp'
159await $`mkdir ${tmpDir}/filename`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 160–163why, one step back
160```
161162```js163// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 164–165why, one step back
164const tmpDir = '/tmp'
165await $`mkdir ${tmpDir}/filename`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 166–167why, one step back
166```
167

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 168why, one step back
168### Variable substitution

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

lines 169–171why, one step back
169
170```sh171# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 172why, one step back
172echo $LANG

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

lines 173–176why, one step back
173```
174175```js176// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 177why, one step back
177await $`echo $LANG`;

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

lines 178–181why, one step back
178```
179180```js181// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 182why, one step back
182await $`echo ${process.env.LANG}`;

Fix argument concatenation with `$` (#553)ehmickybefore adoption07585d0

lines 183–184why, one step back
183```
184

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 185why, one step back
185### Escaping

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 186–188why, one step back
186
187```sh188# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 189why, one step back
189echo 'one two'

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 190–193why, one step back
190```
191192```js193// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 194why, one step back
194await $`echo ${'one two'}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 195–198why, one step back
195```
196197```js198// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 199why, one step back
199await $`echo ${'one two'}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 200–201why, one step back
200```
201

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 202why, one step back
202### Escaping multiple arguments

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 203–205why, one step back
203
204```sh205# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 206why, one step back
206echo 'one two' '$'

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 207–210why, one step back
207```
208209```js210// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 211why, one step back
211await $`echo ${['one two', '$']}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 212–215why, one step back
212```
213214```js215// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 216why, one step back
216await $`echo ${['one two', '$']}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 217–218why, one step back
217```
218

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 219why, one step back
219### Subcommands

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 220–222why, one step back
220
221```sh222# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 223why, one step back
223echo "$(echo example)"

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 224–227why, one step back
224```
225226```js227// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 228–229why, one step back
228const example = await $`echo example`;
229await $`echo ${example}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 230–233why, one step back
230```
231232```js233// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 234–235why, one step back
234const example = await $`echo example`;
235await $`echo ${example}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 236–237why, one step back
236```
237

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 238why, one step back
238### Serial commands

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

line 239why, one step back
239

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 240–244why, one step back
240```sh
241# Bash242echo one && echo two243```244

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 245–246why, one step back
245```js
246// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 247why, one step back
247await $`echo one && echo two`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 248–251why, one step back
248```
249250```js251// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 252–254why, one step back
252await $`echo one`;
253await $`echo two`;254```

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

line 255why, one step back
255

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 256–261why, one step back
256### Parallel commands
257258```sh259# Bash260echo one &261echo two &

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 262–263why, one step back
262```
263

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 264–267why, one step back
264```js
265// zx266await Promise.all([$`echo one`, $`echo two`]);267```

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

line 268why, one step back
268

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 269–275why, one step back
269```js
270// Execa271await Promise.all([$`echo one`, $`echo two`]);272```273274### Global/shared options275

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 276–277why, one step back
276```sh
277# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 278–281why, one step back
278options="timeout 5"
279$options echo one280$options echo two281$options echo three

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 282–285why, one step back
282```
283284```js285// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 286–289why, one step back
286const timeout = '5s';
287await $`echo one`.timeout(timeout);288await $`echo two`.timeout(timeout);289await $`echo three`.timeout(timeout);

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 290–293why, one step back
290```
291292```js293// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 294–300why, one step back
294import {$ as $_} from 'execa';
295296const $ = $_({timeout: 5000});297298await $`echo one`;299await $`echo two`;300await $`echo three`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 301–321why, one step back
301```
302303### Environment variables304305```sh306# Bash307EXAMPLE=1 example_command308```309310```js311// zx312$.env.EXAMPLE = '1';313await $`example_command`;314delete $.env.EXAMPLE;315```316317```js318// Execa319await $({env: {EXAMPLE: '1'}})`example_command`;320```321

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 322why, one step back
322### Local binaries

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 323–325why, one step back
323
324```sh325# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 326why, one step back
326npx tsc --version

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 327–330why, one step back
327```
328329```js330// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 331why, one step back
331await $`npx tsc --version`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 332–335why, one step back
332```
333334```js335// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 336why, one step back
336await $`tsc --version`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 337–338why, one step back
337```
338

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 339why, one step back
339### Builtin utilities

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

line 340why, one step back
340

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 341–354why, one step back
341```js
342// zx343const content = await stdin();344```345346```js347// Execa348import getStdin from 'get-stdin';349350const content = await getStdin();351```352353### Printing to stdout354

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 355–356why, one step back
355```sh
356# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 357why, one step back
357echo example

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 358–361why, one step back
358```
359360```js361// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 362why, one step back
362echo`example`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 363–366why, one step back
363```
364365```js366// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 367why, one step back
367console.log('example');

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 368–369why, one step back
368```
369

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 370why, one step back
370### Silent stderr

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 371–373why, one step back
371
372```sh373# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 374why, one step back
374echo example 2> /dev/null

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 375–378why, one step back
375```
376377```js378// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 379why, one step back
379await $`echo example`.stdio('inherit', 'pipe', 'ignore');

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 380–382why, one step back
380```
381382```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 383–384why, one step back
383// Execa does not print stdout/stderr by default
384await $`echo example`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 385–404why, one step back
385```
386387### Verbose mode388389```sh390# Bash391set -v392echo example393```394395```js396// zx >=8397await $`echo example`.verbose();398399// or:400$.verbose = true;401```402403```js404// Execa

Improve documentation of global script options (#870)ehmickybefore adoption146fa07

lines 405–406why, one step back
405import {$ as $_} from 'execa';
406

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 407–408why, one step back
407// `verbose: 'short'` is also available
408const $ = $_({verbose: 'full'});

Improve documentation of global script options (#870)ehmickybefore adoption146fa07

lines 409–410why, one step back
409
410await $`echo example`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 411–418why, one step back
411```
412413Or:414415```416NODE_DEBUG=execa node file.js417```418

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

lines 419–421why, one step back
419Which prints:
420421```

Print output with `verbose` option (#884)ehmickybefore adoption6fda30f

lines 422–423why, one step back
422[19:49:00.360] [0] $ echo example
423example

Print process completion with verbose option (#887)ehmickybefore adoption7264bcf

line 424why, one step back
424[19:49:00.383] [0] √ (done in 23ms)

Improve `verbose` option (#883)ehmickybefore adoption7d0943f

lines 425–426why, one step back
425```
426

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 427why, one step back
427### Piping stdout to another command

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 428–430why, one step back
428
429```sh430# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 431why, one step back
431echo npm run build | sort | head -n2

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 432–435why, one step back
432```
433434```js435// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 436why, one step back
436await $`npm run build | sort | head -n2`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 437–440why, one step back
437```
438439```js440// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 441–443why, one step back
441await $`npm run build`
442 .pipe`sort`443 .pipe`head -n2`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 444–445why, one step back
444```
445

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 446why, one step back
446### Piping stdout and stderr to another command

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 447–449why, one step back
447
448```sh449# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 450why, one step back
450echo example |& cat

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 451–454why, one step back
451```
452453```js454// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 455–459why, one step back
455const echo = $`echo example`;
456const cat = $`cat`;457echo.pipe(cat)458echo.stderr.pipe(cat.stdin);459await Promise.all([echo, cat]);

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 460–463why, one step back
460```
461462```js463// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 464–465why, one step back
464await $({all: true})`echo example`
465 .pipe({from: 'all'})`cat`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 466–467why, one step back
466```
467

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 468why, one step back
468### Piping stdout to a file

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 469–471why, one step back
469
470```sh471# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 472why, one step back
472echo example > file.txt

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 473–476why, one step back
473```
474475```js476// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 477why, one step back
477await $`echo example`.pipe(fs.createWriteStream('file.txt'));

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 478–481why, one step back
478```
479480```js481// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 482why, one step back
482await $({stdout: {file: 'file.txt'}})`echo example`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 483–484why, one step back
483```
484

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 485why, one step back
485### Piping stdin from a file

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 486–488why, one step back
486
487```sh488# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 489why, one step back
489echo example < file.txt

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 490–493why, one step back
490```
491492```js493// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 494–496why, one step back
494const cat = $`cat`
495fs.createReadStream('file.txt').pipe(cat.stdin)496await cat

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 497–500why, one step back
497```
498499```js500// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 501why, one step back
501await $({inputFile: 'file.txt'})`cat`

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 502–556why, one step back
502```
503504### Errors505506```sh507# Bash communicates errors only through the exit code and stderr508timeout 1 sleep 2509echo $?510```511512```js513// zx514const {515 stdout,516 stderr,517 exitCode,518 signal,519} = await $`sleep 2`.timeout('1s');520// file:///home/me/Desktop/node_modules/zx/build/core.js:146521// let output = new ProcessOutput(code, signal, stdout, stderr, combined, message);522// ^523// ProcessOutput [Error]:524// at file:///home/me/Desktop/example.js:2:20525// exit code: null526// signal: SIGTERM527// at ChildProcess.<anonymous> (file:///home/me/Desktop/node_modules/zx/build/core.js:146:26)528// at ChildProcess.emit (node:events:512:28)529// at maybeClose (node:internal/child_process:1098:16)530// at Socket.<anonymous> (node:internal/child_process:456:11)531// at Socket.emit (node:events:512:28)532// at Pipe.<anonymous> (node:net:316:12)533// at Pipe.callbackTrampoline (node:internal/async_hooks:130:17) {534// _code: null,535// _signal: 'SIGTERM',536// _stdout: '',537// _stderr: '',538// _combined: ''539// }540```541542```js543// Execa544const {545 stdout,546 stderr,547 exitCode,548 signal,549 signalDescription,550 originalMessage,551 shortMessage,552 command,553 escapedCommand,554 failed,555 timedOut,556 isCanceled,

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

line 557why, one step back
557 isTerminated,

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 558–577why, one step back
558 // and other error-related properties: code, etc.
559} = await $({timeout: 1})`sleep 2`;560// file:///home/me/code/execa/lib/kill.js:60561// reject(Object.assign(new Error('Timed out'), {timedOut: true, signal}));562// ^563// Error: Command timed out after 1 milliseconds: sleep 2564// Timed out565// at file:///home/me/Desktop/example.js:2:20566// timedOut: true,567// signal: 'SIGTERM',568// originalMessage: 'Timed out',569// shortMessage: 'Command timed out after 1 milliseconds: sleep 2\nTimed out',570// command: 'sleep 2',571// escapedCommand: 'sleep 2',572// exitCode: undefined,573// signalDescription: 'Termination',574// stdout: '',575// stderr: '',576// failed: true,577// isCanceled: false,

Fix `error.killed` and rename to `error.isTerminated` (#625)ehmickybefore adoption248349c

line 578why, one step back
578// isTerminated: false

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 579–581why, one step back
579// }
580```581

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 582why, one step back
582### Exit codes

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 583–585why, one step back
583
584```sh585# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 586–587why, one step back
586false
587echo $?

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 588–591why, one step back
588```
589590```js591// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 592–593why, one step back
592const {exitCode} = await $`false`.nothrow();
593echo`${exitCode}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 594–597why, one step back
594```
595596```js597// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 598–599why, one step back
598const {exitCode} = await $({reject: false})`false`;
599console.log(exitCode);

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 600–601why, one step back
600```
601

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 602why, one step back
602### Timeouts

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 603–605why, one step back
603
604```sh605# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 606why, one step back
606timeout 5 echo example

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 607–609why, one step back
607```
608609```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 610–611why, one step back
610// zx
611await $`echo example`.timeout('5s');

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 612–615why, one step back
612```
613614```js615// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 616why, one step back
616await $({timeout: 5000})`echo example`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 617–618why, one step back
617```
618

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 619why, one step back
619### Current filename

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 620–622why, one step back
620
621```sh622# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 623why, one step back
623echo "$(basename "$0")"

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 624–627why, one step back
624```
625626```js627// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 628why, one step back
628await $`echo ${__filename}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 629–632why, one step back
629```
630631```js632// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 633–634why, one step back
633import {fileURLToPath} from 'node:url';
634import path from 'node:path';

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

line 635why, one step back
635

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 636why, one step back
636const __filename = path.basename(fileURLToPath(import.meta.url));

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

line 637why, one step back
637

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 638why, one step back
638await $`echo ${__filename}`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 639–640why, one step back
639```
640

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 641why, one step back
641### Current directory

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 642–644why, one step back
642
643```sh644# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 645why, one step back
645cd project

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 646–649why, one step back
646```
647648```js649// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 650–653why, one step back
650cd('project');
651652// or:653$.cwd = 'project';

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 654–657why, one step back
654```
655656```js657// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 658why, one step back
658const $$ = $({cwd: 'project'});

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 659–660why, one step back
659```
660

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 661why, one step back
661### Multiple current directories

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 662–664why, one step back
662
663```sh664# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 665–668why, one step back
665pushd project
666pwd667popd668pwd

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 669–672why, one step back
669```
670671```js672// zx

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 673–678why, one step back
673within(async () => {
674 cd('project');675 await $`pwd`;676});677678await $`pwd`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 679–682why, one step back
679```
680681```js682// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 683–684why, one step back
683await $({cwd: 'project'})`pwd`;
684await $`pwd`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 685–686why, one step back
685```
686

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 687why, one step back
687### Background processes

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 688–690why, one step back
688
689```sh690# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 691why, one step back
691echo one &

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 692–694why, one step back
692```
693694```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 695why, one step back
695// zx does not allow setting the `detached` option

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 696–699why, one step back
696```
697698```js699// Execa

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 700why, one step back
700await $({detached: true})`echo one`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 701–702why, one step back
701```
702

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 703why, one step back
703### PID

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 704–706why, one step back
704
705```sh706# Bash

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 707–708why, one step back
707echo example &
708echo $!

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 709–711why, one step back
709```
710711```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 712why, one step back
712// zx does not return `childProcess.pid`

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

lines 713–715why, one step back
713```
714715```js

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 716–717why, one step back
716// Execa
717const {pid} = $`echo example`;

Improve main method documentation (#545)ehmickybefore adoptiondb2ad9a

line 718why, one step back
718```