refactor: dedupe windows cmd runner helpers

This commit is contained in:
Peter Steinberger
2026-04-06 16:51:18 +01:00
parent ad9481e2d1
commit c88870ac93
3 changed files with 22 additions and 38 deletions

View File

@@ -1,26 +1,6 @@
import fs from "node:fs";
import path from "node:path";
const WINDOWS_UNSAFE_CMD_CHARS_RE = /[&|<>%\r\n]/;
function resolvePathEnvKey(env) {
return Object.keys(env).find((key) => key.toLowerCase() === "path") ?? "PATH";
}
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(" ");
}
import { buildCmdExeCommandLine, resolvePathEnvKey } from "./windows-cmd-helpers.mjs";
function resolveToolchainNpmRunner(params) {
const npmCliCandidates = [

View File

@@ -1,27 +1,11 @@
import { spawn } from "node:child_process";
import path from "node:path";
const WINDOWS_UNSAFE_CMD_CHARS_RE = /[&|<>%\r\n]/;
import { buildCmdExeCommandLine } from "./windows-cmd-helpers.mjs";
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 nodeArgs = params.nodeArgs ?? [];

View File

@@ -0,0 +1,20 @@
const WINDOWS_UNSAFE_CMD_CHARS_RE = /[&|<>%\r\n]/;
export function resolvePathEnvKey(env) {
return Object.keys(env).find((key) => key.toLowerCase() === "path") ?? "PATH";
}
export 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, '""')}"`;
}
export function buildCmdExeCommandLine(command, args) {
return [escapeForCmdExe(command), ...args.map(escapeForCmdExe)].join(" ");
}