forked from sindresorhus/execa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdio.js
61 lines (44 loc) · 1.3 KB
/
stdio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const alias = ['stdin', 'stdout', 'stderr'];
const hasAlias = opts => alias.some(x => Boolean(opts[x]));
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(', ')}`);
}
if (typeof opts.stdio === 'string') {
return opts.stdio;
}
const stdio = opts.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;
};
module.exports = stdio;
module.exports.node = opts => {
const defaultOption = 'pipe';
let stdioOption = stdio(opts || {stdio: defaultOption});
if (typeof stdioOption === 'string') {
stdioOption = [...new Array(3)].fill(stdioOption);
} else if (Array.isArray(stdioOption)) {
stdioOption = stdioOption.map((channel = defaultOption) => channel);
}
if (!stdioOption.includes('ipc')) {
stdioOption.push('ipc');
}
return stdioOption;
};