Skip to content
Myr

input.md

This file

Path

docs/input.md

155 lines · 5.9 KiB · Markdown

Last changed by

Fix documentation and types mistakesSindre Sorhusbefore adoption74b854a

History of this file

Take it

The walk stopped after 200 commits: lines older than 0a13f2b point at it.

Fix the type of the return value (#1007)ehmickybefore adoption0a13f2b

lines 1–36why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# 🎹 Input89## Command arguments1011The simplest way to pass input to a subprocess is to use command arguments.1213```js14import {execa} from 'execa';1516const commandArgument = 'build';17await execa`node child.js ${commandArgument}`;18```1920If the subprocess is a Node.js file, those are available using [`process.argv`](https://nodejs.org/api/process.html#processargv).2122```js23// child.js24import process from 'node:process';2526const commandArgument = process.argv[2];27```2829## Environment variables3031Unlike [command arguments](#command-arguments), [environment variables](https://en.wikipedia.org/wiki/Environment_variable) have names. They are commonly used to configure applications.3233If the subprocess spawns its own subprocesses, they inherit environment variables. To isolate subprocesses from each other, either command arguments or [`stdin`](#string-input) should be preferred instead.3435```js36// Keep the current process' environment variables, and set `NO_COLOR`

Update input.md (#1145)Jim Higsonbefore adoptiond99a52a

line 37why, one step back
37await execa({env: {NO_COLOR: 'true'}})`node child.js`;

Fix the type of the return value (#1007)ehmickybefore adoption0a13f2b

line 38why, one step back
38// Discard the current process' environment variables, only pass `NO_COLOR`

Update input.md (#1145)Jim Higsonbefore adoptiond99a52a

line 39why, one step back
39await execa({env: {NO_COLOR: 'true'}, extendEnv: false})`node child.js`;

Fix the type of the return value (#1007)ehmickybefore adoption0a13f2b

lines 40–70why, one step back
40```
4142If the subprocess is a Node.js file, environment variables are available using [`process.env`](https://nodejs.org/api/process.html#processenv).4344```js45// child.js46import process from 'node:process';4748console.log(process.env.NO_COLOR);49```5051## String input5253Alternatively, input can be provided to [`stdin`](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). Unlike [command arguments](#command-arguments) and [environment variables](#environment-variables) which have [size](https://unix.stackexchange.com/questions/120642/what-defines-the-maximum-size-for-a-command-single-argument) [limits](https://stackoverflow.com/questions/1078031/what-is-the-maximum-size-of-a-linux-environment-variable-value), `stdin` works when the input is big. Also, the input can be redirected from the [terminal](#terminal-input), a [file](#file-input), another [subprocess](pipe.md) or a [stream](streams.md#manual-streaming). Finally, this is required when the input might contain [null bytes](https://en.wikipedia.org/wiki/Null_character), for example when it might be [binary](binary.md#binary-input).5455If the input is already available as a string, it can be passed directly to the [`input`](api.md#optionsinput) option.5657```js58await execa({input: 'stdinInput'})`npm run scaffold`;59```6061The [`stdin`](api.md#optionsstdin) option can also be used, although the string must be wrapped in two arrays for [syntax reasons](output.md#multiple-targets).6263```js64await execa({stdin: [['stdinInput']]})`npm run scaffold`;65```6667## Ignore input6869```js70const subprocess = execa({stdin: 'ignore'})`npm run scaffold`;

Fix documentation and types mistakesSindre Sorhusbefore adoption74b854a

line 71why, one step back
71console.log(subprocess.stdin); // null

Fix the type of the return value (#1007)ehmickybefore adoption0a13f2b

lines 72–92why, one step back
72await subprocess;
73```7475## File input7677```js78await execa({inputFile: 'input.txt'})`npm run scaffold`;79// Or:80await execa({stdin: {file: 'input.txt'}})`npm run scaffold`;81// Or:82await execa({stdin: new URL('file:///path/to/input.txt')})`npm run scaffold`;83```8485## Terminal input8687The parent process' input can be re-used in the subprocess by passing `'inherit'`. This is especially useful to receive interactive input in command line applications.8889```js90await execa({stdin: 'inherit'})`npm run scaffold`;91```92

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 93–94why, one step back
93## Any input type
94

Add `filter` option to `getOneMessage()` (#1076)ehmickybefore adoption571e052

line 95why, one step back
95If the subprocess [uses Node.js](node.md), [almost any type](ipc.md#message-type) can be passed to the subprocess using the [`ipcInput`](ipc.md#send-an-initial-message) option. The subprocess retrieves that input using [`getOneMessage()`](api.md#getonemessagegetonemessageoptions).

Add `ipcInput` option (#1068)ehmickybefore adoption46cae30

lines 96–114why, one step back
96
97```js98// main.js99import {execaNode} from 'execa';100101const ipcInput = [102 {task: 'lint', ignore: /test\.js/},103 {task: 'copy', files: new Set(['main.js', 'index.js']),104}];105await execaNode({ipcInput})`build.js`;106```107108```js109// build.js110import {getOneMessage} from 'execa';111112const ipcInput = await getOneMessage();113```114

Document support for multiple inputs (#1222)ehmickybefore adoption6ad1a6e

lines 115–126why, one step back
115## Multiple inputs
116117The input can come from multiple sources by setting the [`stdin`](api.md#optionsstdin) option to an array of values. Alternatively the [`input`](api.md#optionsinput), [`inputFile`](api.md#optionsinputfile) and [`stdin`](api.md#optionsstdin) options can be combined.118119The following example redirects `stdin` from both the [terminal](#terminal-input) and an `input.txt` [file](#file-input).120121```js122await execa({stdin: ['inherit', {file: 'input.txt'}]})`npm run scaffold`;123```124125__Loss of TTY control:__ Please note that when a file descriptor is configured with a combination of `'inherit'` and other values, this file descriptor will never refer to a TTY in the subprocess, even if in the current process it does.126

Fix the type of the return value (#1007)ehmickybefore adoption0a13f2b

lines 127–137why, one step back
127## Additional file descriptors
128129The [`stdio`](api.md#optionsstdio) option can be used to pass some input to any [file descriptor](https://en.wikipedia.org/wiki/File_descriptor), as opposed to only [`stdin`](api.md#optionsstdin).130131```js132// Pass input to the file descriptor number 3133await execa({134 stdio: ['pipe', 'pipe', 'pipe', new Uint8Array([/* ... */])],135})`npm run build`;136```137

Generalize `{value, input}` to any direction-ambiguous stdio value (#1249)Sindre Sorhusbefore adoption9fbf5ba

line 138why, one step back
138Some values have an ambiguous direction on an additional file descriptor: Execa cannot tell whether they are meant for input or output, so they default to output. This applies to `'pipe'`, `'inherit'`, [files](output.md#file-output) and [transforms](transform.md). To use one for input instead, such as when [streaming](streams.md#different-file-descriptor) to it, wrap it as `{value, input: true}`.

Add `{value: 'pipe', input: true}` to use an additional file descriptor as input (#1246)Sindre Sorhusbefore adoption487a3a4

lines 139–150why, one step back
139
140```js141import {pipeline} from 'node:stream/promises';142143// Stream input to the file descriptor number 3144const subprocess = execa({145 stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}],146})`npm run build`;147148await pipeline(inputStream, subprocess.writable({to: 'fd3'}));149```150

Fix the type of the return value (#1007)ehmickybefore adoption0a13f2b

lines 151–155why, one step back
151<hr>
152153[**Next**: 📢 Output](output.md)\154[**Previous**: 🏁 Termination](termination.md)\155[**Top**: Table of contents](../readme.md#documentation)