Skip to content
Myr

Improve documentation of Execa methods (#1115)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent faab26e · Browse files · 11 files+96 added−55 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

11 files+96 added−55 removed

  • other:+96 added−55 removed

11 files+96 added−55 removed

modified:docs/api.md+63 added-38 removed
4 unmodified lines
5<br>67# 📔 API reference89This lists all available [methods](#methods) and their [options](#options). This also describes the properties of the [subprocess](#subprocess), [result](#result) and [error](#execaerror) they return.9This lists all available [methods](#methods) and their [options](#options-1). This also describes the properties of the [subprocess](#subprocess), [result](#result) and [error](#execaerror) they return.1011## Methods1213### execa(file, arguments?, options?)1415`file`: `string | URL`\16`arguments`: `string[]`\17`options`: [`Options`](#options)\17`options`: [`Options`](#options-1)\18_Returns_: [`ResultPromise`](#return-value)1920Executes a command using `file ...arguments`.2122More info on the [syntax](execution.md#array-syntax) and [escaping](escaping.md#array-syntax).2324### execa\`command\`25### execa(options)\`command\`24### $(file, arguments?, options?)2527`command`: `string`\28`options`: [`Options`](#options)\26`file`: `string | URL`\27`arguments`: `string[]`\28`options`: [`Options`](#options-1)\29_Returns_: [`ResultPromise`](#return-value)3031Executes a command. `command` is a [template string](execution.md#template-string-syntax) that includes both the `file` and its `arguments`.31Same as [`execa()`](#execafile-arguments-options) but using [script-friendly default options](scripts.md#script-files).3233More info on the [syntax](execution.md#template-string-syntax) and [escaping](escaping.md#template-string-syntax).33This is the preferred method when executing multiple commands in a script file.3435### execa(options)35[More info.](scripts.md)3637`options`: [`Options`](#options)\38_Returns_: [`execa`](#execafile-arguments-options)37### execaNode(scriptPath, arguments?, options?)3840Returns a new instance of Execa but with different default [`options`](#options). Consecutive calls are merged to previous ones.39`scriptPath`: `string | URL`\40`arguments`: `string[]`\41`options`: [`Options`](#options-1)\42_Returns_: [`ResultPromise`](#return-value)4342[More info.](execution.md#globalshared-options)44Same as [`execa()`](#execafile-arguments-options) but using the [`node: true`](#optionsnode) option.45Executes a Node.js file using `node scriptPath ...arguments`.4647This is the preferred method when executing Node.js files.4849[More info.](node.md)5051### execaSync(file, arguments?, options?)45### execaSync\`command\`52### $.sync(file, arguments?, options?)53### $.s(file, arguments?, options?)5455`file`: `string | URL`\56`arguments`: `string[]`\57`options`: [`SyncOptions`](#options-1)\58_Returns_: [`SyncResult`](#return-value)5949Same as [`execa()`](#execafile-arguments-options) but synchronous.60Same as [`execa()`](#execafile-arguments-options) and [`$`](#file-arguments-options) but synchronous.6151Returns or throws a subprocess [`result`](#result). The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.62Returns a subprocess [`result`](#result) or throws an [`error`](#execasyncerror). The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.6364Those methods are discouraged as they hold the CPU and lack multiple features.6566[More info.](execution.md#synchronous-execution)6755### $(file, arguments?, options?)68### execa\`command\`69### $\`command\`70### execaNode\`command\`71### execaSync\`command\`72### $.sync\`command\`73### $.s\`command\`7457`file`: `string | URL`\58`arguments`: `string[]`\59`options`: [`Options`](#options)\60_Returns_: [`ResultPromise`](#return-value)75`command`: `string`\76_Returns_: [`ResultPromise`](#return-value), [`SyncResult`](#return-value)7762Same as [`execa()`](#execafile-arguments-options) but using [script-friendly default options](scripts.md#script-files).78Same as [`execa()`](#execafile-arguments-options), [`$()`](#file-arguments-options), [`execaNode()`](#execanodescriptpath-arguments-options) and [`execaSync()`](#execasyncfile-arguments-options) but using a [template string](execution.md#template-string-syntax). `command` includes both the `file` and its `arguments`.7964Just like `execa()`, this can use the [template string syntax](execution.md#template-string-syntax) or [bind options](execution.md#globalshared-options). It can also be [run synchronously](#execasyncfile-arguments-options) using `$.sync()` or `$.s()`.80More info on the [syntax](execution.md#template-string-syntax) and [escaping](escaping.md#template-string-syntax).8166This is the preferred method when executing multiple commands in a script file.82### execa(options)\`command\`83### $(options)\`command\`84### execaNode(options)\`command\`85### execaSync(options)\`command\`86### $.sync(options)\`command\`87### $.s(options)\`command\`8868[More info.](scripts.md)89`command`: `string`\90`options`: [`Options`](#options-1), [`SyncOptions`](#options-1)\91_Returns_: [`ResultPromise`](#return-value), [`SyncResult`](#return-value)9270### execaNode(scriptPath, arguments?, options?)93Same as [```execa`command` ```](#execacommand) but with [options](#options-1).9472`scriptPath`: `string | URL`\73`arguments`: `string[]`\74`options`: [`Options`](#options)\75_Returns_: [`ResultPromise`](#return-value)95[More info.](execution.md#template-string-syntax)9677Same as [`execa()`](#execafile-arguments-options) but using the [`node: true`](#optionsnode) option.78Executes a Node.js file using `node scriptPath ...arguments`.97### execa(options)98### $(options)99### execaNode(options)100### execaSync(options)101### $.sync(options)102### $.s(options)10380Just like `execa()`, this can use the [template string syntax](execution.md#template-string-syntax) or [bind options](execution.md#globalshared-options).104`options`: [`Options`](#options-1), [`SyncOptions`](#options-1)\105_Returns_: [`ExecaMethod`](#execafile-arguments-options), [`ExecaScriptMethod`](#file-arguments-options), [`ExecaNodeMethod`](#execanodescriptpath-arguments-options), [`ExecaSyncMethod`](#execasyncfile-arguments-options), [`ExecaScriptSyncMethod`](#syncfile-arguments-options)10682This is the preferred method when executing Node.js files.107Returns a new instance of those methods but with different default [`options`](#options-1). Consecutive calls are merged to previous ones.10884[More info.](node.md)109[More info.](execution.md#globalshared-options)110111### parseCommandString(command)112113`command`: `string`\
131 unmodified lines
245### subprocess.pipe(file, arguments?, options?)246247`file`: `string | URL`\248`arguments`: `string[]`\224`options`: [`Options`](#options) and [`PipeOptions`](#pipeoptions)\249`options`: [`Options`](#options-1) and [`PipeOptions`](#pipeoptions)\250_Returns_: [`Promise<Result>`](#result)251252[Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the subprocess' [`stdout`](#subprocessstdout) to a second Execa subprocess' [`stdin`](#subprocessstdin). This resolves with that second subprocess' [result](#result). If either subprocess is rejected, this is rejected with that subprocess' [error](#execaerror) instead.253229This follows the same syntax as [`execa(file, arguments?, options?)`](#execafile-arguments-options) except both [regular options](#options) and [pipe-specific options](#pipeoptions) can be specified.254This follows the same syntax as [`execa(file, arguments?, options?)`](#execafile-arguments-options) except both [regular options](#options-1) and [pipe-specific options](#pipeoptions) can be specified.255256[More info.](pipe.md#array-syntax)257258### subprocess.pipe\`command\`259### subprocess.pipe(options)\`command\`260261`command`: `string`\237`options`: [`Options`](#options) and [`PipeOptions`](#pipeoptions)\262`options`: [`Options`](#options-1) and [`PipeOptions`](#pipeoptions)\263_Returns_: [`Promise<Result>`](#result)264265Like [`subprocess.pipe(file, arguments?, options?)`](#subprocesspipefile-arguments-options) but using a [`command` template string](scripts.md#piping-stdout-to-another-command) instead. This follows the same syntax as `execa` [template strings](execution.md#template-string-syntax).266
961 unmodified lines
modified:docs/errors.md+2 added-2 removed
modified:docs/execution.md+2 added-2 removed
98 unmodified lines
99```100101## Options102103[Options](api.md#options) can be passed to influence the execution's behavior.103[Options](api.md#options-1) can be passed to influence the execution's behavior.104105### Array syntax106107```js
35 unmodified lines
143```144145### Synchronous execution146147[Every method](api.md#methods) can be called synchronously by appending `Sync` to the method's name. The [`result`](api.md#result) is returned without needing to `await`. The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.147[`execaSync()`](api.md#execasyncfile-arguments-options) and [`$.sync()`](api.md#syncfile-arguments-options) return the [`result`](api.md#result) without needing to `await`. The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.148149```js150import {execaSync} from 'execa';151
18 unmodified lines
modified:docs/pipe.md+1 added-1 removed
33 unmodified lines
34```3536## Options3738[Options](api.md#options) can be passed to either the source or the destination subprocess. Some [pipe-specific options](api.md#pipeoptions) can also be set by the destination subprocess.38[Options](api.md#options-1) can be passed to either the source or the destination subprocess. Some [pipe-specific options](api.md#pipeoptions) can also be set by the destination subprocess.3940```js41const {stdout} = await execa('npm', ['run', 'build'], subprocessOptions)42 .pipe('head', ['-n', '2'], subprocessOrPipeOptions);
125 unmodified lines
modified:docs/scripts.md+1 added-1 removed
modified:docs/typescript.md+4 added-4 removed
6 unmodified lines
7# 🤓 TypeScript89## Available types1011The following types can be imported: [`ResultPromise`](api.md#return-value), [`Subprocess`](api.md#subprocess), [`Result`](api.md#result), [`ExecaError`](api.md#execaerror), [`Options`](api.md#options), [`StdinOption`](api.md#optionsstdin), [`StdoutStderrOption`](api.md#optionsstdout), [`TemplateExpression`](api.md#execacommand) and [`Message`](api.md#subprocesssendmessagemessage-sendmessageoptions).11The following types can be imported: [`ResultPromise`](api.md#return-value), [`Subprocess`](api.md#subprocess), [`Result`](api.md#result), [`ExecaError`](api.md#execaerror), [`Options`](api.md#options-1), [`StdinOption`](api.md#optionsstdin), [`StdoutStderrOption`](api.md#optionsstdout), [`TemplateExpression`](api.md#execacommand) and [`Message`](api.md#subprocesssendmessagemessage-sendmessageoptions).1213```ts14import {15 execa,
18 unmodified lines
34const message: Message = 'hello world';3536try {37 const subprocess: ResultPromise = execa(options)`npm run ${task}`;38 await subprocess.sendMessage(message);38 await subprocess.sendMessage?.(message);39 const result: Result = await subprocess;40 console.log(result.stdout);41} catch (error) {42 if (error instanceof ExecaError) {
3 unmodified lines
46```4748## Synchronous execution4950Their [synchronous](#synchronous-execution) counterparts are [`SyncResult`](api.md#result), [`ExecaSyncError`](api.md#execasyncerror), [`SyncOptions`](api.md#options), [`StdinSyncOption`](api.md#optionsstdin), [`StdoutStderrSyncOption`](api.md#optionsstdout) and [`TemplateExpression`](api.md#execacommand).50Their [synchronous](#synchronous-execution) counterparts are [`SyncResult`](api.md#result), [`ExecaSyncError`](api.md#execasyncerror), [`SyncOptions`](api.md#options-1), [`StdinSyncOption`](api.md#optionsstdin), [`StdoutStderrSyncOption`](api.md#optionsstdout) and [`TemplateExpression`](api.md#execacommand).5152```ts53import {54 execaSync,
83 unmodified lines
138```139140### Strict unions141142Several options are typed as unions. For example, the [`serialization`](api.md#optionsserialization) option's type is `'advanced' | 'json'`, not `string`. Therefore the following example fails:142Several options are typed as unions of strings: [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout), [`stderr`](api.md#optionsstderr), [`encoding`](api.md#optionsencoding), [`serialization`](api.md#optionsserialization), [`verbose`](api.md#optionsverbose), [`killSignal`](api.md#optionskillsignal), [`from`](api.md#pipeoptionsfrom) and [`to`](api.md#pipeoptionsto). For example, the `serialization` option's type is `'advanced' | 'json'`, not `string`. Therefore the following example fails:143144```ts145import {execa} from 'execa';146
25 unmodified lines
modified:types/methods/command.d.ts+9 added-3 removed
4 unmodified lines
56/**7Executes a command. `command` is a string that includes both the `file` and its `arguments`.89This is only intended for very specific cases, such as a REPL. This should be avoided otherwise.9When `command` is a template string, it includes both the `file` and its `arguments`.1011Just like `execa()`, this can bind options. It can also be run synchronously using `execaCommandSync()`.11`execaCommand(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.1213This is only intended for very specific cases, such as a REPL. This should be avoided otherwise.1415@param command - The program/script to execute and its arguments.16@returns A `ResultPromise` that is both:17- the subprocess.18- a `Promise` either resolving with its successful `result`, or rejecting with its `error`.
32 unmodified lines
5152/**53Same as `execaCommand()` but synchronous.5453Returns or throws a subprocess `result`. The `subprocess` is not returned: its methods and properties are not available.55When `command` is a template string, it includes both the `file` and its `arguments`.5657`execaCommandSync(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.5859Returns a subprocess `result` or throws an `error`. The `subprocess` is not returned: its methods and properties are not available.6061@param command - The program/script to execute and its arguments.62@returns `SyncResult`63@throws `ExecaSyncError`64
50 unmodified lines
modified:types/methods/main-async.d.ts+1 added-1 removed
5 unmodified lines
6Executes a command using `file ...arguments`.78When `command` is a template string, it includes both the `file` and its `arguments`.910`execa(options)` can be used to return a new instance of Execa but with different default `options`. Consecutive calls are merged to previous ones.10`execa(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.1112@param file - The program/script to execute, as a string or file URL13@param arguments - Arguments to pass to `file` on execution.14@returns A `ResultPromise` that is both:
337 unmodified lines
modified:types/methods/main-sync.d.ts+7 added-1 removed
3 unmodified lines
45/**6Same as `execa()` but synchronous.78Returns or throws a subprocess `result`. The `subprocess` is not returned: its methods and properties are not available.8Returns a subprocess `result` or throws an `error`. The `subprocess` is not returned: its methods and properties are not available.910When `command` is a template string, it includes both the `file` and its `arguments`.1112`execaSync(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.1314This method is discouraged as it holds the CPU and lacks multiple features.1516@param file - The program/script to execute, as a string or file URL17@param arguments - Arguments to pass to `file` on execution.18@returns `SyncResult`19@throws `ExecaSyncError`
40 unmodified lines
modified:types/methods/node.d.ts+3 added-1 removed
4 unmodified lines
5/**6Same as `execa()` but using the `node: true` option.7Executes a Node.js file using `node scriptPath ...arguments`.89Just like `execa()`, this can use the template string syntax or bind options.9When `command` is a template string, it includes both the `file` and its `arguments`.1011`execaNode(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.1213This is the preferred method when executing Node.js files.1415@param scriptPath - Node.js script to execute, as a string or file URL16@param arguments - Arguments to pass to `scriptPath` on execution.
46 unmodified lines
modified:types/methods/script.d.ts+3 added-1 removed
9 unmodified lines
1011/**12Same as `execa()` but using script-friendly default options.1314Just like `execa()`, this can use the template string syntax or bind options. It can also be run synchronously using `$.sync()` or `$.s()`.14When `command` is a template string, it includes both the `file` and its `arguments`.1516`$(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.1718This is the preferred method when executing multiple commands in a script file.1920@returns A `ResultPromise` that is both:21- the subprocess.
94 unmodified lines