Skip to content
Myr

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

6 files+165 added−143 removed

modified:types/arguments/options.d.ts+17 added-9 removed
242 unmodified lines
243 ```244 import {execa} from 'execa';245246 const abortController = new AbortController();247 const subprocess = execa('node', [], {cancelSignal: abortController.signal});247248 setTimeout(() => {249 abortController.abort();251 }, 1000);250 }, 5000);251252 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 }258259 throw error;260 }261 ```262 */263 readonly cancelSignal?: Unless<IsSync, AbortSignal>;
71 unmodified lines
335336@example337338```337await execa('./run.js', {verbose: 'full'}) // Same value for stdout and stderr338await execa('./run.js', {verbose: {stdout: 'none', stderr: 'full'}}) // Different values339// Same value for stdout and stderr340await execa({verbose: 'full'})`npm run build`;341342// Different values for stdout and stderr343await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;344```345*/346export type Options = CommonOptions<false>;347
4 unmodified lines
352353@example354355```351execaSync('./run.js', {verbose: 'full'}) // Same value for stdout and stderr352execaSync('./run.js', {verbose: {stdout: 'none', stderr: 'full'}}) // Different values356// Same value for stdout and stderr357execaSync({verbose: 'full'})`npm run build`;358359// Different values for stdout and stderr360execaSync({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;361```362*/363export type SyncOptions = CommonOptions<true>;364
4 unmodified lines
modified:types/methods/command.d.ts+6 added-6 removed
modified:types/methods/main-async.d.ts+117 added-69 removed
31 unmodified lines
32- 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` error3536@example <caption>Promise interface</caption>36@example <caption>Simple syntax</caption>3738```39import {execa} from 'execa';4040const {stdout} = await execa('echo', ['unicorns']);41const {stdout} = await execa`npm run build`;42// Print command's output43console.log(stdout);42//=> 'unicorns'44```4545@example <caption>Global/shared options</caption>46@example <caption>Script</caption>4748```47import {execa as execa_} from 'execa';49import {$} from 'execa';5049const execa = execa_({verbose: 'full'});51const {stdout: name} = await $`cat package.json`.pipe`grep name`;52console.log(name);5351await execa('echo', ['unicorns']);52//=> 'unicorns'54const branch = await $`git branch --show-current`;55await $`dep deploy --branch=${branch}`;5657await Promise.all([58 $`sleep 1`,59 $`sleep 2`,60 $`sleep 3`,61]);6263const directoryName = 'foo bar';64await $`mkdir /tmp/${directoryName}`;65```6655@example <caption>Template string interface</caption>67@example <caption>Local binaries</caption>6869```58import {execa} from 'execa';70$ npm install -D eslint71```7260const arg = 'unicorns';61const {stdout} = await execa`echo ${arg} & rainbows!`;62console.log(stdout);63//=> 'unicorns & rainbows!'73```74await execa({preferLocal: true})`eslint`;75```7666@example <caption>Template string multiple arguments</caption>77@example <caption>Pipe multiple subprocesses</caption>7879```69import {execa} from 'execa';80const {stdout, pipedFrom} = await execa`npm run build`81 .pipe`sort`82 .pipe`head -n 2`;8371const args = ['unicorns', '&', 'rainbows!'];72const {stdout} = await execa`echo ${args}`;84// Output of `npm run build | sort | head -n 2`85console.log(stdout);74//=> 'unicorns & rainbows!'86// Output of `npm run build | sort`87console.log(pipedFrom[0].stdout);88// Output of `npm run build`89console.log(pipedFrom[0].pipedFrom[0].stdout);90```9177@example <caption>Template string with options</caption>92@example <caption>Interleaved output</caption>9394```80import {execa} from 'execa';8182await execa({verbose: 'full'})`echo unicorns`;83//=> 'unicorns'95const {all} = await execa({all: true})`npm run build`;96// stdout + stderr, interleaved97console.log(all);98```9986@example <caption>Redirect output to a file</caption>100@example <caption>Programmatic + terminal output</caption>101102```88import {execa} from 'execa';103const {stdout} = await execa({stdout: ['pipe', 'inherit']})`npm run build`;104// stdout is also printed to the terminal105console.log(stdout);106```10790// Similar to `echo unicorns > stdout.txt` in Bash91await execa('echo', ['unicorns'], {stdout: {file: 'stdout.txt'}});108@example <caption>Files</caption>10993// Similar to `echo unicorns 2> stdout.txt` in Bash94await execa('echo', ['unicorns'], {stderr: {file: 'stderr.txt'}});9596// Similar to `echo unicorns &> stdout.txt` in Bash97await execa('echo', ['unicorns'], {stdout: {file: 'all.txt'}, stderr: {file: 'all.txt'}});110```99100@example <caption>Redirect input from a file</caption>111// Similar to: npm run build > output.txt112await execa({stdout: {file: './output.txt'}})`npm run build`;113```102import {execa} from 'execa';114104// Similar to `cat < stdin.txt` in Bash105const {stdout} = await execa('cat', {inputFile: 'stdin.txt'});115@example <caption>Simple input</caption>116117```118const {stdout} = await execa({input: getInputString()})`sort`;119console.log(stdout);107//=> 'unicorns'120```121110@example <caption>Save and pipe output from a subprocess</caption>111```112import {execa} from 'execa';122@example <caption>Split into text lines</caption>123114const {stdout} = await execa('echo', ['unicorns'], {stdout: ['pipe', 'inherit']});115// Prints `unicorns`116console.log(stdout);117// Also returns 'unicorns'124```125const {stdout} = await execa({lines: true})`npm run build`;126// Print first 10 lines127console.log(stdout.slice(0, 10).join('\n'));128```129120@example <caption>Pipe multiple subprocesses</caption>130@example <caption>Iterate over text lines</caption>131132```122import {execa} from 'execa';133for await (const line of execa`npm run build`) {134 if (line.includes('WARN')) {135 console.warn(line);136 }137}138```139124// Similar to `echo unicorns | cat` in Bash125const {stdout} = await execa('echo', ['unicorns']).pipe(execa('cat'));126console.log(stdout);127//=> 'unicorns'140@example <caption>Transform/filter output</caption>141142```143let count = 0;144130@example <caption>Pipe with template strings</caption>145// Filter out secret lines, then prepend the line number146const transform = function * (line) {147 if (!line.includes('secret')) {148 yield `[${count++}] ${line}`;149 }150};151152await execa({stdout: transform})`npm run build`;153```132import {execa} from 'execa';154134await execa`npm run build`135 .pipe`sort`136 .pipe`head -n2`;155@example <caption>Web streams</caption>156157```158const response = await fetch('https://example.com');159await execa({stdin: response.body})`sort`;160```161139@example <caption>Iterate over output lines</caption>162@example <caption>Convert to Duplex stream</caption>163164```165import {execa} from 'execa';166import {pipeline} from 'node:stream/promises';167import {createReadStream, createWriteStream} from 'node:fs';168143for await (const line of execa`npm run build`)) {144 if (line.includes('ERROR')) {145 console.log(line);146 }147}169await pipeline(170 createReadStream('./input.txt'),171 execa`node ./transform.js`.duplex(),172 createWriteStream('./output.txt'),173);174```175150@example <caption>Handling errors</caption>176@example <caption>Detailed error</caption>177178```152import {execa} from 'execa';179import {execa, ExecaError} from 'execa';180154// Catching an error181try {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 lines
214 path: 'unknown',215 spawnargs: [ 'command' ]216 }217 }190 \*\/218 *\/219}220```221222@example <caption>Verbose mode</caption>223224```225await execa`npm run build`;226await execa`npm run test`;227```228229```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*/242export declare const execa: Execa<{}>;
modified:types/methods/main-sync.d.ts+4 added-47 removed
modified:types/methods/node.d.ts+6 added-2 removed
modified:types/methods/script.d.ts+15 added-10 removed
67 unmodified lines
68```6970@example <caption>Verbose mode</caption>71```72> node file.js73unicorns74rainbows72$ node build.js73Building application...74Done building.75Running tests...76Error: the entrypoint is invalid.7776> NODE_DEBUG=execa node file.js77[19:49:00.360] [0] $ echo unicorns78unicorns79[19:49:00.383] [0] √ (done in 23ms)80[19:49:00.383] [1] $ echo rainbows81rainbows82[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*/90export const $: ExecaScript<{}>;