stdio.js
1 'use strict'; 2 const aliases = ['stdin', 'stdout', 'stderr']; 3 4 const hasAlias = opts => aliases.some(alias => opts[alias] !== undefined); 5 6 const normalizeStdio = opts => { 7 if (!opts) { 8 return; 9 } 10 11 const {stdio} = opts; 12 13 if (stdio === undefined) { 14 return aliases.map(alias => opts[alias]); 15 } 16 17 if (hasAlias(opts)) { 18 throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`); 19 } 20 21 if (typeof stdio === 'string') { 22 return stdio; 23 } 24 25 if (!Array.isArray(stdio)) { 26 throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``); 27 } 28 29 const length = Math.max(stdio.length, aliases.length); 30 return Array.from({length}, (value, index) => stdio[index]); 31 }; 32 33 module.exports = normalizeStdio; 34 35 // `ipc` is pushed unless it is already present 36 module.exports.node = opts => { 37 const stdio = normalizeStdio(opts); 38 39 if (stdio === 'ipc') { 40 return 'ipc'; 41 } 42 43 if (stdio === undefined || typeof stdio === 'string') { 44 return [stdio, stdio, stdio, 'ipc']; 45 } 46 47 if (stdio.includes('ipc')) { 48 return stdio; 49 } 50 51 return [...stdio, 'ipc']; 52 };