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
Provenance
- Commit
ca2e813- Tree
8d64661- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
faab26e
11 files changed+96 added−55 removed
modified:docs/api.md+63 added-38 removed
4 unmodified lines5 <br>6 7 # 📔 API reference8 9-This 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.9+This 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.10 11 ## Methods12 13 ### execa(file, arguments?, options?)14 15 `file`: `string | URL`\16 `arguments`: `string[]`\17-`options`: [`Options`](#options)\17+`options`: [`Options`](#options-1)\18 _Returns_: [`ResultPromise`](#return-value)19 20 Executes a command using `file ...arguments`.21 22 More info on the [syntax](execution.md#array-syntax) and [escaping](escaping.md#array-syntax).23 24-### execa\`command\`25-### execa(options)\`command\`24+### $(file, arguments?, options?)25 27-`command`: `string`\28-`options`: [`Options`](#options)\26+`file`: `string | URL`\27+`arguments`: `string[]`\28+`options`: [`Options`](#options-1)\29 _Returns_: [`ResultPromise`](#return-value)30 31-Executes a command. `command` is a [template string](execution.md#template-string-syntax) that includes both the `file` and its `arguments`.31+Same as [`execa()`](#execafile-arguments-options) but using [script-friendly default options](scripts.md#script-files).32 33-More info on the [syntax](execution.md#template-string-syntax) and [escaping](escaping.md#template-string-syntax).33+This is the preferred method when executing multiple commands in a script file.34 35-### execa(options)35+[More info.](scripts.md)36 37-`options`: [`Options`](#options)\38-_Returns_: [`execa`](#execafile-arguments-options)37+### execaNode(scriptPath, arguments?, options?)38 40-Returns 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)43 42-[More info.](execution.md#globalshared-options)44+Same as [`execa()`](#execafile-arguments-options) but using the [`node: true`](#optionsnode) option.45+Executes a Node.js file using `node scriptPath ...arguments`.46 47+This is the preferred method when executing Node.js files.48+49+[More info.](node.md)50+51 ### execaSync(file, arguments?, options?)45-### execaSync\`command\`52+### $.sync(file, arguments?, options?)53+### $.s(file, arguments?, options?)54 55+`file`: `string | URL`\56+`arguments`: `string[]`\57+`options`: [`SyncOptions`](#options-1)\58 _Returns_: [`SyncResult`](#return-value)59 49-Same as [`execa()`](#execafile-arguments-options) but synchronous.60+Same as [`execa()`](#execafile-arguments-options) and [`$`](#file-arguments-options) but synchronous.61 51-Returns or throws a subprocess [`result`](#result). The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.62+Returns a subprocess [`result`](#result) or throws an [`error`](#execasyncerror). The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.63 64+Those methods are discouraged as they hold the CPU and lack multiple features.65+66 [More info.](execution.md#synchronous-execution)67 55-### $(file, arguments?, options?)68+### execa\`command\`69+### $\`command\`70+### execaNode\`command\`71+### execaSync\`command\`72+### $.sync\`command\`73+### $.s\`command\`74 57-`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)77 62-Same as [`execa()`](#execafile-arguments-options) but using [script-friendly default options](scripts.md#script-files).78+Same 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`.79 64-Just 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()`.80+More info on the [syntax](execution.md#template-string-syntax) and [escaping](escaping.md#template-string-syntax).81 66-This 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\`88 68-[More info.](scripts.md)89+`command`: `string`\90+`options`: [`Options`](#options-1), [`SyncOptions`](#options-1)\91+_Returns_: [`ResultPromise`](#return-value), [`SyncResult`](#return-value)92 70-### execaNode(scriptPath, arguments?, options?)93+Same as [```execa`command` ```](#execacommand) but with [options](#options-1).94 72-`scriptPath`: `string | URL`\73-`arguments`: `string[]`\74-`options`: [`Options`](#options)\75-_Returns_: [`ResultPromise`](#return-value)95+[More info.](execution.md#template-string-syntax)96 77-Same as [`execa()`](#execafile-arguments-options) but using the [`node: true`](#optionsnode) option.78-Executes 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)103 80-Just 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)106 82-This is the preferred method when executing Node.js files.107+Returns a new instance of those methods but with different default [`options`](#options-1). Consecutive calls are merged to previous ones.108 84-[More info.](node.md)109+[More info.](execution.md#globalshared-options)110 111 ### parseCommandString(command)112 113 `command`: `string`\131 unmodified lines245 ### subprocess.pipe(file, arguments?, options?)246 247 `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)251 252 [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.253 229-This 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.254+This 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.255 256 [More info.](pipe.md#array-syntax)257 258 ### subprocess.pipe\`command\`259 ### subprocess.pipe(options)\`command\`260 261 `command`: `string`\237-`options`: [`Options`](#options) and [`PipeOptions`](#pipeoptions)\262+`options`: [`Options`](#options-1) and [`PipeOptions`](#pipeoptions)\263 _Returns_: [`Promise<Result>`](#result)264 265 Like [`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 linesmodified:docs/errors.md+2 added-2 removed
57 unmodified lines5859606162626364656614 unmodified lines8182838485858687888921 unmodified lines57 unmodified lines
Otherwise, the subprocess failed because either:- An exception was thrown in a [stream](streams.md) or [transform](transform.md).- The command's executable file was not found.- An invalid [option](api.md#options) was passed.- An invalid [option](api.md#options-1) was passed.- There was not enough memory or too many subprocesses.
```jstry {14 unmodified lines- Its [`stdout`, `stderr`](output.md#stdout-and-stderr), [other file descriptors'](output.md#additional-file-descriptors) output and [IPC messages](ipc.md), separated with newlines and not [interleaved](output.md#interleaved-output).
[`error.shortMessage`](api.md#errorshortmessage) is the same but without `stdout`, `stderr` nor IPC messages.
[`error.originalMessage`](api.md#errororiginalmessage) is the same but also without the command. This exists only in specific instances, such as when calling [`subprocess.kill(error)`](termination.md#error-message-and-stack-trace), using the [`cancelSignal`](termination.md#canceling) option, passing an invalid command or [option](api.md#options), or throwing an exception in a [stream](streams.md) or [transform](transform.md).[`error.originalMessage`](api.md#errororiginalmessage) is the same but also without the command. This exists only in specific instances, such as when calling [`subprocess.kill(error)`](termination.md#error-message-and-stack-trace), using the [`cancelSignal`](termination.md#canceling) option, passing an invalid command or [option](api.md#options-1), or throwing an exception in a [stream](streams.md) or [transform](transform.md).
```jstry { await execa`npm run build`;21 unmodified linesmodified:docs/execution.md+2 added-2 removed
98 unmodified lines99 ```100 101 ## Options102 103-[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.104 105 ### Array syntax106 107 ```js35 unmodified lines143 ```144 145 ### Synchronous execution146 147-[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.148 149 ```js150 import {execaSync} from 'execa';151 18 unmodified linesmodified:docs/pipe.md+1 added-1 removed
33 unmodified lines34 ```35 36 ## Options37 38-[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.39 40 ```js41 const {stdout} = await execa('npm', ['run', 'build'], subprocessOptions)42 .pipe('head', ['-n', '2'], subprocessOrPipeOptions);125 unmodified linesmodified:docs/scripts.md+1 added-1 removed
33 unmodified lines3435363738383940414211 unmodified lines33 unmodified lines```
## Template string syntax
Just like [`execa`](api.md#execafile-arguments-options), [`$`](api.md#file-arguments-options) can use either the [template string syntax](execution.md#template-string-syntax) or the [array syntax](execution.md#array-syntax).Just like [`execa`](api.md#execacommand), [`$`](api.md#command) can use either the [template string syntax](execution.md#template-string-syntax) or the [array syntax](execution.md#array-syntax).
Conversely, the template string syntax can be used outside of script files: `$` is not required to use that syntax. For example, `execa` [can use it too](execution.md#template-string-syntax).
```js11 unmodified linesmodified:docs/typescript.md+4 added-4 removed
6 unmodified lines7 # 🤓 TypeScript8 9 ## Available types10 11-The 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).11+The 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).12 13 ```ts14 import {15 execa,18 unmodified lines34 const message: Message = 'hello world';35 36 try {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 lines46 ```47 48 ## Synchronous execution49 50-Their [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).50+Their [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).51 52 ```ts53 import {54 execaSync,83 unmodified lines138 ```139 140 ### Strict unions141 142-Several 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:142+Several 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:143 144 ```ts145 import {execa} from 'execa';146 25 unmodified linesmodified:types/methods/command.d.ts+9 added-3 removed
4 unmodified lines5 6 /**7 Executes a command. `command` is a string that includes both the `file` and its `arguments`.8 9-This is only intended for very specific cases, such as a REPL. This should be avoided otherwise.9+When `command` is a template string, it includes both the `file` and its `arguments`.10 11-Just 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.12 13+This is only intended for very specific cases, such as a REPL. This should be avoided otherwise.14+15 @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 lines51 52 /**53 Same as `execaCommand()` but synchronous.54 53-Returns or throws a subprocess `result`. The `subprocess` is not returned: its methods and properties are not available.55+When `command` is a template string, it includes both the `file` and its `arguments`.56 57+`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.58+59+Returns a subprocess `result` or throws an `error`. The `subprocess` is not returned: its methods and properties are not available.60+61 @param command - The program/script to execute and its arguments.62 @returns `SyncResult`63 @throws `ExecaSyncError`64 50 unmodified linesmodified:types/methods/main-async.d.ts+1 added-1 removed
5 unmodified lines6 Executes a command using `file ...arguments`.7 8 When `command` is a template string, it includes both the `file` and its `arguments`.9 10-`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.11 12 @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 linesmodified:types/methods/main-sync.d.ts+7 added-1 removed
3 unmodified lines4 5 /**6 Same as `execa()` but synchronous.7 8-Returns or throws a subprocess `result`. The `subprocess` is not returned: its methods and properties are not available.8+Returns a subprocess `result` or throws an `error`. The `subprocess` is not returned: its methods and properties are not available.9 10+When `command` is a template string, it includes both the `file` and its `arguments`.11+12+`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.13+14+This method is discouraged as it holds the CPU and lacks multiple features.15+16 @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 linesmodified:types/methods/node.d.ts+3 added-1 removed
4 unmodified lines5 /**6 Same as `execa()` but using the `node: true` option.7 Executes a Node.js file using `node scriptPath ...arguments`.8 9-Just like `execa()`, this can use the template string syntax or bind options.9+When `command` is a template string, it includes both the `file` and its `arguments`.10 11+`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.12+13 This is the preferred method when executing Node.js files.14 15 @param scriptPath - Node.js script to execute, as a string or file URL16 @param arguments - Arguments to pass to `scriptPath` on execution.46 unmodified linesmodified:types/methods/script.d.ts+3 added-1 removed
9 unmodified lines10 11 /**12 Same as `execa()` but using script-friendly default options.13 14-Just like `execa()`, this can use the template string syntax or bind options. It can also be run synchronously using `$.sync()` or `$.s()`.14+When `command` is a template string, it includes both the `file` and its `arguments`.15 16+`$(options)` can be used to return a new instance of this method but with different default `options`. Consecutive calls are merged to previous ones.17+18 This is the preferred method when executing multiple commands in a script file.19 20 @returns A `ResultPromise` that is both:21 - the subprocess.94 unmodified lines