option.js
This file
Path
lib/stdio/option.js
40 lines · 1.4 KiB · JavaScript
Last changed by
Fix `stdio: [..., undefined]` option (#948)ehmickybefore adoption96d2ef7
History of this fileTake it
Split files (#837)ehmickybefore adoption672c2d7
lines 1–4why, one step back1import {STANDARD_STREAMS_ALIASES} from '../utils.js';23// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`4export const normalizeStdio = ({stdio, ipc, ...options}) => {Fix `stdio: [..., undefined]` option (#948)ehmickybefore adoption96d2ef7
line 5why, one step back5 const stdioArray = getStdioArray(stdio, options).map((stdioOption, fdNumber) => addDefaultValue(stdioOption, fdNumber));Split files (#837)ehmickybefore adoption672c2d7
lines 6–28why, one step back6 return ipc && !stdioArray.includes('ipc')7 ? [...stdioArray, 'ipc']8 : stdioArray;9};1011const getStdioArray = (stdio, options) => {12 if (stdio === undefined) {13 return STANDARD_STREAMS_ALIASES.map(alias => options[alias]);14 }1516 if (hasAlias(options)) {17 throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${STANDARD_STREAMS_ALIASES.map(alias => `\`${alias}\``).join(', ')}`);18 }1920 if (typeof stdio === 'string') {21 return [stdio, stdio, stdio];22 }2324 if (!Array.isArray(stdio)) {25 throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);26 }2728 const length = Math.max(stdio.length, STANDARD_STREAMS_ALIASES.length);Fix `stdio: [..., undefined]` option (#948)ehmickybefore adoption96d2ef7
line 29why, one step back29 return Array.from({length}, (_, fdNumber) => stdio[fdNumber]);Split files (#837)ehmickybefore adoption672c2d7
lines 30–32why, one step back30};3132const hasAlias = options => STANDARD_STREAMS_ALIASES.some(alias => options[alias] !== undefined);Fix `stdio: [..., undefined]` option (#948)ehmickybefore adoption96d2ef7
lines 33–40why, one step back3334const addDefaultValue = (stdioOption, fdNumber) => {35 if (stdioOption === null || stdioOption === undefined) {36 return fdNumber >= STANDARD_STREAMS_ALIASES.length ? 'ignore' : 'pipe';37 }3839 return stdioOption;40};