Improve examples in types documentation (#546)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 0726126 · Browse files · 2 files·+207 added−107 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+207 added−107 removed
- other:+207 added−107 removed
Provenance
- Commit
72443f3- Tree
a8b9299- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0726126
2 files changed+207 added−107 removed
modified:index.d.ts+207 added-106 removed
513 unmodified lines514 - a `Promise` resolving or rejecting with a `childProcessResult`.515 - a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with some additional methods and properties.516 @throws A `childProcessResult` error517 518-@example518+@example <caption>Promise interface</caption>519 ```520 import {execa} from 'execa';521 522 const {stdout} = await execa('echo', ['unicorns']);523 console.log(stdout);524 //=> 'unicorns'525+```526 526-// Cancelling a spawned process527+@example <caption>Redirect output to a file</caption>528+```529+import {execa} from 'execa';530 528-const subprocess = execa('node');531+// Similar to `echo unicorns > stdout.txt` in Bash532+await execa('echo', ['unicorns']).pipeStdout('stdout.txt');533 530-setTimeout(() => {531- subprocess.cancel()532-}, 1000);534+// Similar to `echo unicorns 2> stdout.txt` in Bash535+await execa('echo', ['unicorns']).pipeStderr('stderr.txt');536 537+// Similar to `echo unicorns &> stdout.txt` in Bash538+await execa('echo', ['unicorns'], {all:true}).pipeAll('all.txt');539+```540+541+@example <caption>Redirect input from a file</caption>542+```543+import {execa} from 'execa';544+545+// Similar to `cat < stdin.txt` in Bash546+const {stdout} = await execa('cat', {inputFile:'stdin.txt'});547+console.log(stdout);548+//=> 'unicorns'549+```550+551+@example <caption>Save and pipe output from a child process</caption>552+```553+import {execa} from 'execa';554+555+const {stdout} = await execa('echo', ['unicorns']).pipeStdout(process.stdout);556+// Prints `unicorns`557+console.log(stdout);558+// Also returns 'unicorns'559+```560+561+@example <caption>Pipe multiple processes</caption>562+```563+import {execa} from 'execa';564+565+// Similar to `echo unicorns | cat` in Bash566+const {stdout} = await execa('echo', ['unicorns']).pipeStdout(execa('cat'));567+console.log(stdout);568+//=> 'unicorns'569+```570+571+@example <caption>Handling errors</caption>572+```573+import {execa} from 'execa';574+575+// Catching an error576 try {535- await subprocess;577+ await execa('unknown', ['command']);578 } catch (error) {537- console.log(subprocess.killed); // true538- console.log(error.isCanceled); // true579+ console.log(error);580+ /*581+ {582+ message: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',583+ errno: -2,584+ code: 'ENOENT',585+ syscall: 'spawn unknown',586+ path: 'unknown',587+ spawnargs: ['command'],588+ originalMessage: 'spawn unknown ENOENT',589+ shortMessage: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',590+ command: 'unknown command',591+ escapedCommand: 'unknown command',592+ stdout: '',593+ stderr: '',594+ failed: true,595+ timedOut: false,596+ isCanceled: false,597+ killed: false598+ }599+ \*\/600 }601+```602 541-// Pipe the child process stdout to the current stdout542-execa('echo', ['unicorns']).stdout.pipe(process.stdout);603+@example <caption>Graceful termination</caption>604 ```605+const subprocess = execa('node');606+607+setTimeout(() => {608+ subprocess.kill('SIGTERM', {609+ forceKillAfterTimeout: 2000610+ });611+}, 1000);612+```613 */614 export function execa(615 file: string,616 arguments?: readonly string[],13 unmodified lines630 @param file - The program/script to execute.631 @param arguments - Arguments to pass to `file` on execution.632 @returns A `childProcessResult` object633 @throws A `childProcessResult` error634+635+@example <caption>Promise interface</caption>636+```637+import {execa} from 'execa';638+639+const {stdout} = execaSync('echo', ['unicorns']);640+console.log(stdout);641+//=> 'unicorns'642+```643+644+@example <caption>Redirect input from a file</caption>645+```646+import {execa} from 'execa';647+648+// Similar to `cat < stdin.txt` in Bash649+const {stdout} = execaSync('cat', {inputFile:'stdin.txt'});650+console.log(stdout);651+//=> 'unicorns'652+```653+654+@example <caption>Handling errors</caption>655+```656+import {execa} from 'execa';657+658+// Catching an error659+try {660+ execaSync('unknown', ['command']);661+} catch (error) {662+ console.log(error);663+ /*664+ {665+ message: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',666+ errno: -2,667+ code: 'ENOENT',668+ syscall: 'spawnSync unknown',669+ path: 'unknown',670+ spawnargs: ['command'],671+ originalMessage: 'spawnSync unknown ENOENT',672+ shortMessage: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',673+ command: 'unknown command',674+ escapedCommand: 'unknown command',675+ stdout: '',676+ stderr: '',677+ failed: true,678+ timedOut: false,679+ isCanceled: false,680+ killed: false681+ }682+ \*\/683+}684+```685 */686 export function execaSync(687 file: string,688 arguments?: readonly string[],34 unmodified lines723 */724 export function execaCommand(command: string, options?: Options): ExecaChildProcess;725 export function execaCommand(command: string, options?: Options<null>): ExecaChildProcess<Buffer>;726 727+/**728+Same as `execaCommand()` but synchronous.729+730+@param command - The program/script to execute and its arguments.731+@returns A `childProcessResult` object732+@throws A `childProcessResult` error733+734+@example735+```736+import {execaCommandSync} from 'execa';737+738+const {stdout} = execaCommandSync('echo unicorns');739+console.log(stdout);740+//=> 'unicorns'741+```742+*/743+export function execaCommandSync(command: string, options?: SyncOptions): ExecaSyncReturnValue;744+export function execaCommandSync(command: string, options?: SyncOptions<null>): ExecaSyncReturnValue<Buffer>;745+746 type TemplateExpression =747 | string748 | number749 | ExecaReturnValue<string | Buffer>1 unmodified line751 | Array<string | number | ExecaReturnValue<string | Buffer> | ExecaSyncReturnValue<string | Buffer>>;752 753 type Execa$<StdoutStderrType extends StdoutStderrAll = string> = {754 /**616- Same as `execa()` (including its return value) except both file and arguments are specified in a single tagged template string. For example, `` $`echo unicorns` `` is the same as `execa('echo', ['unicorns'])`.755+ Binds options to the `$` API. For example, you can use `$(options)` to create a new `$` instance with specific default options, which are then bound to both the asynchronous `` $`command` `` and synchronous `` $.sync`command` `` APIs.756 618- It's important to note that quotes, backslashes, and spaces are automatically escaped and have no special meaning unless the `shell` option is used. This escaping behavior also applies to interpolated expressions such as strings (`` $`echo ${'string'}` ``), arrays of strings (`` $`echo ${['array', 'of strings']}` ``), and so on.757+ > **Note:** Consecutive calls to this API will shallow merge the options.758 620- The `shell` option must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.759+ @param options - Options to set760+ @returns A new instance of `$` with those `options` set761 622- As a convenience, the result from previous `` $`command` `` or `` $.sync`command` `` calls can be used as template expressions in subsequent commands and `$`/`$.sync` will use the `stdout` value. See the example below `` with results from `$` or `$.sync` `` for more details.623-624- @returns An `ExecaChildProcess` that is both:625- - a `Promise` resolving or rejecting with a `childProcessResult`.626- - a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with some additional methods and properties.627- @throws A `childProcessResult` error628-629- @example <caption>Basic</caption>762+ @example763 ```764 import {$} from 'execa';765 633- const {stdout} = await $`echo unicorns`;634- // const {stdout} = await $`echo ${'unicorns'}`;635- // const {stdout} = await $`echo ${['unicorns', 'rainbows']}`;636-637- console.log(stdout);638- //=> 'unicorns'639- ```640-641- @example <caption>With options</caption>642- ```643- import {$} from 'execa';644-645- await $({stdio: 'inherit'})`echo unicorns`;646- //=> 'unicorns'647- ```648-649- @example <caption>With pre-defined options</caption>650- ```651- import {$} from 'execa';652-766 const $$ = $({stdio: 'inherit'});767+768 await $$`echo unicorns`;769 //=> 'unicorns'656- await $$({shell: true})`echo unicorns && echo rainbows`;657- //=> 'unicorns'658- //=> 'rainbows'659- ```770 661- @example <caption>Synchronous</caption>662- ```663- import {$} from 'execa';664-665- const {stdout} = $.sync`echo unicorns`;666- console.log(stdout);667- //=> 'unicorns'668-669- $({stdio: 'inherit'}).sync`echo rainbows`;771+ await $$`echo rainbows`;772 //=> 'rainbows'773 ```672-673- @example <caption>With results from `$` or `$.sync`</caption>674- ```675- import {$} from 'execa';676-677- const unicorns = await $`echo unicorns`;678-679- $({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;680- //=> 'unicorns rainbows'681- ```774 */775 (options: Options<undefined>): Execa$<StdoutStderrType>;776 (options: Options): Execa$;777 (options: Options<null>): Execa$<Buffer>;7 unmodified lines785 786 @returns A `childProcessResult` object787 @throws A `childProcessResult` error788 697- @example789+ @example <caption>Basic</caption>790 ```791 import {$} from 'execa';792 793+ const branch = $.sync`git branch --show-current`794+ $.sync`dep deploy --branch=${branch}`795+ ```796+797+ @example <caption>Multiple arguments</caption>798+ ```799+ import {$} from 'execa';800+801+ const args = ['unicorns', '&', 'rainbows!']701- const {stdout} = $.sync`echo unicorns`;802+ const {stdout} = $.sync`echo ${args}`;803 console.log(stdout);804+ //=> 'unicorns & rainbows!'805+ ```806+807+ @example <caption>With options</caption>808+ ```809+ import {$} from 'execa';810+811+ $.sync({stdio: 'inherit'})`echo unicorns`;812 //=> 'unicorns'813+ ```814 815+ @example <caption>Shared options</caption>816+ ```817+ import {$} from 'execa';818+819+ const $$ = $({stdio: 'inherit'});820+821+ $$.sync`echo unicorns`;822+ //=> 'unicorns'823+705- $({stdio: 'inherit'}).sync`echo rainbows`;824+ $$.sync`echo rainbows`;825 //=> 'rainbows'826 ```827 */828 sync(19 unmodified lines848 @example <caption>Basic</caption>849 ```850 import {$} from 'execa';851 733-const {stdout} = await $`echo unicorns`;734-// const {stdout} = await $`echo ${'unicorns'}`;735-// const {stdout} = await $`echo ${['unicorns', 'rainbows']}`;852+const branch = await $`git branch --show-current`853+await $`dep deploy --branch=${branch}`854+```855 856+@example <caption>Multiple arguments</caption>857+```858+import {$} from 'execa';859+860+const args = ['unicorns', '&', 'rainbows!']861+const {stdout} = await $`echo ${args}`;862 console.log(stdout);738-//=> 'unicorns'863+//=> 'unicorns & rainbows!'864 ```865 866 @example <caption>With options</caption>867 ```2 unmodified lines870 await $({stdio: 'inherit'})`echo unicorns`;871 //=> 'unicorns'872 ```873 749-@example <caption>With pre-defined options</caption>874+@example <caption>Shared options</caption>875 ```876 import {$} from 'execa';877 878 const $$ = $({stdio: 'inherit'});879+880 await $$`echo unicorns`;881 //=> 'unicorns'756-await $$({shell: true})`echo unicorns && echo rainbows`;757-//=> 'unicorns'758-//=> 'rainbows'759-```882 761-@example <caption>Synchronous</caption>762-```763-import {$} from 'execa';764-765-const {stdout} = $.sync`echo unicorns`;766-console.log(stdout);767-//=> 'unicorns'768-769-$({stdio: 'inherit'}).sync`echo rainbows`;883+await $$`echo rainbows`;884 //=> 'rainbows'885 ```772-773-@example <caption>With results from `$` or `$.sync`</caption>774-```775-import {$} from 'execa';776-777-const unicorns = await $`echo unicorns`;778-779-$({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;780-//=> 'unicorns rainbows'781-```886 */887 export const $: Execa$;888 889 /**786-Same as `execaCommand()` but synchronous.787-788-@param command - The program/script to execute and its arguments.789-@returns A `childProcessResult` object790-@throws A `childProcessResult` error791-*/792-export function execaCommandSync(command: string, options?: SyncOptions): ExecaSyncReturnValue;793-export function execaCommandSync(command: string, options?: SyncOptions<null>): ExecaSyncReturnValue<Buffer>;794-795-/**890 Execute a Node.js script as a child process.891 892 Same as `execa('node', [scriptPath, ...arguments], options)` except (like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options)):893 - the current Node version and options are used. This can be overridden using the `nodePath` and `nodeArguments` options.5 unmodified lines899 @returns An `ExecaChildProcess` that is both:900 - a `Promise` resolving or rejecting with a `childProcessResult`.901 - a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with some additional methods and properties.902 @throws A `childProcessResult` error903+904+@example905+```906+import {execa} from 'execa';907+908+await execaNode('scriptPath', ['argument']);909+```910 */911 export function execaNode(912 scriptPath: string,913 arguments?: readonly string[],9 unmodified linesmodified:readme.md-1 removed
177 unmodified lines178 command: 'unknown command',179 escapedCommand: 'unknown command',180 stdout: '',181 stderr: '',182- all: '',182 failed: true,183 timedOut: false,184 isCanceled: false,185 killed: false589 unmodified lines