/
node.js
51 lines (44 loc) · 1.61 KB
/
node.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
import {execPath, execArgv} from 'node:process';
import {basename, resolve} from 'node:path';
import {safeNormalizeFileUrl} from '../arguments/file-url.js';
// `execaNode()` is a shortcut for `execa(..., {node: true})`
export const mapNode = ({options}) => {
if (options.node === false) {
throw new TypeError('The "node" option cannot be false with `execaNode()`.');
}
return {options: {...options, node: true}};
};
// Applies the `node: true` option, and the related `nodePath`/`nodeOptions` options
// Modifies the file commands/arguments to ensure the same Node binary and flags are re-used.
// Also adds `ipc: true` and `shell: false`.
export const handleNodeOption = (file, commandArguments, {
node: shouldHandleNode = false,
nodePath = execPath,
nodeOptions = execArgv.filter(nodeOption => !nodeOption.startsWith('--inspect')),
cwd,
execPath: formerNodePath,
...options
}) => {
if (formerNodePath !== undefined) {
throw new TypeError('The "execPath" option has been removed. Please use the "nodePath" option instead.');
}
const normalizedNodePath = safeNormalizeFileUrl(nodePath, 'The "nodePath" option');
const resolvedNodePath = resolve(cwd, normalizedNodePath);
const newOptions = {
...options,
nodePath: resolvedNodePath,
node: shouldHandleNode,
cwd,
};
if (!shouldHandleNode) {
return [file, commandArguments, newOptions];
}
if (basename(file, '.exe') === 'node') {
throw new TypeError('When the "node" option is true, the first argument does not need to be "node".');
}
return [
resolvedNodePath,
[...nodeOptions, file, ...commandArguments],
{ipc: true, ...newOptions, shell: false},
];
};