Improve examples used in types (#1000)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent aca3db3 · Browse files · 6 files·+165 added−143 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+165 added−143 removed
- other:+165 added−143 removed
Provenance
- Commit
63de206- Tree
cdc64aa- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
aca3db3
6 files changed+165 added−143 removed
modified:types/arguments/options.d.ts+17 added-9 removed
242 unmodified lines243 ```244 import {execa} from 'execa';245 246 const abortController = new AbortController();247- const subprocess = execa('node', [], {cancelSignal: abortController.signal});247 248 setTimeout(() => {249 abortController.abort();251- }, 1000);250+ }, 5000);251 252 try {254- await subprocess;253+ await execa({cancelSignal: abortController.signal})`npm run build`;254 } catch (error) {256- console.log(error.isTerminated); // true257- console.log(error.isCanceled); // true255+ if (error.isCanceled) {256+ console.error('Aborted by cancelSignal.');257+ }258+259+ throw error;260 }261 ```262 */263 readonly cancelSignal?: Unless<IsSync, AbortSignal>;71 unmodified lines335 336 @example337 338 ```337-await execa('./run.js', {verbose: 'full'}) // Same value for stdout and stderr338-await execa('./run.js', {verbose: {stdout: 'none', stderr: 'full'}}) // Different values339+// Same value for stdout and stderr340+await execa({verbose: 'full'})`npm run build`;341+342+// Different values for stdout and stderr343+await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;344 ```345 */346 export type Options = CommonOptions<false>;347 4 unmodified lines352 353 @example354 355 ```351-execaSync('./run.js', {verbose: 'full'}) // Same value for stdout and stderr352-execaSync('./run.js', {verbose: {stdout: 'none', stderr: 'full'}}) // Different values356+// Same value for stdout and stderr357+execaSync({verbose: 'full'})`npm run build`;358+359+// Different values for stdout and stderr360+execaSync({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;361 ```362 */363 export type SyncOptions = CommonOptions<true>;364 4 unmodified linesmodified:types/methods/command.d.ts+6 added-6 removed
29 unmodified lines303132333435363435363738394020 unmodified lines6162636465666765666768697029 unmodified lines@example```import {execaCommand} from 'execa';
const {stdout} = await execaCommand('echo unicorns');console.log(stdout);//=> 'unicorns'for await (const commandAndArguments of getReplLine()) { await execaCommand(commandAndArguments);}```*/export declare const execaCommand: ExecaCommand<{}>;
20 unmodified lines@example```import {execaCommandSync} from 'execa';
const {stdout} = execaCommandSync('echo unicorns');console.log(stdout);//=> 'unicorns'for (const commandAndArguments of getReplLine()) { execaCommandSync(commandAndArguments);}```*/export declare const execaCommandSync: ExecaCommandSync<{}>;modified:types/methods/main-async.d.ts+117 added-69 removed
31 unmodified lines32 - a `Promise` resolving or rejecting with a subprocess `result`.33 - a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with some additional methods and properties.34 @throws A subprocess `result` error35 36-@example <caption>Promise interface</caption>36+@example <caption>Simple syntax</caption>37+38 ```39 import {execa} from 'execa';40 40-const {stdout} = await execa('echo', ['unicorns']);41+const {stdout} = await execa`npm run build`;42+// Print command's output43 console.log(stdout);42-//=> 'unicorns'44 ```45 45-@example <caption>Global/shared options</caption>46+@example <caption>Script</caption>47+48 ```47-import {execa as execa_} from 'execa';49+import {$} from 'execa';50 49-const execa = execa_({verbose: 'full'});51+const {stdout: name} = await $`cat package.json`.pipe`grep name`;52+console.log(name);53 51-await execa('echo', ['unicorns']);52-//=> 'unicorns'54+const branch = await $`git branch --show-current`;55+await $`dep deploy --branch=${branch}`;56+57+await Promise.all([58+ $`sleep 1`,59+ $`sleep 2`,60+ $`sleep 3`,61+]);62+63+const directoryName = 'foo bar';64+await $`mkdir /tmp/${directoryName}`;65 ```66 55-@example <caption>Template string interface</caption>67+@example <caption>Local binaries</caption>68 69 ```58-import {execa} from 'execa';70+$ npm install -D eslint71+```72 60-const arg = 'unicorns';61-const {stdout} = await execa`echo ${arg} & rainbows!`;62-console.log(stdout);63-//=> 'unicorns & rainbows!'73 ```74+await execa({preferLocal: true})`eslint`;75+```76 66-@example <caption>Template string multiple arguments</caption>77+@example <caption>Pipe multiple subprocesses</caption>78 79 ```69-import {execa} from 'execa';80+const {stdout, pipedFrom} = await execa`npm run build`81+ .pipe`sort`82+ .pipe`head -n 2`;83 71-const args = ['unicorns', '&', 'rainbows!'];72-const {stdout} = await execa`echo ${args}`;84+// Output of `npm run build | sort | head -n 2`85 console.log(stdout);74-//=> 'unicorns & rainbows!'86+// Output of `npm run build | sort`87+console.log(pipedFrom[0].stdout);88+// Output of `npm run build`89+console.log(pipedFrom[0].pipedFrom[0].stdout);90 ```91 77-@example <caption>Template string with options</caption>92+@example <caption>Interleaved output</caption>93 94 ```80-import {execa} from 'execa';81-82-await execa({verbose: 'full'})`echo unicorns`;83-//=> 'unicorns'95+const {all} = await execa({all: true})`npm run build`;96+// stdout + stderr, interleaved97+console.log(all);98 ```99 86-@example <caption>Redirect output to a file</caption>100+@example <caption>Programmatic + terminal output</caption>101+102 ```88-import {execa} from 'execa';103+const {stdout} = await execa({stdout: ['pipe', 'inherit']})`npm run build`;104+// stdout is also printed to the terminal105+console.log(stdout);106+```107 90-// Similar to `echo unicorns > stdout.txt` in Bash91-await execa('echo', ['unicorns'], {stdout: {file: 'stdout.txt'}});108+@example <caption>Files</caption>109 93-// Similar to `echo unicorns 2> stdout.txt` in Bash94-await execa('echo', ['unicorns'], {stderr: {file: 'stderr.txt'}});95-96-// Similar to `echo unicorns &> stdout.txt` in Bash97-await execa('echo', ['unicorns'], {stdout: {file: 'all.txt'}, stderr: {file: 'all.txt'}});110 ```99-100-@example <caption>Redirect input from a file</caption>111+// Similar to: npm run build > output.txt112+await execa({stdout: {file: './output.txt'}})`npm run build`;113 ```102-import {execa} from 'execa';114 104-// Similar to `cat < stdin.txt` in Bash105-const {stdout} = await execa('cat', {inputFile: 'stdin.txt'});115+@example <caption>Simple input</caption>116+117+```118+const {stdout} = await execa({input: getInputString()})`sort`;119 console.log(stdout);107-//=> 'unicorns'120 ```121 110-@example <caption>Save and pipe output from a subprocess</caption>111-```112-import {execa} from 'execa';122+@example <caption>Split into text lines</caption>123 114-const {stdout} = await execa('echo', ['unicorns'], {stdout: ['pipe', 'inherit']});115-// Prints `unicorns`116-console.log(stdout);117-// Also returns 'unicorns'124 ```125+const {stdout} = await execa({lines: true})`npm run build`;126+// Print first 10 lines127+console.log(stdout.slice(0, 10).join('\n'));128+```129 120-@example <caption>Pipe multiple subprocesses</caption>130+@example <caption>Iterate over text lines</caption>131+132 ```122-import {execa} from 'execa';133+for await (const line of execa`npm run build`) {134+ if (line.includes('WARN')) {135+ console.warn(line);136+ }137+}138+```139 124-// Similar to `echo unicorns | cat` in Bash125-const {stdout} = await execa('echo', ['unicorns']).pipe(execa('cat'));126-console.log(stdout);127-//=> 'unicorns'140+@example <caption>Transform/filter output</caption>141+142 ```143+let count = 0;144 130-@example <caption>Pipe with template strings</caption>145+// Filter out secret lines, then prepend the line number146+const transform = function * (line) {147+ if (!line.includes('secret')) {148+ yield `[${count++}] ${line}`;149+ }150+};151+152+await execa({stdout: transform})`npm run build`;153 ```132-import {execa} from 'execa';154 134-await execa`npm run build`135- .pipe`sort`136- .pipe`head -n2`;155+@example <caption>Web streams</caption>156+157 ```158+const response = await fetch('https://example.com');159+await execa({stdin: response.body})`sort`;160+```161 139-@example <caption>Iterate over output lines</caption>162+@example <caption>Convert to Duplex stream</caption>163+164 ```165 import {execa} from 'execa';166+import {pipeline} from 'node:stream/promises';167+import {createReadStream, createWriteStream} from 'node:fs';168 143-for await (const line of execa`npm run build`)) {144- if (line.includes('ERROR')) {145- console.log(line);146- }147-}169+await pipeline(170+ createReadStream('./input.txt'),171+ execa`node ./transform.js`.duplex(),172+ createWriteStream('./output.txt'),173+);174 ```175 150-@example <caption>Handling errors</caption>176+@example <caption>Detailed error</caption>177+178 ```152-import {execa} from 'execa';179+import {execa, ExecaError} from 'execa';180 154-// Catching an error181 try {156- await execa('unknown', ['command']);182+ await execa`unknown command`;183 } catch (error) {184+ if (error instanceof ExecaError) {158- console.log(error);185+ console.log(error);186+ }187 /*188 ExecaError: Command failed with ENOENT: unknown command189 spawn unknown ENOENT190 at ...23 unmodified lines214 path: 'unknown',215 spawnargs: [ 'command' ]216 }217 }190- \*\/218+ *\/219 }220 ```221+222+@example <caption>Verbose mode</caption>223+224+```225+await execa`npm run build`;226+await execa`npm run test`;227+```228+229+```230+$ NODE_DEBUG=execa node build.js231+[00:57:44.581] [0] $ npm run build232+[00:57:44.653] [0] Building application...233+[00:57:44.653] [0] Done building.234+[00:57:44.658] [0] ✔ (done in 78ms)235+[00:57:44.658] [1] $ npm run test236+[00:57:44.740] [1] Running tests...237+[00:57:44.740] [1] Error: the entrypoint is invalid.238+[00:57:44.747] [1] ✘ Command failed with exit code 1: npm run test239+[00:57:44.747] [1] ✘ (done in 89ms)240+```241 */242 export declare const execa: Execa<{}>;modified:types/methods/main-sync.d.ts+4 added-47 removed
27 unmodified lines282930313233343233363738343536414243444546373839484050515253545556575859606162636465666768697071727374757677787980818283414227 unmodified lines@param arguments - Arguments to pass to `file` on execution.@returns A subprocess `result` object@throws A subprocess `result` error
@example <caption>Promise interface</caption>```import {execa} from 'execa';@example
const {stdout} = execaSync('echo', ['unicorns']);console.log(stdout);//=> 'unicorns'```import {execaSync} from 'execa';
@example <caption>Redirect input from a file</caption>```import {execa} from 'execa';
// Similar to `cat < stdin.txt` in Bashconst {stdout} = execaSync('cat', {inputFile: 'stdin.txt'});const {stdout} = execaSync`npm run build`;// Print command's outputconsole.log(stdout);//=> 'unicorns'```
@example <caption>Handling errors</caption>```import {execa} from 'execa';
// Catching an errortry { execaSync('unknown', ['command']);} catch (error) { console.log(error); /* { message: 'Command failed with ENOENT: unknown command\nspawnSync unknown ENOENT', errno: -2, code: 'ENOENT', syscall: 'spawnSync unknown', path: 'unknown', spawnargs: ['command'], shortMessage: 'Command failed with ENOENT: unknown command\nspawnSync unknown ENOENT', originalMessage: 'spawnSync unknown ENOENT', command: 'unknown command', escapedCommand: 'unknown command', cwd: '/path/to/cwd', failed: true, timedOut: false, isCanceled: false, isTerminated: false, isMaxBuffer: false, stdio: [], pipedFrom: [] } \*\/}```*/export declare const execaSync: ExecaSync<{}>;modified:types/methods/node.d.ts+6 added-2 removed
34 unmodified lines3536373839394041414243444546474834 unmodified lines@throws A subprocess `result` error
@example```import {execaNode} from 'execa';import {execaNode, execa} from 'execa';
await execaNode('scriptPath', ['argument']);await execaNode`file.js argument`;// Is the same as:await execa({node: true})`file.js argument`;// Or:await execa`node file.js argument`;```*/export declare const execaNode: ExecaNode<{}>;modified:types/methods/script.d.ts+15 added-10 removed
67 unmodified lines68 ```69 70 @example <caption>Verbose mode</caption>71 ```72-> node file.js73-unicorns74-rainbows72+$ node build.js73+Building application...74+Done building.75+Running tests...76+Error: the entrypoint is invalid.77 76-> NODE_DEBUG=execa node file.js77-[19:49:00.360] [0] $ echo unicorns78-unicorns79-[19:49:00.383] [0] √ (done in 23ms)80-[19:49:00.383] [1] $ echo rainbows81-rainbows82-[19:49:00.404] [1] √ (done in 21ms)78+$ NODE_DEBUG=execa node build.js79+[00:57:44.581] [0] $ npm run build80+[00:57:44.653] [0] Building application...81+[00:57:44.653] [0] Done building.82+[00:57:44.658] [0] ✔ (done in 78ms)83+[00:57:44.658] [1] $ npm run test84+[00:57:44.740] [1] Running tests...85+[00:57:44.740] [1] Error: the entrypoint is invalid.86+[00:57:44.747] [1] ✘ Command failed with exit code 1: npm run test87+[00:57:44.747] [1] ✘ (done in 89ms)88 ```89 */90 export const $: ExecaScript<{}>;