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
Provenance
- Commit
5320fef- Tree
c28962a- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
1e23e91
1 file changed+46 added−92 removed
modified:readme.md+46 added-92 removed
32 unmodified lines33 ```34 35 ## Usage36 37+### Promise interface38+39 ```js40 import {execa} from 'execa';41 42 const {stdout} = await execa('echo', ['unicorns']);2 unmodified lines45 ```46 47 ### Scripts interface48 47-For more information, please see [this page](docs/scripts.md).49+For more information about Execa scripts, please see [this page](docs/scripts.md).50 51 #### Basic52 53 ```js54 import {$} from 'execa';55 54-const {stdout} = await $`echo unicorns`;55-// const {stdout} = await $`echo ${'unicorns'}`;56-// const {stdout} = await $`echo ${['unicorns', 'rainbows']}`;56+const branch = await $`git branch --show-current`57+await $`dep deploy --branch=${branch}`58+```59 60+#### Multiple arguments61+62+```js63+import {$} from 'execa';64+65+const args = ['unicorns', '&', 'rainbows!']66+const {stdout} = await $`echo ${args}`;67 console.log(stdout);59-//=> 'unicorns'68+//=> 'unicorns & rainbows!'69 ```70 71 #### With options72 3 unmodified lines76 await $({stdio: 'inherit'})`echo unicorns`;77 //=> 'unicorns'78 ```79 71-#### With pre-defined options80+#### Shared options81 82 ```js83 import {$} from 'execa';84 85 const $$ = $({stdio: 'inherit'});86 87 await $$`echo unicorns`;88 //=> 'unicorns'80-await $$({shell: true})`echo unicorns && echo rainbows`;81-//=> 'unicorns'82-//=> 'rainbows'83-```89 85-#### Synchronous86-87-```js88-import {$} from 'execa';89-90-const {stdout} = $.sync`echo unicorns`;91-console.log(stdout);92-//=> 'unicorns'93-94-$({stdio: 'inherit'}).sync`echo rainbows`;90+await $$`echo rainbows`;91 //=> 'rainbows'92 ```93 98-#### With results from `$` or `$.sync`99-100-```js101-import {$} from 'execa';102-103-const unicorns = await $`echo unicorns`;104-105-$({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;106-//=> 'unicorns rainbows'107-```108-94 #### Verbose mode95 96 ```sh97 > node file.js6 unmodified lines104 [16:50:03.308] echo rainbows105 rainbows106 ```107 123-### Redirect output to a file108+### Input/output109 110+#### Redirect output to a file111+112 ```js113 import {execa} from 'execa';114 115 // Similar to `echo unicorns > stdout.txt` in Bash5 unmodified lines121 // Similar to `echo unicorns &> stdout.txt` in Bash122 await execa('echo', ['unicorns'], {all:true}).pipeAll('all.txt');123 ```124 138-### Redirect input from a file125+#### Redirect input from a file126 127 ```js128 import {execa} from 'execa';129 2 unmodified lines132 console.log(stdout);133 //=> 'unicorns'134 ```135 149-### Save and pipe output from a child process136+#### Save and pipe output from a child process137 138 ```js139 import {execa} from 'execa';140 2 unmodified lines143 console.log(stdout);144 // Also returns 'unicorns'145 ```146 160-### Pipe multiple processes147+#### Pipe multiple processes148 149 ```js150 import {execa} from 'execa';151 36 unmodified lines188 */189 }190 ```191 205-### Cancelling a spawned process192+### Graceful termination193 207-```js208-import {execa} from 'execa';209-210-const abortController = new AbortController();211-const subprocess = execa('node', [], {signal: abortController.signal});212-213-setTimeout(() => {214- abortController.abort();215-}, 1000);216-217-try {218- await subprocess;219-} catch (error) {220- console.log(subprocess.killed); // true221- console.log(error.isCanceled); // true222-}223-```224-225-### Catching an error with the sync method226-227-```js228-import {execaSync} from 'execa';229-230-try {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-```257-258-### Kill a process259-194 Using SIGTERM, and after 2 seconds, kill it with SIGKILL.195 196 ```js197 const subprocess = execa('node');511 unmodified lines709 710 console.log(await pRetry(run, {retries: 5}));711 ```712 713+### Cancelling a spawned process714+715+```js716+import {execa} from 'execa';717+718+const abortController = new AbortController();719+const subprocess = execa('node', [], {signal: abortController.signal});720+721+setTimeout(() => {722+ abortController.abort();723+}, 1000);724+725+try {726+ await subprocess;727+} catch (error) {728+ console.log(subprocess.killed); // true729+ console.log(error.isCanceled); // true730+}731+```732+733 ### Execute the current package's binary734 735 ```js782-import {getBinPathSync} from 'get-bin-path';736+import {getBinPath} from 'get-bin-path';737 784-const binPath = getBinPathSync();785-const subprocess = execa(binPath);738+const binPath = await getBinPath();739+await execa(binPath);740 ```741 742 `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