| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const alias = ['stdin', 'stdout', 'stderr']; |
| 3 | |
| 4 | const hasAlias = opts => alias.some(x => Boolean(opts[x])); |
| 5 | |
| 6 | module.exports = opts => { |
| 7 | if (!opts) { |
| 8 | return null; |
| 9 | } |
| 10 | |
| 11 | if (opts.stdio && hasAlias(opts)) { |
| 12 | throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${alias.map(x => `\`${x}\``).join(', ')}`); |
| 13 | } |
| 14 | |
| 15 | if (typeof opts.stdio === 'string') { |
| 16 | return opts.stdio; |
| 17 | } |
| 18 | |
| 19 | const stdio = opts.stdio || []; |
| 20 | |
| 21 | if (!Array.isArray(stdio)) { |
| 22 | throw new TypeError(`Expected \`stdio\` to be of type \`string\` or \`Array\`, got \`${typeof stdio}\``); |
| 23 | } |
| 24 | |
| 25 | const result = []; |
| 26 | const len = Math.max(stdio.length, alias.length); |
| 27 | |
| 28 | for (let i = 0; i < len; i++) { |
| 29 | let value = null; |
| 30 | |
| 31 | if (stdio[i] !== undefined) { |
| 32 | value = stdio[i]; |
| 33 | } else if (opts[alias[i]] !== undefined) { |
| 34 | value = opts[alias[i]]; |
| 35 | } |
| 36 | |
| 37 | result[i] = value; |
| 38 | } |
| 39 | |
| 40 | return result; |
| 41 | }; |