Improve documentation of `$` (#888)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 209f78f · Browse files · 1 file·+134 added−48 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+134 added−48 removed
- other:+134 added−48 removed
Provenance
- Commit
a7f8598- Tree
0f33885- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
209f78f
1 file changed+134 added−48 removed
modified:docs/scripts.md+134 added-48 removed
1 # Node.js scripts2 3-With Execa, you can write scripts with Node.js instead of a shell language. It is [secure](#escaping), [performant](#performance), [simple](#simplicity) and [cross-platform](#shell).3+With Execa, you can write scripts with Node.js instead of a shell language. [Compared to Bash and zx](#differences-with-bash-and-zx), this is more:4+ - [performant](#performance)5+ - [cross-platform](#shell): [no shell](../readme.md#shell-syntax) is used, only JavaScript.6+ - [secure](#escaping): no shell injection.7+ - [simple](#simplicity): minimalistic API, no [globals](#global-variables), no [binary](#main-binary), no [builtin CLI utilities](#builtin-utilities).8+ - [featureful](#simplicity): all Execa features are available ([process piping](#piping-stdout-to-another-command), [IPC](#ipc), [transforms](#transforms), [background processes](#background-processes), [cancellation](#cancellation), [local binaries](#local-binaries), [cleanup on exit](../readme.md#cleanup), [interleaved output](#interleaved-output), [forceful termination](../readme.md#forcekillafterdelay), etc.).9+ - [easy to debug](#debugging): [verbose mode](#verbose-mode), [detailed errors](#errors), [messages and stack traces](#cancellation), stateless API.10 11 ```js12 import {$} from 'execa';13 13 unmodified lines27 const dirName = 'foo bar';28 await $`mkdir /tmp/${dirName}`;29 ```30 25-## Summary31+## Examples32 27-This file describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).28-29-### Flexibility30-31-Unlike shell languages like Bash, libraries like Execa and zx enable you to write scripts with a more featureful programming language (JavaScript). This allows complex logic (such as [parallel execution](#parallel-commands)) to be expressed easily. This also lets you use [any Node.js package](#builtin-utilities).32-33-### Shell34-35-The main difference between Execa and zx is that Execa does not require any shell. Shell-specific keywords and features are [written in JavaScript](#variable-substitution) instead.36-37-This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.38-39-Also, there is no shell syntax to remember: everything is just plain JavaScript.40-41-If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.42-43-### Simplicity44-45-Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).46-47-[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.48-49-Execa is a thin wrapper around the core Node.js [`child_process` module](https://nodejs.org/api/child_process.html). Unlike zx, it lets you use [any of its native features](#background-processes): [`pid`](#pid), [IPC](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback), [`unref()`](https://nodejs.org/api/child_process.html#subprocessunref), [`detached`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`uid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`gid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`cancelSignal`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), etc.50-51-### Modularity52-53-zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.54-55-Execa does not include [any utility](#builtin-utilities): it focuses on being small and modular instead. Any Node.js package can be used in your scripts.56-57-### Performance58-59-Spawning a shell for every command comes at a performance cost, which Execa avoids.60-61-Also, [local binaries](#local-binaries) can be directly executed without using `npx`.62-63-### Debugging64-65-Child processes can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).66-67-Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a process failed.68-69-Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.70-71-## Details72-33 ### Main binary34 35 ```sh36 # Bash622 unmodified lines659 // Execa660 await $({detached: true})`echo one`;661 ```662 663+### IPC664+665+```sh666+# Bash does not allow simple IPC667+```668+669+```js670+// zx does not allow simple IPC671+```672+673+```js674+// Execa675+const childProcess = $({ipc: true})`node script.js`;676+677+childProcess.on('message', message => {678+ if (message === 'ping') {679+ childProcess.send('pong');680+ }681+});682+```683+684+### Transforms685+686+```sh687+# Bash does not allow transforms688+```689+690+```js691+// zx does not allow transforms692+```693+694+```js695+// Execa696+const transform = function * (line) {697+ if (!line.includes('secret')) {698+ yield line;699+ }700+};701+702+await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;703+```704+705+### Cancellation706+707+```sh708+# Bash709+kill $PID710+```711+712+```js713+// zx714+childProcess.kill();715+```716+717+```js718+// Execa719+// Can specify an error message and stack trace720+childProcess.kill(error);721+722+// Or use an `AbortSignal`723+const controller = new AbortController();724+await $({signal: controller.signal})`node long-script.js`;725+```726+727+### Interleaved output728+729+```sh730+# Bash prints stdout and stderr interleaved731+```732+733+```js734+// zx separates stdout and stderr735+const {stdout, stderr} = await $`node example.js`;736+```737+738+```js739+// Execa can interleave stdout and stderr740+const {all} = await $({all: true})`node example.js`;741+```742+743 ### PID744 745 ```sh746 # Bash8 unmodified lines755 ```js756 // Execa757 const {pid} = $`echo example`;758 ```759+760+## Differences with Bash and zx761+762+This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).763+764+### Flexibility765+766+Unlike shell languages like Bash, libraries like Execa and zx enable you to write scripts with a more featureful programming language (JavaScript). This allows complex logic (such as [parallel execution](#parallel-commands)) to be expressed easily. This also lets you use [any Node.js package](#builtin-utilities).767+768+### Shell769+770+The main difference between Execa and zx is that Execa does not require any shell. Shell-specific keywords and features are [written in JavaScript](#variable-substitution) instead.771+772+This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.773+774+Also, there is no shell syntax to remember: everything is just plain JavaScript.775+776+If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.777+778+### Simplicity779+780+Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).781+782+[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.783+784+Execa is a thin wrapper around the core Node.js [`child_process` module](https://nodejs.org/api/child_process.html). Unlike zx, it lets you use [any of its native features](#background-processes): [`pid`](#pid), [IPC](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback), [`unref()`](https://nodejs.org/api/child_process.html#subprocessunref), [`detached`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`uid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`gid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`cancelSignal`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), etc.785+786+### Modularity787+788+zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.789+790+Execa does not include [any utility](#builtin-utilities): it focuses on being small and modular instead. Any Node.js package can be used in your scripts.791+792+### Performance793+794+Spawning a shell for every command comes at a performance cost, which Execa avoids.795+796+Also, [local binaries](#local-binaries) can be directly executed without using `npx`.797+798+### Debugging799+800+Child processes can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).801+802+Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a process failed. Error messages and stack traces can be set with [`childProcess.kill(error)`](../readme.md#killerror).803+804+Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.