Skip to content
Myr

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

1 file+134 added−48 removed

modified:docs/scripts.md+134 added-48 removed
1# Node.js scripts23With 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).3With 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.1011```js12import {$} from 'execa';13
13 unmodified lines
27const dirName = 'foo bar';28await $`mkdir /tmp/${dirName}`;29```3025## Summary31## Examples3227This file describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).2829### Flexibility3031Unlike 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).3233### Shell3435The 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.3637This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.3839Also, there is no shell syntax to remember: everything is just plain JavaScript.4041If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.4243### Simplicity4445Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).4647[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.4849Execa 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.5051### Modularity5253zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.5455Execa 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.5657### Performance5859Spawning a shell for every command comes at a performance cost, which Execa avoids.6061Also, [local binaries](#local-binaries) can be directly executed without using `npx`.6263### Debugging6465Child processes can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).6667Also, Execa's error messages and [properties](#errors) are very detailed to make it clear to determine why a process failed.6869Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.7071## Details7233### Main binary3435```sh36# Bash
622 unmodified lines
659// Execa660await $({detached: true})`echo one`;661```662663### IPC664665```sh666# Bash does not allow simple IPC667```668669```js670// zx does not allow simple IPC671```672673```js674// Execa675const childProcess = $({ipc: true})`node script.js`;676677childProcess.on('message', message => {678 if (message === 'ping') {679 childProcess.send('pong');680 }681});682```683684### Transforms685686```sh687# Bash does not allow transforms688```689690```js691// zx does not allow transforms692```693694```js695// Execa696const transform = function * (line) {697 if (!line.includes('secret')) {698 yield line;699 }700};701702await $({stdout: [transform, 'inherit']})`echo ${'This is a secret.'}`;703```704705### Cancellation706707```sh708# Bash709kill $PID710```711712```js713// zx714childProcess.kill();715```716717```js718// Execa719// Can specify an error message and stack trace720childProcess.kill(error);721722// Or use an `AbortSignal`723const controller = new AbortController();724await $({signal: controller.signal})`node long-script.js`;725```726727### Interleaved output728729```sh730# Bash prints stdout and stderr interleaved731```732733```js734// zx separates stdout and stderr735const {stdout, stderr} = await $`node example.js`;736```737738```js739// Execa can interleave stdout and stderr740const {all} = await $({all: true})`node example.js`;741```742743### PID744745```sh746# Bash
8 unmodified lines
755```js756// Execa757const {pid} = $`echo example`;758```759760## Differences with Bash and zx761762This section describes the differences between Bash, Execa, and [zx](https://github.com/google/zx) (which inspired this feature).763764### Flexibility765766Unlike 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).767768### Shell769770The 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.771772This is more cross-platform. For example, your code works the same on Windows machines without Bash installed.773774Also, there is no shell syntax to remember: everything is just plain JavaScript.775776If you really need a shell though, the [`shell` option](../readme.md#shell) can be used.777778### Simplicity779780Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).781782[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.783784Execa 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.785786### Modularity787788zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.789790Execa 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.791792### Performance793794Spawning a shell for every command comes at a performance cost, which Execa avoids.795796Also, [local binaries](#local-binaries) can be directly executed without using `npx`.797798### Debugging799800Child processes can be hard to debug, which is why Execa includes a [`verbose` option](#verbose-mode).801802Also, 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).803804Finally, unlike Bash and zx, which are stateful (options, current directory, etc.), Execa is [purely functional](#current-directory), which also helps with debugging.