diff --git a/lib/stdio.js b/lib/stdio.js index d9f5458b4d..bef74aeead 100644 --- a/lib/stdio.js +++ b/lib/stdio.js @@ -1,43 +1,33 @@ 'use strict'; -const alias = ['stdin', 'stdout', 'stderr']; +const aliases = ['stdin', 'stdout', 'stderr']; -const hasAlias = opts => alias.some(x => Boolean(opts[x])); +const hasAlias = opts => aliases.some(alias => Boolean(opts[alias])); const stdio = opts => { if (!opts) { return; } - if (opts.stdio && hasAlias(opts)) { - throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${alias.map(x => `\`${x}\``).join(', ')}`); + const {stdio} = opts; + + if (stdio === undefined) { + return aliases.map(alias => opts[alias]); } - if (typeof opts.stdio === 'string') { - return opts.stdio; + if (hasAlias(opts)) { + throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`); } - const stdio = opts.stdio || []; + if (typeof stdio === 'string') { + return stdio; + } if (!Array.isArray(stdio)) { throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``); } - const result = []; - const len = Math.max(stdio.length, alias.length); - - for (let i = 0; i < len; i++) { - let value; - - if (stdio[i] !== undefined) { - value = stdio[i]; - } else if (opts[alias[i]] !== undefined) { - value = opts[alias[i]]; - } - - result[i] = value; - } - - return result; + const length = Math.max(stdio.length, aliases.length); + return Array.from({length}, (value, index) => stdio[index]); }; module.exports = stdio;