Skip to content
Myr

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

2 files+207 added−107 removed

modified:index.d.ts+207 added-106 removed
513 unmodified lines
514 - 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` error517518@example518@example <caption>Promise interface</caption>519```520import {execa} from 'execa';521522const {stdout} = await execa('echo', ['unicorns']);523console.log(stdout);524//=> 'unicorns'525```526526// Cancelling a spawned process527@example <caption>Redirect output to a file</caption>528```529import {execa} from 'execa';530528const subprocess = execa('node');531// Similar to `echo unicorns > stdout.txt` in Bash532await execa('echo', ['unicorns']).pipeStdout('stdout.txt');533530setTimeout(() => {531 subprocess.cancel()532}, 1000);534// Similar to `echo unicorns 2> stdout.txt` in Bash535await execa('echo', ['unicorns']).pipeStderr('stderr.txt');536537// Similar to `echo unicorns &> stdout.txt` in Bash538await execa('echo', ['unicorns'], {all:true}).pipeAll('all.txt');539```540541@example <caption>Redirect input from a file</caption>542```543import {execa} from 'execa';544545// Similar to `cat < stdin.txt` in Bash546const {stdout} = await execa('cat', {inputFile:'stdin.txt'});547console.log(stdout);548//=> 'unicorns'549```550551@example <caption>Save and pipe output from a child process</caption>552```553import {execa} from 'execa';554555const {stdout} = await execa('echo', ['unicorns']).pipeStdout(process.stdout);556// Prints `unicorns`557console.log(stdout);558// Also returns 'unicorns'559```560561@example <caption>Pipe multiple processes</caption>562```563import {execa} from 'execa';564565// Similar to `echo unicorns | cat` in Bash566const {stdout} = await execa('echo', ['unicorns']).pipeStdout(execa('cat'));567console.log(stdout);568//=> 'unicorns'569```570571@example <caption>Handling errors</caption>572```573import {execa} from 'execa';574575// Catching an error576try {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```602541// Pipe the child process stdout to the current stdout542execa('echo', ['unicorns']).stdout.pipe(process.stdout);603@example <caption>Graceful termination</caption>604```605const subprocess = execa('node');606607setTimeout(() => {608 subprocess.kill('SIGTERM', {609 forceKillAfterTimeout: 2000610 });611}, 1000);612```613*/614export function execa(615 file: string,616 arguments?: readonly string[],
13 unmodified lines
630@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` error634635@example <caption>Promise interface</caption>636```637import {execa} from 'execa';638639const {stdout} = execaSync('echo', ['unicorns']);640console.log(stdout);641//=> 'unicorns'642```643644@example <caption>Redirect input from a file</caption>645```646import {execa} from 'execa';647648// Similar to `cat < stdin.txt` in Bash649const {stdout} = execaSync('cat', {inputFile:'stdin.txt'});650console.log(stdout);651//=> 'unicorns'652```653654@example <caption>Handling errors</caption>655```656import {execa} from 'execa';657658// Catching an error659try {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*/686export function execaSync(687 file: string,688 arguments?: readonly string[],
34 unmodified lines
723*/724export function execaCommand(command: string, options?: Options): ExecaChildProcess;725export function execaCommand(command: string, options?: Options<null>): ExecaChildProcess<Buffer>;726727/**728Same as `execaCommand()` but synchronous.729730@param command - The program/script to execute and its arguments.731@returns A `childProcessResult` object732@throws A `childProcessResult` error733734@example735```736import {execaCommandSync} from 'execa';737738const {stdout} = execaCommandSync('echo unicorns');739console.log(stdout);740//=> 'unicorns'741```742*/743export function execaCommandSync(command: string, options?: SyncOptions): ExecaSyncReturnValue;744export function execaCommandSync(command: string, options?: SyncOptions<null>): ExecaSyncReturnValue<Buffer>;745746type TemplateExpression =747 | string748 | number749 | ExecaReturnValue<string | Buffer>
1 unmodified line
751 | Array<string | number | ExecaReturnValue<string | Buffer> | ExecaSyncReturnValue<string | Buffer>>;752753type 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.756618 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.758620 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` set761622 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.623624 @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` error628629 @example <caption>Basic</caption>762 @example763 ```764 import {$} from 'execa';765633 const {stdout} = await $`echo unicorns`;634 // const {stdout} = await $`echo ${'unicorns'}`;635 // const {stdout} = await $`echo ${['unicorns', 'rainbows']}`;636637 console.log(stdout);638 //=> 'unicorns'639 ```640641 @example <caption>With options</caption>642 ```643 import {$} from 'execa';644645 await $({stdio: 'inherit'})`echo unicorns`;646 //=> 'unicorns'647 ```648649 @example <caption>With pre-defined options</caption>650 ```651 import {$} from 'execa';652766 const $$ = $({stdio: 'inherit'});767768 await $$`echo unicorns`;769 //=> 'unicorns'656 await $$({shell: true})`echo unicorns && echo rainbows`;657 //=> 'unicorns'658 //=> 'rainbows'659 ```770661 @example <caption>Synchronous</caption>662 ```663 import {$} from 'execa';664665 const {stdout} = $.sync`echo unicorns`;666 console.log(stdout);667 //=> 'unicorns'668669 $({stdio: 'inherit'}).sync`echo rainbows`;771 await $$`echo rainbows`;772 //=> 'rainbows'773 ```672673 @example <caption>With results from `$` or `$.sync`</caption>674 ```675 import {$} from 'execa';676677 const unicorns = await $`echo unicorns`;678679 $({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 lines
785786 @returns A `childProcessResult` object787 @throws A `childProcessResult` error788697 @example789 @example <caption>Basic</caption>790 ```791 import {$} from 'execa';792793 const branch = $.sync`git branch --show-current`794 $.sync`dep deploy --branch=${branch}`795 ```796797 @example <caption>Multiple arguments</caption>798 ```799 import {$} from 'execa';800801 const args = ['unicorns', '&', 'rainbows!']701 const {stdout} = $.sync`echo unicorns`;802 const {stdout} = $.sync`echo ${args}`;803 console.log(stdout);804 //=> 'unicorns & rainbows!'805 ```806807 @example <caption>With options</caption>808 ```809 import {$} from 'execa';810811 $.sync({stdio: 'inherit'})`echo unicorns`;812 //=> 'unicorns'813 ```814815 @example <caption>Shared options</caption>816 ```817 import {$} from 'execa';818819 const $$ = $({stdio: 'inherit'});820821 $$.sync`echo unicorns`;822 //=> 'unicorns'823705 $({stdio: 'inherit'}).sync`echo rainbows`;824 $$.sync`echo rainbows`;825 //=> 'rainbows'826 ```827 */828 sync(
19 unmodified lines
848@example <caption>Basic</caption>849```850import {$} from 'execa';851733const {stdout} = await $`echo unicorns`;734// const {stdout} = await $`echo ${'unicorns'}`;735// const {stdout} = await $`echo ${['unicorns', 'rainbows']}`;852const branch = await $`git branch --show-current`853await $`dep deploy --branch=${branch}`854```855856@example <caption>Multiple arguments</caption>857```858import {$} from 'execa';859860const args = ['unicorns', '&', 'rainbows!']861const {stdout} = await $`echo ${args}`;862console.log(stdout);738//=> 'unicorns'863//=> 'unicorns & rainbows!'864```865866@example <caption>With options</caption>867```
2 unmodified lines
870await $({stdio: 'inherit'})`echo unicorns`;871//=> 'unicorns'872```873749@example <caption>With pre-defined options</caption>874@example <caption>Shared options</caption>875```876import {$} from 'execa';877878const $$ = $({stdio: 'inherit'});879880await $$`echo unicorns`;881//=> 'unicorns'756await $$({shell: true})`echo unicorns && echo rainbows`;757//=> 'unicorns'758//=> 'rainbows'759```882761@example <caption>Synchronous</caption>762```763import {$} from 'execa';764765const {stdout} = $.sync`echo unicorns`;766console.log(stdout);767//=> 'unicorns'768769$({stdio: 'inherit'}).sync`echo rainbows`;883await $$`echo rainbows`;884//=> 'rainbows'885```772773@example <caption>With results from `$` or `$.sync`</caption>774```775import {$} from 'execa';776777const unicorns = await $`echo unicorns`;778779$({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;780//=> 'unicorns rainbows'781```886*/887export const $: Execa$;888889/**786Same as `execaCommand()` but synchronous.787788@param command - The program/script to execute and its arguments.789@returns A `childProcessResult` object790@throws A `childProcessResult` error791*/792export function execaCommandSync(command: string, options?: SyncOptions): ExecaSyncReturnValue;793export function execaCommandSync(command: string, options?: SyncOptions<null>): ExecaSyncReturnValue<Buffer>;794795/**890Execute a Node.js script as a child process.891892Same 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 lines
899@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` error903904@example905```906import {execa} from 'execa';907908await execaNode('scriptPath', ['argument']);909```910*/911export function execaNode(912 scriptPath: string,913 arguments?: readonly string[],
9 unmodified lines
modified:readme.md-1 removed
177 unmodified lines
178 command: 'unknown command',179 escapedCommand: 'unknown command',180 stdout: '',181 stderr: '',182 all: '',182 failed: true,183 timedOut: false,184 isCanceled: false,185 killed: false
589 unmodified lines