Refactor `stdio` normalization logic (#638)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 9632b38 · Browse files · 3 files·+13 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+13 added−31 removed
- tests:+6 added−6 removed
- other:+7 added−25 removed
Provenance
- Commit
f4955ad- Tree
7d863af- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
9632b38
3 files changed+13 added−31 removed
modified:lib/stdio/handle.js+4 added-9 removed
3 unmodified lines456789108910111214151314151634 unmodified lines51525354585960555662573 unmodified lines
// Handle `input`, `inputFile`, `stdin`, `stdout` and `stderr` options, before spawning, in async/sync modeexport const handleInput = (addProperties, options) => { const stdio = normalizeStdio(options); const stdioArray = arrifyStdio(stdio); const stdioStreams = stdioArray.map((stdioOption, index) => getStdioStream(stdioOption, index, addProperties, options)); options.stdio = transformStdio(stdio, stdioStreams); const stdioStreams = stdio.map((stdioOption, index) => getStdioStream(stdioOption, index, addProperties, options)); options.stdio = transformStdio(stdioStreams); return stdioStreams;};
const arrifyStdio = (stdio = []) => Array.isArray(stdio) ? stdio : [stdio, stdio, stdio];
const getStdioStream = (stdioOption, index, addProperties, {input, inputFile}) => { let stdioStream = getInitialStdioStream(stdioOption, index);
stdioStream = handleInputOption(stdioStream, index, input);34 unmodified lines};
// When the `std*: Iterable | WebStream | URL | filePath`, `input` or `inputFile` option is used, we pipe to `spawned.std*`.// Therefore the `std*` options must be either `pipe` or `overlapped`. Other values do not set `spawned.std*`.const transformStdio = (stdio, stdioStreams) => Array.isArray(stdio) ? stdio.map((stdioItem, index) => transformStdioItem(stdioItem, stdioStreams[index])) : stdio;const transformStdio = stdioStreams => stdioStreams.map(stdioStream => transformStdioItem(stdioStream));
const transformStdioItem = (stdioItem, {type}) => type !== 'native' && stdioItem !== 'overlapped' ? 'pipe' : stdioItem;const transformStdioItem = stdioStream => stdioStream.type !== 'native' && stdioStream.value !== 'overlapped' ? 'pipe' : stdioStream.value;modified:lib/stdio/normalize.js+3 added-16 removed
1234456785 unmodified lines141516171818192021229 unmodified lines3233343536373839404142434445464748493637// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`export const normalizeStdio = options => { if (!options) { return; return [undefined, undefined, undefined]; }
const {stdio} = options;
5 unmodified lines throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`); }
if (typeof stdio === 'string') { return stdio; return [stdio, stdio, stdio]; }
if (!Array.isArray(stdio)) { throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);9 unmodified lines
// Same but for `execaNode()`, i.e. push `ipc` unless already presentexport const normalizeStdioNode = options => { const stdio = normalizeStdio(options);
if (stdio === 'ipc') { return 'ipc'; }
if (stdio === undefined || typeof stdio === 'string') { return [stdio, stdio, stdio, 'ipc']; }
if (stdio.includes('ipc')) { return stdio; }
return [...stdio, 'ipc']; return stdio.includes('ipc') ? stdio : [...stdio, 'ipc'];};modified:test/stdio/normalize.js+6 added-6 removed
16 unmodified lines1718192021222122232425262425262728293020 unmodified lines515253545555565758597 unmodified lines16 unmodified lines
const stdioMacro = (...args) => macro(...args, normalizeStdio);stdioMacro.title = macroTitle('execa()');
test(stdioMacro, undefined, undefined);test(stdioMacro, null, undefined);test(stdioMacro, undefined, [undefined, undefined, undefined]);test(stdioMacro, null, [undefined, undefined, undefined]);
test(stdioMacro, {stdio: 'inherit'}, 'inherit');test(stdioMacro, {stdio: 'pipe'}, 'pipe');test(stdioMacro, {stdio: 'ignore'}, 'ignore');test(stdioMacro, {stdio: 'inherit'}, ['inherit', 'inherit', 'inherit']);test(stdioMacro, {stdio: 'pipe'}, ['pipe', 'pipe', 'pipe']);test(stdioMacro, {stdio: 'ignore'}, ['ignore', 'ignore', 'ignore']);test(stdioMacro, {stdio: [0, 1, 2]}, [0, 1, 2]);
test(stdioMacro, {}, [undefined, undefined, undefined]);test(stdioMacro, {stdio: []}, [undefined, undefined, undefined]);20 unmodified linesforkMacro.title = macroTitle('execaNode()');
test(forkMacro, undefined, [undefined, undefined, undefined, 'ipc']);test(forkMacro, {stdio: 'ignore'}, ['ignore', 'ignore', 'ignore', 'ipc']);test(forkMacro, {stdio: 'ipc'}, 'ipc');test(forkMacro, {stdio: 'ipc'}, ['ipc', 'ipc', 'ipc']);test(forkMacro, {stdio: [0, 1, 2]}, [0, 1, 2, 'ipc']);test(forkMacro, {stdio: [0, 1, 2, 3]}, [0, 1, 2, 3, 'ipc']);test(forkMacro, {stdio: [0, 1, 2, 'ipc']}, [0, 1, 2, 'ipc']);
7 unmodified lines