liusuyi
2023-04-24 4737f1e038743ced243c9e52423404d9034d6107
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
'use strict';
const aliases = ['stdin', 'stdout', 'stderr'];
 
const hasAlias = opts => aliases.some(alias => opts[alias] !== undefined);
 
const normalizeStdio = opts => {
    if (!opts) {
        return;
    }
 
    const {stdio} = opts;
 
    if (stdio === undefined) {
        return aliases.map(alias => opts[alias]);
    }
 
    if (hasAlias(opts)) {
        throw new Error(`It's not possible to provide \`stdio\` in combination with one of ${aliases.map(alias => `\`${alias}\``).join(', ')}`);
    }
 
    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 length = Math.max(stdio.length, aliases.length);
    return Array.from({length}, (value, index) => stdio[index]);
};
 
module.exports = normalizeStdio;
 
// `ipc` is pushed unless it is already present
module.exports.node = opts => {
    const stdio = normalizeStdio(opts);
 
    if (stdio === 'ipc') {
        return 'ipc';
    }
 
    if (stdio === undefined || typeof stdio === 'string') {
        return [stdio, stdio, stdio, 'ipc'];
    }
 
    if (stdio.includes('ipc')) {
        return stdio;
    }
 
    return [...stdio, 'ipc'];
};