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
Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 1–3why, one step back1// Add support for `stdin`/`stdout`/`stderr` as an alias for `stdio`2export const normalizeStdio = options => {3 if (!options) {Refactor `stdio` normalization logic (#638)ehmickybefore adoptionf4955ad
line 4why, one step back4 return [undefined, undefined, undefined];Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 5–17why, one step back5 }67 const {stdio} = options;89 if (stdio === undefined) {10 return aliases.map(alias => options[alias]);11 }1213 if (hasAlias(options)) {14 throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`);15 }1617 if (typeof stdio === 'string') {Refactor `stdio` normalization logic (#638)ehmickybefore adoptionf4955ad
line 18why, one step back18 return [stdio, stdio, stdio];Refactor `stdio` options (#623)ehmickybefore adoption2d76897
lines 19–35why, one step back19 }2021 if (!Array.isArray(stdio)) {22 throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``);23 }2425 const length = Math.max(stdio.length, aliases.length);26 return Array.from({length}, (value, index) => stdio[index]);27};2829const hasAlias = options => aliases.some(alias => options[alias] !== undefined);3031const aliases = ['stdin', 'stdout', 'stderr'];3233// Same but for `execaNode()`, i.e. push `ipc` unless already present34export const normalizeStdioNode = options => {35 const stdio = normalizeStdio(options);Refactor `stdio` normalization logic (#638)ehmickybefore adoptionf4955ad
line 36why, one step back36 return stdio.includes('ipc') ? stdio : [...stdio, 'ipc'];Refactor `stdio` options (#623)ehmickybefore adoption2d76897
line 37why, one step back37};