forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawn-command.js
More file actions
29 lines (26 loc) 路 1001 Bytes
/
Copy pathspawn-command.js
File metadata and controls
29 lines (26 loc) 路 1001 Bytes
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
/**
* Build a shell-free child-process invocation for Unix and Windows.
*
* Windows `.cmd`/`.bat` files are not directly executable with CreateProcess.
* Routing only those extensions through ComSpec keeps normal commands on the
* safe direct-spawn path while preserving PATH lookup for PowerShell/cmd
* workflows used by the CLI.
*/
function quoteWindowsArg(value) {
const text = String(value);
if (!/[\s"&|<>^]/.test(text)) return text;
return `"${text.replace(/["^]/g, (match) => `^${match}`)}"`;
}
function buildSpawnSpec(command, args = []) {
const isWindowsScript = process.platform === 'win32' && /\.(?:cmd|bat)$/i.test(command);
if (!isWindowsScript) {
return { command, args, options: {} };
}
const shellCommand = [command, ...args].map(quoteWindowsArg).join(' ');
return {
command: process.env.ComSpec || 'cmd.exe',
args: ['/d', '/s', '/c', shellCommand],
options: { windowsHide: true },
};
}
module.exports = { buildSpawnSpec, quoteWindowsArg };