-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
fd-options.js
108 lines (84 loc) · 3.88 KB
/
fd-options.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {parseFd} from './specific.js';
// Retrieve stream targeted by the `to` option
export const getToStream = (destination, to = 'stdin') => {
const isWritable = true;
const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(destination);
const fdNumber = getFdNumber(fileDescriptors, to, isWritable);
const destinationStream = destination.stdio[fdNumber];
if (destinationStream === null) {
throw new TypeError(getInvalidStdioOptionMessage(fdNumber, to, options, isWritable));
}
return destinationStream;
};
// Retrieve stream targeted by the `from` option
export const getFromStream = (source, from = 'stdout') => {
const isWritable = false;
const {options, fileDescriptors} = SUBPROCESS_OPTIONS.get(source);
const fdNumber = getFdNumber(fileDescriptors, from, isWritable);
const sourceStream = fdNumber === 'all' ? source.all : source.stdio[fdNumber];
if (sourceStream === null || sourceStream === undefined) {
throw new TypeError(getInvalidStdioOptionMessage(fdNumber, from, options, isWritable));
}
return sourceStream;
};
// Keeps track of the options passed to each Execa call
export const SUBPROCESS_OPTIONS = new WeakMap();
const getFdNumber = (fileDescriptors, fdName, isWritable) => {
const fdNumber = parseFdNumber(fdName, isWritable);
validateFdNumber(fdNumber, fdName, isWritable, fileDescriptors);
return fdNumber;
};
const parseFdNumber = (fdName, isWritable) => {
const fdNumber = parseFd(fdName);
if (fdNumber !== undefined) {
return fdNumber;
}
const {validOptions, defaultValue} = isWritable
? {validOptions: '"stdin"', defaultValue: 'stdin'}
: {validOptions: '"stdout", "stderr", "all"', defaultValue: 'stdout'};
throw new TypeError(`"${getOptionName(isWritable)}" must not be "${fdName}".
It must be ${validOptions} or "fd3", "fd4" (and so on).
It is optional and defaults to "${defaultValue}".`);
};
const validateFdNumber = (fdNumber, fdName, isWritable, fileDescriptors) => {
const fileDescriptor = fileDescriptors[getUsedDescriptor(fdNumber)];
if (fileDescriptor === undefined) {
throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. That file descriptor does not exist.
Please set the "stdio" option to ensure that file descriptor exists.`);
}
if (fileDescriptor.direction === 'input' && !isWritable) {
throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a readable stream, not writable.`);
}
if (fileDescriptor.direction !== 'input' && isWritable) {
throw new TypeError(`"${getOptionName(isWritable)}" must not be ${fdName}. It must be a writable stream, not readable.`);
}
};
const getInvalidStdioOptionMessage = (fdNumber, fdName, options, isWritable) => {
if (fdNumber === 'all' && !options.all) {
return 'The "all" option must be true to use "from: \'all\'".';
}
const {optionName, optionValue} = getInvalidStdioOption(fdNumber, options);
return `The "${optionName}: ${serializeOptionValue(optionValue)}" option is incompatible with using "${getOptionName(isWritable)}: ${serializeOptionValue(fdName)}".
Please set this option with "pipe" instead.`;
};
const getInvalidStdioOption = (fdNumber, {stdin, stdout, stderr, stdio}) => {
const usedDescriptor = getUsedDescriptor(fdNumber);
if (usedDescriptor === 0 && stdin !== undefined) {
return {optionName: 'stdin', optionValue: stdin};
}
if (usedDescriptor === 1 && stdout !== undefined) {
return {optionName: 'stdout', optionValue: stdout};
}
if (usedDescriptor === 2 && stderr !== undefined) {
return {optionName: 'stderr', optionValue: stderr};
}
return {optionName: `stdio[${usedDescriptor}]`, optionValue: stdio[usedDescriptor]};
};
const getUsedDescriptor = fdNumber => fdNumber === 'all' ? 1 : fdNumber;
const getOptionName = isWritable => isWritable ? 'to' : 'from';
export const serializeOptionValue = value => {
if (typeof value === 'string') {
return `'${value}'`;
}
return typeof value === 'number' ? `${value}` : 'Stream';
};