normalize.js
This file
Path
lib/stdio/normalize.js
37 lines · 1.1 KiB · JavaScript
Last changed by
Refactor `stdio` normalization logic (#638)ehmickybefore adoptionf4955ad
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`export const normalizeStdio = options => { if (!options) { return [undefined, undefined, undefined]; }
const {stdio} = options;
if (stdio === undefined) { return aliases.map(alias => options[alias]); }
if (hasAlias(options)) { 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, stdio, stdio]; }
if (!Array.isArray(stdio)) { throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``); }
const length = Math.max(stdio.length, aliases.length); return Array.from({length}, (value, index) => stdio[index]);};
const hasAlias = options => aliases.some(alias => options[alias] !== undefined);
const aliases = ['stdin', 'stdout', 'stderr'];
// Same but for `execaNode()`, i.e. push `ipc` unless already presentexport const normalizeStdioNode = options => { const stdio = normalizeStdio(options); return stdio.includes('ipc') ? stdio : [...stdio, 'ipc'];};