Allow `stdin`/`stdout`/`stderr` to be an array (#643)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent d6b4553 · Browse files · 17 files·+620 added−43 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
17 files+620 added−43 removed
- tests:+336 added−1 removed
- other:+284 added−42 removed
Provenance
- Commit
7efd50c- Tree
8e129fa- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
d6b4553
17 files changed+620 added−43 removed
modified:lib/stdio/async.js+5 added-5 removed
5 unmodified lines67891011121011121314151615161718192026 unmodified lines5 unmodified linesexport const handleInputAsync = options => handleInput(addPropertiesAsync, options);
const addPropertiesAsync = { input: { filePath: ({value}) => ({value: createReadStream(value)}), webStream: ({value}) => ({value: Readable.fromWeb(value)}), iterable: ({value}) => ({value: Readable.from(value)}), filePath: ({value}) => ({value: createReadStream(value), autoDestroy: true}), webStream: ({value}) => ({value: Readable.fromWeb(value), autoDestroy: true}), iterable: ({value}) => ({value: Readable.from(value), autoDestroy: true}), }, output: { filePath: ({value}) => ({value: createWriteStream(value)}), webStream: ({value}) => ({value: Writable.fromWeb(value)}), filePath: ({value}) => ({value: createWriteStream(value), autoDestroy: true}), webStream: ({value}) => ({value: Writable.fromWeb(value), autoDestroy: true}), iterable({optionName}) { throw new TypeError(`The \`${optionName}\` option cannot be an iterable.`); }, },26 unmodified linesmodified:lib/stdio/direction.js+30 added-1 removed
1234567891011121013141516171819202122232425262728293031323334353610 unmodified lines47484950515253545556575859606162636465666768697071import process from 'node:process';import { isStream as isNodeStream, isReadableStream as isNodeReadableStream, isWritableStream as isNodeWritableStream,} from 'is-stream';import {isWritableStream} from './type.js';
// For `stdio[index]` beyond stdin/stdout/stderr, we need to guess whether the value passed is intended for inputs or outputs.// This allows us to know whether to pipe _into_ or _from_ the stream.// When `stdio[index]` is a single value, this guess is fairly straightforward.// However, when it is an array instead, we also need to make sure the different values are not incompatible with each other.export const addStreamDirection = stdioStream => {export const addStreamDirection = stdioStream => Array.isArray(stdioStream) ? addStreamArrayDirection(stdioStream) : addStreamSingleDirection(stdioStream);
const addStreamSingleDirection = stdioStream => { const direction = getStreamDirection(stdioStream); return addDirection(stdioStream, direction);};
const addStreamArrayDirection = stdioStream => { const directions = stdioStream.map(stdioStreamItem => getStreamDirection(stdioStreamItem));
if (directions.includes('input') && directions.includes('output')) { throw new TypeError(`The \`${stdioStream[0].optionName}\` option must not be an array of both readable and writable values.`); }
const direction = directions.find(Boolean); return stdioStream.map(stdioStreamItem => addDirection(stdioStreamItem, direction));};
const getStreamDirection = stdioStream => KNOWN_DIRECTIONS[stdioStream.index] ?? guessStreamDirection[stdioStream.type](stdioStream.value);
// `stdin`/`stdout`/`stderr` have a known directionconst KNOWN_DIRECTIONS = ['input', 'output', 'output'];10 unmodified lines
return 'output'; }, native(stdioOption) { if ([0, process.stdin].includes(stdioOption)) { return 'input'; }
if ([1, 2, process.stdout, process.stderr].includes(stdioOption)) { return 'output'; }
if (isNodeStream(stdioOption)) { return guessStreamDirection.nodeStream(stdioOption); } },};
const addDirection = (stdioStream, direction = DEFAULT_DIRECTION) => ({...stdioStream, direction});
// When ambiguous, we initially keep the direction as `undefined`.// This allows arrays of `stdio` values to resolve the ambiguity.// For example, `stdio[3]: DuplexStream` is ambiguous, but `stdio[3]: [DuplexStream, WritableStream]` is not.// When the ambiguity remains, we default to `output` since it is the most common use case for additional file descriptors.const DEFAULT_DIRECTION = 'output';modified:lib/stdio/handle.js+57 added-8 removed
1234567891010111212131414151617171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970303132717273746 unmodified lines8182838485868788899091929394959697985699100101102103104105import {getStdioOptionType, isRegularUrl, isUnknownStdioString} from './type.js';import {addStreamDirection} from './direction.js';import {normalizeStdio} from './normalize.js';import {handleInputOption, handleInputFileOption} from './input.js';import {handleNativeStream} from './native.js';
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeexport const handleInput = (addProperties, options) => { const stdio = normalizeStdio(options); const stdioStreams = stdio .map((stdioOption, index) => getStdioStream(stdioOption, index, options)) .map((stdioOption, index) => getStdioStreams(stdioOption, index, options)) .map(stdioStream => addStreamDirection(stdioStream)) .map(stdioStream => addStreamProperties(stdioStream, addProperties)); .map(stdioStream => addStreamsProperties(stdioStream, addProperties)); options.stdio = transformStdio(stdioStreams); return stdioStreams; return stdioStreams.flat();};
const getStdioStream = (stdioOption, index, {input, inputFile}) => {// We make sure passing an array with a single item behaves the same as passing that item without an array.// This is what users would expect.// For example, `stdout: ['ignore']` behaves the same as `stdout: 'ignore'`.const getStdioStreams = (stdioOption, index, options) => { const optionName = getOptionName(index); const stdioParameters = {...options, optionName, index};
if (!Array.isArray(stdioOption)) { return getStdioStream(stdioOption, false, stdioParameters); }
if (stdioOption.length === 0) { throw new TypeError(`The \`${optionName}\` option must not be an empty array.`); }
const stdioOptionArray = [...new Set(stdioOption)]; if (stdioOptionArray.length === 1) { return getStdioStream(stdioOptionArray[0], false, stdioParameters); }
validateStdioArray(stdioOptionArray, optionName);
return stdioOptionArray.map(stdioOptionItem => getStdioStream(stdioOptionItem, true, stdioParameters));};
const getOptionName = index => KNOWN_OPTION_NAMES[index] ?? `stdio[${index}]`;const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];
const validateStdioArray = (stdioOptionArray, optionName) => { for (const invalidStdioOption of INVALID_STDIO_ARRAY_OPTIONS) { if (stdioOptionArray.includes(invalidStdioOption)) { throw new Error(`The \`${optionName}\` option must not include \`${invalidStdioOption}\`.`); } }};
// Using those `stdio` values together with others for the same stream does not make sense, so we make it fail.// However, we do allow it if the array has a single item.const INVALID_STDIO_ARRAY_OPTIONS = ['ignore', 'ipc'];
const getStdioStream = (stdioOption, isStdioArray, {optionName, index, input, inputFile}) => { const type = getStdioOptionType(stdioOption); let stdioStream = {type, value: stdioOption, optionName, index};
stdioStream = handleInputOption(stdioStream, input); stdioStream = handleInputFileOption(stdioStream, inputFile, input); stdioStream = handleNativeStream(stdioStream, isStdioArray);
validateFileStdio(stdioStream);
return stdioStream;};
const getOptionName = index => KNOWN_OPTION_NAMES[index] ?? `stdio[${index}]`;const KNOWN_OPTION_NAMES = ['stdin', 'stdout', 'stderr'];
const validateFileStdio = ({type, value, optionName}) => { if (isRegularUrl(value)) { throw new TypeError(`The \`${optionName}: URL\` option must use the \`file:\` scheme.For example, you can use the \`pathToFileURL()\` method of the \`url\` core module.`);6 unmodified lines
// Some `stdio` values require Execa to create streams.// For example, file paths create file read/write streams.// Those transformations are specified in `addProperties`, which is both direction-specific and type-specific.const addStreamsProperties = (stdioStream, addProperties) => Array.isArray(stdioStream) ? stdioStream.map(stdioStreamItem => addStreamProperties(stdioStreamItem, addProperties)) : addStreamProperties(stdioStream, addProperties);
const addStreamProperties = (stdioStream, addProperties) => ({ ...stdioStream, ...addProperties[stdioStream.direction][stdioStream.type]?.(stdioStream),});
// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `spawned.std*`.// When the `std*: Array` option is used, we emulate some of the native values ('inherit', Node.js stream and file descriptor integer). To do so, we also need to pipe to `spawned.std*`.// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `spawned.std*`.const transformStdio = stdioStreams => stdioStreams.map(stdioStream => transformStdioItem(stdioStream));
const transformStdioItem = stdioStream => stdioStream.type !== 'native' && stdioStream.value !== 'overlapped' ? 'pipe' : stdioStream.value;const transformStdioItem = stdioStream => { if (Array.isArray(stdioStream)) { return stdioStream.some(({value}) => value === 'overlapped') ? 'overlapped' : 'pipe'; }
return stdioStream.type !== 'native' && stdioStream.value !== 'overlapped' ? 'pipe' : stdioStream.value;};added:lib/stdio/native.js+48 added
1+import process from 'node:process';2+import {isStream as isNodeStream} from 'is-stream';3+4+// When we use multiple `stdio` values for the same streams, we pass 'pipe' to `child_process.spawn()`.5+// We then emulate the piping done by core Node.js.6+// To do so, we transform the following values:7+// - Node.js streams are marked as `type: nodeStream`8+// - 'inherit' becomes `process.stdin|stdout|stderr`9+// - any file descriptor integer becomes `process.stdio[index]`10+// All of the above transformations tell Execa to perform manual piping.11+export const handleNativeStream = (stdioStream, isStdioArray) => {12+ const {type, value, index, optionName} = stdioStream;13+14+ if (!isStdioArray || type !== 'native') {15+ return stdioStream;16+ }17+18+ if (value === 'inherit') {19+ return {...stdioStream, type: 'nodeStream', value: getStandardStream(index, value, optionName)};20+ }21+22+ if (typeof value === 'number') {23+ return {...stdioStream, type: 'nodeStream', value: getStandardStream(value, value, optionName)};24+ }25+26+ if (isNodeStream(value)) {27+ return {...stdioStream, type: 'nodeStream'};28+ }29+30+ return stdioStream;31+};32+33+// Node.js does not allow to easily retrieve file descriptors beyond stdin/stdout/stderr as streams.34+// - `fs.createReadStream()`/`fs.createWriteStream()` with the `fd` option do not work with character devices that use blocking reads/writes (such as interactive TTYs).35+// - Using a TCP `Socket` would work but be rather complex to implement.36+// Since this is an edge case, we simply throw an error message.37+// See https://github.com/sindresorhus/execa/pull/643#discussion_r143590570738+const getStandardStream = (index, value, optionName) => {39+ const standardStream = STANDARD_STREAMS[index];40+41+ if (standardStream === undefined) {42+ throw new TypeError(`The \`${optionName}: ${value}\` option is invalid: no such standard stream.`);43+ }44+45+ return standardStream;46+};47+48+const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];modified:lib/stdio/type.js+4 added-3 removed
35 unmodified lines36 const isReadableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object ReadableStream]';37 export const isWritableStream = stdioOption => Object.prototype.toString.call(stdioOption) === '[object WritableStream]';38 const isWebStream = stdioOption => isReadableStream(stdioOption) || isWritableStream(stdioOption);39 40-const isIterableObject = stdinOption => typeof stdinOption === 'object'41- && stdinOption !== null42- && (typeof stdinOption[Symbol.asyncIterator] === 'function' || typeof stdinOption[Symbol.iterator] === 'function');40+const isIterableObject = stdioOption => typeof stdioOption === 'object'41+ && stdioOption !== null42+ && !Array.isArray(stdioOption)43+ && (typeof stdioOption[Symbol.asyncIterator] === 'function' || typeof stdioOption[Symbol.iterator] === 'function');44 45 // Convert types to human-friendly strings for error messages46 export const TYPE_TO_MESSAGE = {47 filePath: 'a file path',6 unmodified linesmodified:lib/stream.js+11 added-1 removed
71 unmodified lines72 const [error] = await once(stream, 'error');73 throw error;74 };75 76+const cleanupStdioStreams = stdioStreams => {77+ for (const stdioStream of stdioStreams) {78+ if (stdioStream.autoDestroy) {79+ stdioStream.value.destroy();80+ }81+ }82+};83+84 // Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)85 export const getSpawnedResult = async (86 spawned,87 {encoding, buffer, maxBuffer},12 unmodified lines100 allPromise,101 ...stdioStreams.map(stdioStream => waitForStreamEnd(stdioStream, processDone)),102 ]);103 } catch (error) {96- spawned.kill();104 return Promise.all([105 {error, signal: error.signal, timedOut: error.timedOut},106 getBufferedData(spawned.stdout, stdoutPromise),107 getBufferedData(spawned.stderr, stderrPromise),108 getBufferedData(spawned.all, allPromise),109 ]);110+ } finally {111+ cleanupStdioStreams(stdioStreams);112+ spawned.kill();113 }114 };added:test/fixtures/empty.js+1 added
1#!/usr/bin/env nodeadded:test/fixtures/nested-multiple-stderr.js+7 added
1234567#!/usr/bin/env nodeimport process from 'node:process';import {execa} from '../../index.js';
const [options] = process.argv.slice(2);const result = await execa('noop-err.js', ['foobar'], {stderr: JSON.parse(options)});process.stdout.write(`nested ${result.stderr}`);added:test/fixtures/nested-multiple-stdin.js+9 added
1+#!/usr/bin/env node2+import process from 'node:process';3+import {execa} from '../../index.js';4+5+const [options] = process.argv.slice(2);6+const childProcess = execa('stdin.js', {stdin: JSON.parse(options)});7+childProcess.stdin.write('foobar');8+const {stdout} = await childProcess;9+console.log(stdout);added:test/fixtures/nested-multiple-stdout.js+7 added
1+#!/usr/bin/env node2+import process from 'node:process';3+import {execa} from '../../index.js';4+5+const [options] = process.argv.slice(2);6+const result = await execa('noop.js', ['foobar'], {stdout: JSON.parse(options)});7+process.stderr.write(`nested ${result.stdout}`);added:test/fixtures/nested-stdio.js+26 added
1+#!/usr/bin/env node2+import process from 'node:process';3+import {execa} from '../../index.js';4+5+const [stdioOption, index, file, ...args] = process.argv.slice(2);6+let optionValue = JSON.parse(stdioOption);7+optionValue = typeof optionValue === 'string' ? process[optionValue] : optionValue;8+optionValue = Array.isArray(optionValue) && typeof optionValue[0] === 'string'9+ ? [process[optionValue[0]], ...optionValue.slice(1)]10+ : optionValue;11+const stdio = ['ignore', 'inherit', 'inherit'];12+stdio[index] = optionValue;13+const childProcess = execa(file, args, {stdio});14+15+const shouldPipe = Array.isArray(optionValue) && optionValue.includes('pipe');16+const hasPipe = childProcess.stdio[index] !== null;17+18+if (shouldPipe && !hasPipe) {19+ throw new Error(`childProcess.stdio[${index}] is null.`);20+}21+22+if (!shouldPipe && hasPipe) {23+ throw new Error(`childProcess.stdio[${index}] should be null.`);24+}25+26+await childProcess;modified:test/fixtures/stdin-fd3.js+1 added-1 removed
1 #!/usr/bin/env node2 import process from 'node:process';3 import {readFileSync} from 'node:fs';4 5-const fileDescriptorIndex = Number(process.argv[3] || 3);5+const fileDescriptorIndex = Number(process.argv[2] || 3);6 console.log(readFileSync(fileDescriptorIndex, {encoding: 'utf8'}));added:test/stdio/array.js+257 added
1+import {readFile, writeFile, rm} from 'node:fs/promises';2+import process from 'node:process';3+import {PassThrough} from 'node:stream';4+import test from 'ava';5+import tempfile from 'tempfile';6+import {execa, execaSync} from '../../index.js';7+import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';8+import {setFixtureDir} from '../helpers/fixtures-dir.js';9+10+setFixtureDir();11+12+const testEmptyArray = (t, getOptions, optionName, execaMethod) => {13+ t.throws(() => {14+ execaMethod('noop.js', getOptions([]));15+ }, {message: `The \`${optionName}\` option must not be an empty array.`});16+};17+18+test('Cannot pass an empty array to stdin', testEmptyArray, getStdinOption, 'stdin', execa);19+test('Cannot pass an empty array to stdout', testEmptyArray, getStdoutOption, 'stdout', execa);20+test('Cannot pass an empty array to stderr', testEmptyArray, getStderrOption, 'stderr', execa);21+test('Cannot pass an empty array to stdio[*]', testEmptyArray, getStdioOption, 'stdio[3]', execa);22+test('Cannot pass an empty array to stdin - sync', testEmptyArray, getStdinOption, 'stdin', execaSync);23+test('Cannot pass an empty array to stdout - sync', testEmptyArray, getStdoutOption, 'stdout', execaSync);24+test('Cannot pass an empty array to stderr - sync', testEmptyArray, getStderrOption, 'stderr', execaSync);25+test('Cannot pass an empty array to stdio[*] - sync', testEmptyArray, getStdioOption, 'stdio[3]', execaSync);26+27+const testNoPipeOption = async (t, stdioOption, streamName) => {28+ const childProcess = execa('empty.js', {[streamName]: stdioOption});29+ t.is(childProcess[streamName], null);30+ await execa;31+};32+33+test('stdin can be "ignore"', testNoPipeOption, 'ignore', 'stdin');34+test('stdin can be ["ignore"]', testNoPipeOption, ['ignore'], 'stdin');35+test('stdin can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 'stdin');36+test('stdin can be "ipc"', testNoPipeOption, 'ipc', 'stdin');37+test('stdin can be ["ipc"]', testNoPipeOption, ['ipc'], 'stdin');38+test('stdin can be "inherit"', testNoPipeOption, 'inherit', 'stdin');39+test('stdin can be ["inherit"]', testNoPipeOption, ['inherit'], 'stdin');40+test('stdin can be 0', testNoPipeOption, 0, 'stdin');41+test('stdin can be [0]', testNoPipeOption, [0], 'stdin');42+test('stdout can be "ignore"', testNoPipeOption, 'ignore', 'stdout');43+test('stdout can be ["ignore"]', testNoPipeOption, ['ignore'], 'stdout');44+test('stdout can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 'stdout');45+test('stdout can be "ipc"', testNoPipeOption, 'ipc', 'stdout');46+test('stdout can be ["ipc"]', testNoPipeOption, ['ipc'], 'stdout');47+test('stdout can be "inherit"', testNoPipeOption, 'inherit', 'stdout');48+test('stdout can be ["inherit"]', testNoPipeOption, ['inherit'], 'stdout');49+test('stdout can be 1', testNoPipeOption, 1, 'stdout');50+test('stdout can be [1]', testNoPipeOption, [1], 'stdout');51+test('stderr can be "ignore"', testNoPipeOption, 'ignore', 'stderr');52+test('stderr can be ["ignore"]', testNoPipeOption, ['ignore'], 'stderr');53+test('stderr can be ["ignore", "ignore"]', testNoPipeOption, ['ignore', 'ignore'], 'stderr');54+test('stderr can be "ipc"', testNoPipeOption, 'ipc', 'stderr');55+test('stderr can be ["ipc"]', testNoPipeOption, ['ipc'], 'stderr');56+test('stderr can be "inherit"', testNoPipeOption, 'inherit', 'stderr');57+test('stderr can be ["inherit"]', testNoPipeOption, ['inherit'], 'stderr');58+test('stderr can be 2', testNoPipeOption, 2, 'stderr');59+test('stderr can be [2]', testNoPipeOption, [2], 'stderr');60+61+const testNoPipeStdioOption = async (t, stdioOption) => {62+ const childProcess = execa('empty.js', {stdio: ['pipe', 'pipe', 'pipe', stdioOption]});63+ t.is(childProcess.stdio[3], null);64+ await execa;65+};66+67+test('stdio[*] can be "ignore"', testNoPipeStdioOption, 'ignore');68+test('stdio[*] can be ["ignore"]', testNoPipeStdioOption, ['ignore']);69+test('stdio[*] can be ["ignore", "ignore"]', testNoPipeStdioOption, ['ignore', 'ignore']);70+test('stdio[*] can be "ipc"', testNoPipeStdioOption, 'ipc');71+test('stdio[*] can be ["ipc"]', testNoPipeStdioOption, ['ipc']);72+test('stdio[*] can be "inherit"', testNoPipeStdioOption, 'inherit');73+test('stdio[*] can be ["inherit"]', testNoPipeStdioOption, ['inherit']);74+test('stdio[*] can be 3', testNoPipeStdioOption, 3);75+test('stdio[*] can be [3]', testNoPipeStdioOption, [3]);76+77+const testInvalidArrayValue = (t, invalidStdio, getOptions, execaMethod) => {78+ t.throws(() => {79+ execaMethod('noop.js', getOptions(['pipe', invalidStdio]));80+ }, {message: /must not include/});81+};82+83+test('Cannot pass "ignore" and another value to stdin', testInvalidArrayValue, 'ignore', getStdinOption, execa);84+test('Cannot pass "ignore" and another value to stdout', testInvalidArrayValue, 'ignore', getStdoutOption, execa);85+test('Cannot pass "ignore" and another value to stderr', testInvalidArrayValue, 'ignore', getStderrOption, execa);86+test('Cannot pass "ignore" and another value to stdio[*]', testInvalidArrayValue, 'ignore', getStdioOption, execa);87+test('Cannot pass "ignore" and another value to stdin - sync', testInvalidArrayValue, 'ignore', getStdinOption, execaSync);88+test('Cannot pass "ignore" and another value to stdout - sync', testInvalidArrayValue, 'ignore', getStdoutOption, execaSync);89+test('Cannot pass "ignore" and another value to stderr - sync', testInvalidArrayValue, 'ignore', getStderrOption, execaSync);90+test('Cannot pass "ignore" and another value to stdio[*] - sync', testInvalidArrayValue, 'ignore', getStdioOption, execaSync);91+test('Cannot pass "ipc" and another value to stdin', testInvalidArrayValue, 'ipc', getStdinOption, execa);92+test('Cannot pass "ipc" and another value to stdout', testInvalidArrayValue, 'ipc', getStdoutOption, execa);93+test('Cannot pass "ipc" and another value to stderr', testInvalidArrayValue, 'ipc', getStderrOption, execa);94+test('Cannot pass "ipc" and another value to stdio[*]', testInvalidArrayValue, 'ipc', getStdioOption, execa);95+test('Cannot pass "ipc" and another value to stdin - sync', testInvalidArrayValue, 'ipc', getStdinOption, execaSync);96+test('Cannot pass "ipc" and another value to stdout - sync', testInvalidArrayValue, 'ipc', getStdoutOption, execaSync);97+test('Cannot pass "ipc" and another value to stderr - sync', testInvalidArrayValue, 'ipc', getStderrOption, execaSync);98+test('Cannot pass "ipc" and another value to stdio[*] - sync', testInvalidArrayValue, 'ipc', getStdioOption, execaSync);99+100+const testInputOutput = (t, stdioOption, execaMethod) => {101+ t.throws(() => {102+ execaMethod('noop.js', getStdioOption([new ReadableStream(), stdioOption]));103+ }, {message: /readable and writable/});104+};105+106+test('Cannot pass both readable and writable values to stdio[*] - WritableStream', testInputOutput, new WritableStream(), execa);107+test('Cannot pass both readable and writable values to stdio[*] - 1', testInputOutput, 1, execa);108+test('Cannot pass both readable and writable values to stdio[*] - 2', testInputOutput, 2, execa);109+test('Cannot pass both readable and writable values to stdio[*] - process.stdout', testInputOutput, process.stdout, execa);110+test('Cannot pass both readable and writable values to stdio[*] - process.stderr', testInputOutput, process.stderr, execa);111+test('Cannot pass both readable and writable values to stdio[*] - WritableStream - sync', testInputOutput, new WritableStream(), execaSync);112+test('Cannot pass both readable and writable values to stdio[*] - 1 - sync', testInputOutput, 1, execaSync);113+test('Cannot pass both readable and writable values to stdio[*] - 2 - sync', testInputOutput, 2, execaSync);114+test('Cannot pass both readable and writable values to stdio[*] - process.stdout - sync', testInputOutput, process.stdout, execaSync);115+test('Cannot pass both readable and writable values to stdio[*] - process.stderr - sync', testInputOutput, process.stderr, execaSync);116+117+const testAmbiguousDirection = async (t, execaMethod) => {118+ const [filePathOne, filePathTwo] = [tempfile(), tempfile()];119+ await execaMethod('noop-fd3.js', ['foobar'], getStdioOption([filePathOne, filePathTwo]));120+ t.deepEqual(await Promise.all([readFile(filePathOne, 'utf8'), readFile(filePathTwo, 'utf8')]), ['foobar\n', 'foobar\n']);121+ await Promise.all([rm(filePathOne), rm(filePathTwo)]);122+};123+124+test('stdio[*] default direction is output', testAmbiguousDirection, execa);125+test('stdio[*] default direction is output - sync', testAmbiguousDirection, execaSync);126+127+const testAmbiguousMultiple = async (t, stdioOptions) => {128+ const filePath = tempfile();129+ await writeFile(filePath, 'foobar');130+ const {stdout} = await execa('stdin-fd3.js', getStdioOption([filePath, ...stdioOptions]));131+ t.is(stdout, 'foobar');132+ await rm(filePath);133+};134+135+test('stdio[*] ambiguous direction is influenced by other values like ReadableStream', testAmbiguousMultiple, [new ReadableStream()]);136+test('stdio[*] ambiguous direction is influenced by other values like 0', testAmbiguousMultiple, [0]);137+test('stdio[*] ambiguous direction is influenced by other values like process.stdin', testAmbiguousMultiple, [process.stdin]);138+test('stdio[*] Duplex has an ambiguous direction like ReadableStream', testAmbiguousMultiple, [new PassThrough(), new ReadableStream()]);139+test('stdio[*] Duplex has an ambiguous direction like 0', testAmbiguousMultiple, [new PassThrough(), 0]);140+test('stdio[*] Duplex has an ambiguous direction like process.stdin', testAmbiguousMultiple, [new PassThrough(), process.stdin]);141+142+const testRedirectInput = async (t, stdioOption, index, fixtureName) => {143+ const {stdout} = await execa('nested-stdio.js', [JSON.stringify(stdioOption), String(index), fixtureName], {input: 'foobar'});144+ t.is(stdout, 'foobar');145+};146+147+test.serial('stdio[*] can be 0', testRedirectInput, 0, 3, 'stdin-fd3.js');148+test.serial('stdio[*] can be [0]', testRedirectInput, [0], 3, 'stdin-fd3.js');149+test.serial('stdio[*] can be [0, "pipe"]', testRedirectInput, [0, 'pipe'], 3, 'stdin-fd3.js');150+test.serial('stdio[*] can be process.stdin', testRedirectInput, 'stdin', 3, 'stdin-fd3.js');151+test.serial('stdio[*] can be [process.stdin]', testRedirectInput, ['stdin'], 3, 'stdin-fd3.js');152+test.serial('stdio[*] can be [process.stdin, "pipe"]', testRedirectInput, ['stdin', 'pipe'], 3, 'stdin-fd3.js');153+154+const OUTPUT_DESCRIPTOR_FIXTURES = ['noop.js', 'noop-err.js', 'noop-fd3.js'];155+156+const isStdoutDescriptor = stdioOption => stdioOption === 1157+ || stdioOption === 'stdout'158+ || (Array.isArray(stdioOption) && isStdoutDescriptor(stdioOption[0]));159+160+const testRedirectOutput = async (t, stdioOption, index) => {161+ const fixtureName = OUTPUT_DESCRIPTOR_FIXTURES[index - 1];162+ const result = await execa('nested-stdio.js', [JSON.stringify(stdioOption), String(index), fixtureName, 'foobar']);163+ const streamName = isStdoutDescriptor(stdioOption) ? 'stdout' : 'stderr';164+ t.is(result[streamName], 'foobar');165+};166+167+test('stdout can be 2', testRedirectOutput, 2, 1);168+test('stdout can be [2]', testRedirectOutput, [2], 1);169+test('stdout can be [2, "pipe"]', testRedirectOutput, [2, 'pipe'], 1);170+test('stdout can be process.stderr', testRedirectOutput, 'stderr', 1);171+test('stdout can be [process.stderr]', testRedirectOutput, ['stderr'], 1);172+test('stdout can be [process.stderr, "pipe"]', testRedirectOutput, ['stderr', 'pipe'], 1);173+test('stderr can be 1', testRedirectOutput, 1, 2);174+test('stderr can be [1]', testRedirectOutput, [1], 2);175+test('stderr can be [1, "pipe"]', testRedirectOutput, [1, 'pipe'], 2);176+test('stderr can be process.stdout', testRedirectOutput, 'stdout', 2);177+test('stderr can be [process.stdout]', testRedirectOutput, ['stdout'], 2);178+test('stderr can be [process.stdout, "pipe"]', testRedirectOutput, ['stdout', 'pipe'], 2);179+test('stdio[*] can be 1', testRedirectOutput, 1, 3);180+test('stdio[*] can be [1]', testRedirectOutput, [1], 3);181+test('stdio[*] can be [1, "pipe"]', testRedirectOutput, [1, 'pipe'], 3);182+test('stdio[*] can be 2', testRedirectOutput, 2, 3);183+test('stdio[*] can be [2]', testRedirectOutput, [2], 3);184+test('stdio[*] can be [2, "pipe"]', testRedirectOutput, [2, 'pipe'], 3);185+test('stdio[*] can be process.stdout', testRedirectOutput, 'stdout', 3);186+test('stdio[*] can be [process.stdout]', testRedirectOutput, ['stdout'], 3);187+test('stdio[*] can be [process.stdout, "pipe"]', testRedirectOutput, ['stdout', 'pipe'], 3);188+test('stdio[*] can be process.stderr', testRedirectOutput, 'stderr', 3);189+test('stdio[*] can be [process.stderr]', testRedirectOutput, ['stderr'], 3);190+test('stdio[*] can be [process.stderr, "pipe"]', testRedirectOutput, ['stderr', 'pipe'], 3);191+192+const testInheritStdin = async (t, stdin) => {193+ const {stdout} = await execa('nested-multiple-stdin.js', [JSON.stringify(stdin)], {input: 'foobar'});194+ t.is(stdout, 'foobarfoobar');195+};196+197+test('stdin can be ["inherit", "pipe"]', testInheritStdin, ['inherit', 'pipe']);198+test('stdin can be [0, "pipe"]', testInheritStdin, [0, 'pipe']);199+200+const testInheritStdout = async (t, stdout) => {201+ const result = await execa('nested-multiple-stdout.js', [JSON.stringify(stdout)]);202+ t.is(result.stdout, 'foobar');203+ t.is(result.stderr, 'nested foobar');204+};205+206+test('stdout can be ["inherit", "pipe"]', testInheritStdout, ['inherit', 'pipe']);207+test('stdout can be [1, "pipe"]', testInheritStdout, [1, 'pipe']);208+209+const testInheritStderr = async (t, stderr) => {210+ const result = await execa('nested-multiple-stderr.js', [JSON.stringify(stderr)]);211+ t.is(result.stdout, 'nested foobar');212+ t.is(result.stderr, 'foobar');213+};214+215+test('stderr can be ["inherit", "pipe"]', testInheritStderr, ['inherit', 'pipe']);216+test('stderr can be [2, "pipe"]', testInheritStderr, [2, 'pipe']);217+218+const testOverflowStream = async (t, stdio) => {219+ const {stdout} = await execa('nested.js', [JSON.stringify({stdio}), 'empty.js'], {stdio: ['pipe', 'pipe', 'pipe', 'pipe']});220+ t.is(stdout, '');221+};222+223+if (process.platform === 'linux') {224+ test('stdin can use 4+', testOverflowStream, [4, 'pipe', 'pipe', 'pipe']);225+ test('stdin can use [4+]', testOverflowStream, [[4], 'pipe', 'pipe', 'pipe']);226+ test('stdout can use 4+', testOverflowStream, ['pipe', 4, 'pipe', 'pipe']);227+ test('stdout can use [4+]', testOverflowStream, ['pipe', [4], 'pipe', 'pipe']);228+ test('stderr can use 4+', testOverflowStream, ['pipe', 'pipe', 4, 'pipe']);229+ test('stderr can use [4+]', testOverflowStream, ['pipe', 'pipe', [4], 'pipe']);230+ test('stdio[*] can use 4+', testOverflowStream, ['pipe', 'pipe', 'pipe', 4]);231+ test('stdio[*] can use [4+]', testOverflowStream, ['pipe', 'pipe', 'pipe', [4]]);232+}233+234+test('stdio[*] can use "inherit"', testOverflowStream, ['pipe', 'pipe', 'pipe', 'inherit']);235+test('stdio[*] can use ["inherit"]', testOverflowStream, ['pipe', 'pipe', 'pipe', ['inherit']]);236+237+const testOverflowStreamArray = (t, stdio) => {238+ t.throws(() => {239+ execa('noop.js', {stdio});240+ }, {message: /no such standard stream/});241+};242+243+test('stdin cannot use 4+ and another value', testOverflowStreamArray, [[4, 'pipe'], 'pipe', 'pipe', 'pipe']);244+test('stdout cannot use 4+ and another value', testOverflowStreamArray, ['pipe', [4, 'pipe'], 'pipe', 'pipe']);245+test('stderr cannot use 4+ and another value', testOverflowStreamArray, ['pipe', 'pipe', [4, 'pipe'], 'pipe']);246+test('stdio[*] cannot use 4+ and another value', testOverflowStreamArray, ['pipe', 'pipe', 'pipe', [4, 'pipe']]);247+test('stdio[*] cannot use "inherit" and another value', testOverflowStreamArray, ['pipe', 'pipe', 'pipe', ['inherit', 'pipe']]);248+249+const testOverlapped = async (t, getOptions) => {250+ const {stdout} = await execa('noop.js', ['foobar'], getOptions(['overlapped', 'pipe']));251+ t.is(stdout, 'foobar');252+};253+254+test('stdin can be ["overlapped", "pipe"]', testOverlapped, getStdinOption);255+test('stdout can be ["overlapped", "pipe"]', testOverlapped, getStdoutOption);256+test('stderr can be ["overlapped", "pipe"]', testOverlapped, getStderrOption);257+test('stdio[*] can be ["overlapped", "pipe"]', testOverlapped, getStdioOption);modified:test/stdio/node-stream.js+28 added
73 unmodified lines74 75 test('stdout can be a Node.js Writable with a file descriptor', testFileWritable, getStdoutOption, 'noop.js');76 test('stderr can be a Node.js Writable with a file descriptor', testFileWritable, getStderrOption, 'noop-err.js');77 test('stdio[*] can be a Node.js Writable with a file descriptor', testFileWritable, getStdioOption, 'noop-fd3.js');78+79+const testLazyFileReadable = async (t, fixtureName, getOptions) => {80+ const filePath = tempfile();81+ await writeFile(filePath, 'foobar');82+ const stream = createReadStream(filePath);83+84+ const {stdout} = await execa(fixtureName, getOptions([stream, 'pipe']));85+ t.is(stdout, 'foobar');86+87+ await rm(filePath);88+};89+90+test('stdin can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 'stdin.js', getStdinOption);91+test('stdio[*] can be [Readable, "pipe"] without a file descriptor', testLazyFileReadable, 'stdin-fd3.js', getStdioOption);92+93+const testLazyFileWritable = async (t, getOptions, fixtureName) => {94+ const filePath = tempfile();95+ const stream = createWriteStream(filePath);96+97+ await execa(fixtureName, ['foobar'], getOptions([stream, 'pipe']));98+ t.is(await readFile(filePath, 'utf8'), 'foobar\n');99+100+ await rm(filePath);101+};102+103+test('stdout can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, getStdoutOption, 'noop.js');104+test('stderr can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, getStderrOption, 'noop-err.js');105+test('stdio[*] can be [Writable, "pipe"] without a file descriptor', testLazyFileWritable, getStdioOption, 'noop-fd3.js');modified:index.d.ts+26 added-13 removed
23 unmodified lines24 type OutputStdioOption =25 | Writable26 | WritableStream;27 28-export type StdinOption = CommonStdioOption | InputStdioOption;29-export type StdoutStderrOption = CommonStdioOption | OutputStdioOption;30-export type StdioOption = CommonStdioOption | InputStdioOption | OutputStdioOption;28+export type StdinOption =29+ CommonStdioOption | InputStdioOption30+ | Array<CommonStdioOption | InputStdioOption>;31+export type StdoutStderrOption =32+ CommonStdioOption | OutputStdioOption33+ | Array<CommonStdioOption | OutputStdioOption>;34+export type StdioOption =35+ CommonStdioOption | InputStdioOption | OutputStdioOption36+ | Array<CommonStdioOption | InputStdioOption | OutputStdioOption>;37 38 type StdioOptions =39 | BaseStdioOption40 | readonly [StdinOption, StdoutStderrOption, StdoutStderrOption, ...StdioOption[]];83 unmodified lines124 - file URL.125 - web [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).126 - [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols)127 128+ This can be an [array of values](https://github.com/sindresorhus/execa#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.129+130 @default `inherit` with `$`, `pipe` otherwise131 */132 readonly stdin?: StdinOption;133 11 unmodified lines145 - file path. If relative, it must start with `.`.146 - file URL.147 - web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).148 149+ This can be an [array of values](https://github.com/sindresorhus/execa#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.150+151 @default 'pipe'152 */153 readonly stdout?: StdoutStderrOption;154 11 unmodified lines166 - file path. If relative, it must start with `.`.167 - file URL.168 - web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).169 170+ This can be an [array of values](https://github.com/sindresorhus/execa#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.171+172 @default 'pipe'173 */174 readonly stderr?: StdoutStderrOption;175 176 /**177+ Like the `stdin`, `stdout` and `stderr` options but for all file descriptors at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.178+179+ A single string can be used as a shortcut. For example, `{stdio: 'pipe'}` is the same as `{stdin: 'pipe', stdout: 'pipe', stderr: 'pipe'}`.180+181+ The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`. For example, `{stdio: ['pipe', 'pipe', 'pipe', 'ipc']}` sets a fourth file descriptor `'ipc'`.182+183+ @default 'pipe'184+ */185+ readonly stdio?: StdioOptions;186+187+ /**188 Setting this to `false` resolves the promise with the error instead of rejecting it.189 190 @default true191 */39 unmodified lines231 */232 readonly argv0?: string;233 234 /**212- Like the `stdin`, `stdout` and `stderr` options but for all file descriptors at once.213- The possible values are the same except it can also be:214- - a single string, to set the same value to each standard stream.215- - an array with more than 3 values, to create more than 3 file descriptors.216-217- @default 'pipe'218- */219- readonly stdio?: StdioOptions;220-221- /**235 Specify the kind of serialization used for sending messages between processes when using the `stdio: 'ipc'` option or `execaNode()`:236 - `json`: Uses `JSON.stringify()` and `JSON.parse()`.237 - `advanced`: Uses [`v8.serialize()`](https://nodejs.org/api/v8.html#v8_v8_serialize_value)238 844 unmodified linesmodified:index.test-d.ts+76 added-3 removed
158 unmodified lines159 execa('unicorns', {input: process.stdin});160 execa('unicorns', {inputFile: ''});161 execa('unicorns', {inputFile: fileUrl});162 execa('unicorns', {stdin: 'pipe'});163+execa('unicorns', {stdin: ['pipe']});164 execa('unicorns', {stdin: 'overlapped'});165+execa('unicorns', {stdin: ['overlapped']});166 execa('unicorns', {stdin: 'ipc'});167+execa('unicorns', {stdin: ['ipc']});168 execa('unicorns', {stdin: 'ignore'});169+execa('unicorns', {stdin: ['ignore']});170 execa('unicorns', {stdin: 'inherit'});171+execa('unicorns', {stdin: ['inherit']});172 execa('unicorns', {stdin: process.stdin});173+execa('unicorns', {stdin: [process.stdin]});174 execa('unicorns', {stdin: new Readable()});175+execa('unicorns', {stdin: [new Readable()]});176 expectError(execa('unicorns', {stdin: new Writable()}));177+expectError(execa('unicorns', {stdin: [new Writable()]}));178 execa('unicorns', {stdin: new ReadableStream()});179+execa('unicorns', {stdin: [new ReadableStream()]});180 expectError(execa('unicorns', {stdin: new WritableStream()}));172-execa('unicorns', {stdin: ['']});173-execa('unicorns', {stdin: [new Uint8Array(0)]});181+expectError(execa('unicorns', {stdin: [new WritableStream()]}));182 execa('unicorns', {stdin: stringGenerator()});183+execa('unicorns', {stdin: [stringGenerator()]});184 execa('unicorns', {stdin: binaryGenerator()});185+execa('unicorns', {stdin: [binaryGenerator()]});186 execa('unicorns', {stdin: asyncStringGenerator()});177-expectError(execa('unicorns', {stdin: [0]}));187+execa('unicorns', {stdin: [asyncStringGenerator()]});188 expectError(execa('unicorns', {stdin: numberGenerator()}));189+expectError(execa('unicorns', {stdin: [numberGenerator()]}));190 execa('unicorns', {stdin: fileUrl});191+execa('unicorns', {stdin: [fileUrl]});192 execa('unicorns', {stdin: './test'});193+execa('unicorns', {stdin: ['./test']});194 execa('unicorns', {stdin: 1});195+execa('unicorns', {stdin: [1]});196 execa('unicorns', {stdin: undefined});197+execa('unicorns', {stdin: [undefined]});198+execa('unicorns', {stdin: ['pipe', 'inherit']});199 execa('unicorns', {stdout: 'pipe'});200+execa('unicorns', {stdout: ['pipe']});201 execa('unicorns', {stdout: 'overlapped'});202+execa('unicorns', {stdout: ['overlapped']});203 execa('unicorns', {stdout: 'ipc'});204+execa('unicorns', {stdout: ['ipc']});205 execa('unicorns', {stdout: 'ignore'});206+execa('unicorns', {stdout: ['ignore']});207 execa('unicorns', {stdout: 'inherit'});208+execa('unicorns', {stdout: ['inherit']});209 execa('unicorns', {stdout: process.stdout});210+execa('unicorns', {stdout: [process.stdout]});211 execa('unicorns', {stdout: new Writable()});212+execa('unicorns', {stdout: [new Writable()]});213 expectError(execa('unicorns', {stdout: new Readable()}));214+expectError(execa('unicorn', {stdout: [new Readable()]}));215 execa('unicorns', {stdout: new WritableStream()});216+execa('unicorns', {stdout: [new WritableStream()]});217 expectError(execa('unicorns', {stdout: new ReadableStream()}));218+expectError(execa('unicorn', {stdout: [new ReadableStream()]}));219 execa('unicorns', {stdout: fileUrl});220+execa('unicorns', {stdout: [fileUrl]});221 execa('unicorns', {stdout: './test'});222+execa('unicorns', {stdout: ['./test']});223 execa('unicorns', {stdout: 1});224+execa('unicorns', {stdout: [1]});225 execa('unicorns', {stdout: undefined});226+execa('unicorns', {stdout: [undefined]});227+execa('unicorns', {stdout: ['pipe', 'inherit']});228 execa('unicorns', {stderr: 'pipe'});229+execa('unicorns', {stderr: ['pipe']});230 execa('unicorns', {stderr: 'overlapped'});231+execa('unicorns', {stderr: ['overlapped']});232 execa('unicorns', {stderr: 'ipc'});233+execa('unicorns', {stderr: ['ipc']});234 execa('unicorns', {stderr: 'ignore'});235+execa('unicorns', {stderr: ['ignore']});236 execa('unicorns', {stderr: 'inherit'});237+execa('unicorns', {stderr: ['inherit']});238 execa('unicorns', {stderr: process.stderr});239+execa('unicorns', {stderr: [process.stderr]});240 execa('unicorns', {stderr: new Writable()});241+execa('unicorns', {stderr: [new Writable()]});242 expectError(execa('unicorns', {stderr: new Readable()}));243+expectError(execa('unicorns', {stderr: [new Readable()]}));244 execa('unicorns', {stderr: new WritableStream()});245+execa('unicorns', {stderr: [new WritableStream()]});246 expectError(execa('unicorns', {stderr: new ReadableStream()}));247+expectError(execa('unicorns', {stderr: [new ReadableStream()]}));248 execa('unicorns', {stderr: fileUrl});249+execa('unicorns', {stderr: [fileUrl]});250 execa('unicorns', {stderr: './test'});251+execa('unicorns', {stderr: ['./test']});252 execa('unicorns', {stderr: 1});253+execa('unicorns', {stderr: [1]});254 execa('unicorns', {stderr: undefined});255+execa('unicorns', {stderr: [undefined]});256+execa('unicorns', {stderr: ['pipe', 'inherit']});257 execa('unicorns', {all: true});258 execa('unicorns', {reject: false});259 execa('unicorns', {stripFinalNewline: false});260 execa('unicorns', {extendEnv: false});17 unmodified lines278 expectError(execa('unicorns', {stdio: stringGenerator()}));279 expectError(execa('unicorns', {stdio: asyncStringGenerator()}));280 expectError(execa('unicorns', {stdio: ['pipe', 'pipe']}));281 execa('unicorns', {stdio: [new Readable(), 'pipe', 'pipe']});282+execa('unicorns', {stdio: [[new Readable()], ['pipe'], ['pipe']]});283 execa('unicorns', {stdio: ['pipe', new Writable(), 'pipe']});284+execa('unicorns', {stdio: [['pipe'], [new Writable()], ['pipe']]});285 execa('unicorns', {stdio: ['pipe', 'pipe', new Writable()]});286+execa('unicorns', {stdio: [['pipe'], ['pipe'], [new Writable()]]});287 expectError(execa('unicorns', {stdio: [new Writable(), 'pipe', 'pipe']}));288+expectError(execa('unicorns', {stdio: [[new Writable()], ['pipe'], ['pipe']]}));289 expectError(execa('unicorns', {stdio: ['pipe', new Readable(), 'pipe']}));290+expectError(execa('unicorns', {stdio: [['pipe'], [new Readable()], ['pipe']]}));291 expectError(execa('unicorns', {stdio: ['pipe', 'pipe', new Readable()]}));292+expectError(execa('unicorns', {stdio: [['pipe'], ['pipe'], [new Readable()]]}));293 execa('unicorns', {294 stdio: [295 'pipe',296 'overlapped',12 unmodified lines309 stringGenerator(),310 asyncStringGenerator(),311 ],312 });313+execa('unicorns', {314+ stdio: [315+ ['pipe'],316+ ['pipe', 'inherit'],317+ ['overlapped'],318+ ['ipc'],319+ ['ignore'],320+ ['inherit'],321+ [process.stdin],322+ [1],323+ [undefined],324+ [fileUrl],325+ ['./test'],326+ [new Writable()],327+ [new Readable()],328+ [new WritableStream()],329+ [new ReadableStream()],330+ [stringGenerator()],331+ [asyncStringGenerator()],332+ ],333+});334 execa('unicorns', {serialization: 'advanced'});335 execa('unicorns', {detached: true});336 execa('unicorns', {uid: 0});337 execa('unicorns', {gid: 0});146 unmodified linesmodified:readme.md+27 added-8 removed
567 unmodified lines568 See also the [`input`](#input) and [`stdin`](#stdin) options.569 570 #### stdin571 572-Type: `string | number | stream.Readable | ReadableStream | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>`\572+Type: `string | number | stream.Readable | ReadableStream | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>` (or a tuple of those types)\573 Default: `inherit` with [`$`](#command), `pipe` otherwise574 575 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard input. This can be:576 - `'pipe'`: Sets [`childProcess.stdin`](https://nodejs.org/api/child_process.html#subprocessstdin) stream.9 unmodified lines586 - file URL.587 - web [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).588 - [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) or [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols)589 590+This can be an [array of values](#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.591+592 #### stdout593 592-Type: `string | number | stream.Writable | WritableStream | URL`\594+Type: `string | number | stream.Writable | WritableStream | URL` (or a tuple of those types)\595 Default: `pipe`596 597 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard output. This can be:598 - `'pipe'`: Sets [`childProcessResult.stdout`](#stdout) (as a string or `Uint8Array`) and [`childProcess.stdout`](https://nodejs.org/api/child_process.html#subprocessstdout) (as a stream).8 unmodified lines607 - file path. If relative, it must start with `.`.608 - file URL.609 - web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).610 611+This can be an [array of values](#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.612+613 #### stderr614 611-Type: `string | number | stream.Writable | WritableStream | URL`\615+Type: `string | number | stream.Writable | WritableStream | URL` (or a tuple of those types)`\616 Default: `pipe`617 618 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard error. This can be:619 - `'pipe'`: Sets [`childProcessResult.stderr`](#stderr) (as a string or `Uint8Array`) and [`childProcess.stderr`](https://nodejs.org/api/child_process.html#subprocessstderr) (as a stream).8 unmodified lines628 - file path. If relative, it must start with `.`.629 - file URL.630 - web [`WritableStream`](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream).631 632+This can be an [array of values](#redirect-stdinstdoutstderr-to-multiple-destinations) such as `['inherit', 'pipe']` or `[filePath, 'pipe']`.633+634 #### stdio635 630-Type: `string | [StdinOption, StdoutOption, StderrOption] | StdioOption[]`\636+Type: `string | Array<string | number | stream.Readable | stream.Writable | ReadableStream | WritableStream | URL | Iterable<string | Uint8Array> | AsyncIterable<string | Uint8Array>>` (or a tuple of those types)\637 Default: `pipe`638 633-Like the [`stdin`](#stdin), [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options but for all file descriptors at once.\634-The possible values are the same except it can also be:635-- a single string, to set the same value to each standard stream.636-- an array with more than 3 values, to create more than 3 file descriptors.639+Like the [`stdin`](#stdin), [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options but for all file descriptors at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.640 641+A single string can be used as a shortcut. For example, `{stdio: 'pipe'}` is the same as `{stdin: 'pipe', stdout: 'pipe', stderr: 'pipe'}`.642+643+The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`. For example, `{stdio: ['pipe', 'pipe', 'pipe', 'ipc']}` sets a fourth file descriptor `'ipc'`.644+645 #### all646 647 Type: `boolean`\648 Default: `false`160 unmodified lines809 List of [CLI options](https://nodejs.org/api/cli.html#cli_options) passed to the Node.js executable.810 811 ## Tips812 813+### Redirect stdin/stdout/stderr to multiple destinations814+815+The [`stdin`](#stdin), [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options can be an array of values.816+The following example redirects `stdout` to both the terminal and an `output.txt` file, while also retrieving its value programmatically.817+818+```js819+const {stdout} = await execa('npm', ['install'], {stdout: ['inherit', './output.txt', 'pipe']})820+console.log(stdout);821+```822+823+When combining `inherit` with other values, please note that the child process will not be an interactive TTY, even if the parent process is one.824+825 ### Retry on error826 827 Gracefully handle failures by using automatic retries and exponential backoff with the [`p-retry`](https://github.com/sindresorhus/p-retry) package:828 84 unmodified lines