UNPKG

1.16 kBJavaScriptView Raw
1'use strict';
2const aliases = ['stdin', 'stdout', 'stderr'];
3
4const hasAlias = opts => aliases.some(alias => opts[alias] !== undefined);
5
6const 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
33module.exports = normalizeStdio;
34
35// `ipc` is pushed unless it is already present
36module.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};