Skip to content
Myr

Improve documentation of the usage section (#541)

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

authored by ehmicky · committed by GitHub · parent 1e23e91 · Browse files · 1 file+46 added−92 removed
This commit

State

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

Diff stats

1 file+46 added−92 removed

  • other:+46 added−92 removed

1 file+46 added−92 removed

modified:readme.md+46 added-92 removed
32 unmodified lines
33```3435## Usage3637### Promise interface3839```js40import {execa} from 'execa';4142const {stdout} = await execa('echo', ['unicorns']);
2 unmodified lines
45```4647### Scripts interface4847For more information, please see [this page](docs/scripts.md).49For more information about Execa scripts, please see [this page](docs/scripts.md).5051#### Basic5253```js54import {$} from 'execa';5554const {stdout} = await $`echo unicorns`;55// const {stdout} = await $`echo ${'unicorns'}`;56// const {stdout} = await $`echo ${['unicorns', 'rainbows']}`;56const branch = await $`git branch --show-current`57await $`dep deploy --branch=${branch}`58```5960#### Multiple arguments6162```js63import {$} from 'execa';6465const args = ['unicorns', '&', 'rainbows!']66const {stdout} = await $`echo ${args}`;67console.log(stdout);59//=> 'unicorns'68//=> 'unicorns & rainbows!'69```7071#### With options72
3 unmodified lines
76await $({stdio: 'inherit'})`echo unicorns`;77//=> 'unicorns'78```7971#### With pre-defined options80#### Shared options8182```js83import {$} from 'execa';8485const $$ = $({stdio: 'inherit'});8687await $$`echo unicorns`;88//=> 'unicorns'80await $$({shell: true})`echo unicorns && echo rainbows`;81//=> 'unicorns'82//=> 'rainbows'83```8985#### Synchronous8687```js88import {$} from 'execa';8990const {stdout} = $.sync`echo unicorns`;91console.log(stdout);92//=> 'unicorns'9394$({stdio: 'inherit'}).sync`echo rainbows`;90await $$`echo rainbows`;91//=> 'rainbows'92```9398#### With results from `$` or `$.sync`99100```js101import {$} from 'execa';102103const unicorns = await $`echo unicorns`;104105$({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;106//=> 'unicorns rainbows'107```10894#### Verbose mode9596```sh97> node file.js
6 unmodified lines
104[16:50:03.308] echo rainbows105rainbows106```107123### Redirect output to a file108### Input/output109110#### Redirect output to a file111112```js113import {execa} from 'execa';114115// Similar to `echo unicorns > stdout.txt` in Bash
5 unmodified lines
121// Similar to `echo unicorns &> stdout.txt` in Bash122await execa('echo', ['unicorns'], {all:true}).pipeAll('all.txt');123```124138### Redirect input from a file125#### Redirect input from a file126127```js128import {execa} from 'execa';129
2 unmodified lines
132console.log(stdout);133//=> 'unicorns'134```135149### Save and pipe output from a child process136#### Save and pipe output from a child process137138```js139import {execa} from 'execa';140
2 unmodified lines
143console.log(stdout);144// Also returns 'unicorns'145```146160### Pipe multiple processes147#### Pipe multiple processes148149```js150import {execa} from 'execa';151
36 unmodified lines
188 */189}190```191205### Cancelling a spawned process192### Graceful termination193207```js208import {execa} from 'execa';209210const abortController = new AbortController();211const subprocess = execa('node', [], {signal: abortController.signal});212213setTimeout(() => {214 abortController.abort();215}, 1000);216217try {218 await subprocess;219} catch (error) {220 console.log(subprocess.killed); // true221 console.log(error.isCanceled); // true222}223```224225### Catching an error with the sync method226227```js228import {execaSync} from 'execa';229230try {231 execaSync('unknown', ['command']);232} catch (error) {233 console.log(error);234 /*235 {236 message: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',237 errno: -2,238 code: 'ENOENT',239 syscall: 'spawnSync unknown',240 path: 'unknown',241 spawnargs: ['command'],242 originalMessage: 'spawnSync unknown ENOENT',243 shortMessage: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',244 command: 'unknown command',245 escapedCommand: 'unknown command',246 stdout: '',247 stderr: '',248 all: '',249 failed: true,250 timedOut: false,251 isCanceled: false,252 killed: false253 }254 */255}256```257258### Kill a process259194Using SIGTERM, and after 2 seconds, kill it with SIGKILL.195196```js197const subprocess = execa('node');
511 unmodified lines
709710console.log(await pRetry(run, {retries: 5}));711```712713### Cancelling a spawned process714715```js716import {execa} from 'execa';717718const abortController = new AbortController();719const subprocess = execa('node', [], {signal: abortController.signal});720721setTimeout(() => {722 abortController.abort();723}, 1000);724725try {726 await subprocess;727} catch (error) {728 console.log(subprocess.killed); // true729 console.log(error.isCanceled); // true730}731```732733### Execute the current package's binary734735```js782import {getBinPathSync} from 'get-bin-path';736import {getBinPath} from 'get-bin-path';737784const binPath = getBinPathSync();785const subprocess = execa(binPath);738const binPath = await getBinPath();739await execa(binPath);740```741742`execa` can be combined with [`get-bin-path`](https://github.com/ehmicky/get-bin-path) to test the current package's binary. As opposed to hard-coding the path to the binary, this validates that the `package.json` `bin` field is correctly set up.743
22 unmodified lines