mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-04 08:27:35 +02:00
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
import path from "node:path";
|
|
|
|
const WINDOWS_UNSAFE_CMD_CHARS_RE = /[&|<>%\r\n]/;
|
|
|
|
function isPnpmExecPath(value) {
|
|
return /^pnpm(?:-cli)?(?:\.(?:c?js|cmd|exe))?$/.test(path.basename(value).toLowerCase());
|
|
}
|
|
|
|
function escapeForCmdExe(arg) {
|
|
if (WINDOWS_UNSAFE_CMD_CHARS_RE.test(arg)) {
|
|
throw new Error(`unsafe Windows cmd.exe argument detected: ${JSON.stringify(arg)}`);
|
|
}
|
|
const escaped = arg.replace(/\^/g, "^^");
|
|
if (!escaped.includes(" ") && !escaped.includes('"')) {
|
|
return escaped;
|
|
}
|
|
return `"${escaped.replace(/"/g, '""')}"`;
|
|
}
|
|
|
|
function buildCmdExeCommandLine(command, args) {
|
|
return [escapeForCmdExe(command), ...args.map(escapeForCmdExe)].join(" ");
|
|
}
|
|
|
|
export function resolvePnpmRunner(params = {}) {
|
|
const pnpmArgs = params.pnpmArgs ?? [];
|
|
const npmExecPath = params.npmExecPath ?? process.env.npm_execpath;
|
|
const nodeExecPath = params.nodeExecPath ?? process.execPath;
|
|
const platform = params.platform ?? process.platform;
|
|
const comSpec = params.comSpec ?? process.env.ComSpec ?? "cmd.exe";
|
|
|
|
if (typeof npmExecPath === "string" && npmExecPath.length > 0 && isPnpmExecPath(npmExecPath)) {
|
|
return {
|
|
command: nodeExecPath,
|
|
args: [npmExecPath, ...pnpmArgs],
|
|
shell: false,
|
|
};
|
|
}
|
|
|
|
if (platform === "win32") {
|
|
return {
|
|
command: comSpec,
|
|
args: ["/d", "/s", "/c", buildCmdExeCommandLine("pnpm.cmd", pnpmArgs)],
|
|
shell: false,
|
|
windowsVerbatimArguments: true,
|
|
};
|
|
}
|
|
|
|
return {
|
|
command: "pnpm",
|
|
args: pnpmArgs,
|
|
shell: false,
|
|
};
|
|
}
|