Skip to content
Myr

Improve documentation (#1003)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 63de206 · Browse files · 4 files+67 added−64 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+67 added−64 removed

  • other:+67 added−64 removed

4 files+67 added−64 removed

modified:docs/bash.md+61 added-58 removed
80 unmodified lines
81### Global variables8283```js84// zx85await $`echo example`;85await $`npm run build`;86```8788```js89// Execa90import {$} from 'execa';9192await $`echo example`;92await $`npm run build`;93```9495[More info.](execution.md)9697### Command execution9899```sh100# Bash101echo example101npm run build102```103104```js105// zx106await $`echo example`;106await $`npm run build`;107```108109```js110// Execa111await $`echo example`;111await $`npm run build`;112```113114### Multiline commands115
103 unmodified lines
219### Subcommands220221```sh222# Bash223echo "$(echo example)"223echo "$(npm run build)"224```225226```js227// zx228const example = await $`echo example`;229await $`echo ${example}`;228const result = await $`npm run build`;229await $`echo ${result}`;230```231232```js233// Execa234const example = await $`echo example`;235await $`echo ${example}`;234const result = await $`npm run build`;235await $`echo ${result}`;236```237238[More info.](execution.md#subcommands)239240### Serial commands241242```sh243# Bash244echo one && echo two244npm run build && npm run test245```246247```js248// zx249await $`echo one && echo two`;249await $`npm run build && npm run test`;250```251252```js253// Execa254await $`echo one`;255await $`echo two`;254await $`npm run build`;255await $`npm run test`;256```257258### Parallel commands259260```sh261# Bash262echo one &263echo two &262npm run build &263npm run test &264```265266```js267// zx268await Promise.all([$`echo one`, $`echo two`]);268await Promise.all([$`npm run build`, $`npm run test`]);269```270271```js272// Execa273await Promise.all([$`echo one`, $`echo two`]);273await Promise.all([$`npm run build`, $`npm run test`]);274```275276### Global/shared options277278```sh279# Bash280options="timeout 5"281$options echo one282$options echo two283$options echo three281$options npm run init282$options npm run build283$options npm run test284```285286```js287// zx288const timeout = '5s';289await $`echo one`.timeout(timeout);290await $`echo two`.timeout(timeout);291await $`echo three`.timeout(timeout);289await $`npm run init`.timeout(timeout);290await $`npm run build`.timeout(timeout);291await $`npm run test`.timeout(timeout);292```293294```js295// Execa296import {$ as $_} from 'execa';297298const $ = $_({timeout: 5000});299300await $`echo one`;301await $`echo two`;302await $`echo three`;300await $`npm run init`;301await $`npm run build`;302await $`npm run test`;303```304305[More info.](execution.md#globalshared-options)306307### Environment variables308309```sh310# Bash311EXAMPLE=1 example_command311EXAMPLE=1 npm run build312```313314```js315// zx316$.env.EXAMPLE = '1';317await $`example_command`;317await $`npm run build`;318delete $.env.EXAMPLE;319```320321```js322// Execa323await $({env: {EXAMPLE: '1'}})`example_command`;323await $({env: {EXAMPLE: '1'}})`npm run build`;324```325326[More info.](input.md#environment-variables)327
50 unmodified lines
378### Silent stderr379380```sh381# Bash382echo example 2> /dev/null382npm run build 2> /dev/null383```384385```js386// zx387await $`echo example`.stdio('inherit', 'pipe', 'ignore');387await $`npm run build`.stdio('inherit', 'pipe', 'ignore');388```389390```js391// Execa does not print stdout/stderr by default392await $`echo example`;392await $`npm run build`;393```394395### Verbose mode396397```sh398# Bash399set -v400echo example400npm run build401```402403```js404// zx >=8405await $`echo example`.verbose();405await $`npm run build`.verbose();406407// or:408$.verbose = true;409```410411```js412// Execa413await $({verbose: 'full'})`echo example`;413await $({verbose: 'full'})`npm run build`;414```415416Or:417
3 unmodified lines
421422Which prints:423424```425[19:49:00.360] [0] $ echo example426example425[19:49:00.360] [0] $ npm run build426Building...427Done.428[19:49:00.383] [0] √ (done in 23ms)429```430431[More info.](debugging.md#verbose-mode)
22 unmodified lines
454### Piping stdout and stderr to another command455456```sh457# Bash457echo example |& cat458npm run build |& cat459```460461```js462// zx462const echo = $`echo example`;463const subprocess = $`npm run build`;464const cat = $`cat`;464echo.pipe(cat)465echo.stderr.pipe(cat.stdin);466await Promise.all([echo, cat]);465subprocess.pipe(cat);466subprocess.stderr.pipe(cat.stdin);467await Promise.all([subprocess, cat]);468```469470```js471// Execa471await $({all: true})`echo example`472await $({all: true})`npm run build`473 .pipe({from: 'all'})`cat`;474```475476[More info.](pipe.md#source-file-descriptor)
1 unmodified line
478### Piping stdout to a file479480```sh481# Bash481echo example > output.txt482npm run build > output.txt483```484485```js486// zx487import {createWriteStream} from 'node:fs';488486await $`echo example`.pipe(fs.createWriteStream('output.txt'));489await $`npm run build`.pipe(createWriteStream('output.txt'));490```491492```js493// Execa491await $({stdout: {file: 'output.txt'}})`echo example`;494await $({stdout: {file: 'output.txt'}})`npm run build`;495```496497[More info.](output.md#file-output)498499### Piping stdin from a file500501```sh502# Bash500echo example < input.txt503cat < input.txt504```505506```js507// zx
123 unmodified lines
631### Exit codes632633```sh634# Bash632false635npm run build636echo $?637```638639```js640// zx638const {exitCode} = await $`false`.nothrow();641const {exitCode} = await $`npm run build`.nothrow();642echo`${exitCode}`;643```644645```js646// Execa644const {exitCode} = await $({reject: false})`false`;647const {exitCode} = await $({reject: false})`npm run build`;648console.log(exitCode);649```650651[More info.](errors.md#exit-code)
1 unmodified line
653### Timeouts654655```sh656# Bash654timeout 5 echo example657timeout 5 npm run build658```659660```js661// zx659await $`echo example`.timeout('5s');662await $`npm run build`.timeout('5s');663```664665```js666// Execa664await $({timeout: 5000})`echo example`;667await $({timeout: 5000})`npm run build`;668```669670[More info.](termination.md#timeout)671
67 unmodified lines
739### Background subprocess740741```sh742# Bash740echo one &743npm run build &744```745746```js747// zx does not allow setting the `detached` option748```749750```js751// Execa749await $({detached: true})`echo one`;752await $({detached: true})`npm run build`;753```754755[More info.](environment.md#background-subprocess)756
88 unmodified lines
845### PID846847```sh848# Bash846echo example &849npm run build &850echo $!851```852853```js
1 unmodified line
855```856857```js858// Execa856const {pid} = $`echo example`;859const {pid} = $`npm run build`;860```861862[More info.](termination.md#inter-process-termination)863
5 unmodified lines
modified:docs/input.md+2 added-2 removed
modified:docs/output.md+2 added-2 removed
27 unmodified lines
2829## File output3031```js32await execa({stdout: {file: './output.txt'}})`npm run build`;32await execa({stdout: {file: 'output.txt'}})`npm run build`;33// Or:34await execa({stdout: new URL('file:///path/to/output.txt')})`npm run build`;35```36
20 unmodified lines
5758The following example redirects `stdout` to both the [terminal](#terminal-output) and an `output.txt` [file](#file-output), while also retrieving its value [programmatically](#stdout-and-stderr).5960```js61const {stdout} = await execa({stdout: ['inherit', {file: './output.txt'}, 'pipe']})`npm run build`;61const {stdout} = await execa({stdout: ['inherit', {file: 'output.txt'}, 'pipe']})`npm run build`;62console.log(stdout);63```6465When combining [`'inherit'`](#terminal-output) with other values, please note that the subprocess will not be an interactive TTY, even if the current process is one.
106 unmodified lines
modified:docs/streams.md+2 added-2 removed
13 unmodified lines
14import {createReadStream} from 'node:fs';15import {once} from 'node:events';16import {execa} from 'execa';1718const readable = createReadStream('./input.txt');18const readable = createReadStream('input.txt');19await once(readable, 'open');20await execa({stdin: readable})`npm run scaffold`;21```22
3 unmodified lines
26import {createWriteStream} from 'node:fs';27import {once} from 'node:events';28import {execa} from 'execa';2930const writable = createWriteStream('./output.txt');30const writable = createWriteStream('output.txt');31await once(writable, 'open');32await execa({stdout: writable})`npm run build`;33```34
83 unmodified lines