refactor(coding-agent): inline npm command parsing

This commit is contained in:
Armin Ronacher
2026-05-01 18:52:54 +02:00
parent ade08de14c
commit c3282e4a7d

View File

@@ -57,11 +57,6 @@ export function detectInstallMethod(): InstallMethod {
return "unknown"; return "unknown";
} }
function getNpmCommand(npmCommand?: string[]): { command: string; args: string[] } {
const [command = "npm", ...args] = npmCommand ?? [];
return { command, args };
}
function getInferredNpmInstall(packageName: string): { root: string; prefix: string } | undefined { function getInferredNpmInstall(packageName: string): { root: string; prefix: string } | undefined {
const packageDir = getPackageDir(); const packageDir = getPackageDir();
const path = process.platform === "win32" || packageDir.includes("\\") ? win32 : { basename, dirname }; const path = process.platform === "win32" || packageDir.includes("\\") ? win32 : { basename, dirname };
@@ -101,11 +96,11 @@ function getSelfUpdateCommandForMethod(
case "bun": case "bun":
return { command: "bun", args: ["install", "-g", packageName], display: `bun install -g ${packageName}` }; return { command: "bun", args: ["install", "-g", packageName], display: `bun install -g ${packageName}` };
case "npm": { case "npm": {
const npm = getNpmCommand(npmCommand); const [command = "npm", ...npmArgs] = npmCommand ?? [];
const inferred = npmCommand?.length ? undefined : getInferredNpmInstall(packageName); const inferred = npmCommand?.length ? undefined : getInferredNpmInstall(packageName);
const args = [...npm.args, ...(inferred ? ["--prefix", inferred.prefix] : []), "install", "-g", packageName]; const args = [...npmArgs, ...(inferred ? ["--prefix", inferred.prefix] : []), "install", "-g", packageName];
const display = [npm.command, ...args].map((arg) => (/\s/.test(arg) ? `"${arg}"` : arg)).join(" "); const display = [command, ...args].map((arg) => (/\s/.test(arg) ? `"${arg}"` : arg)).join(" ");
return { command: npm.command, args, display }; return { command, args, display };
} }
case "unknown": case "unknown":
return undefined; return undefined;
@@ -133,10 +128,10 @@ function readCommandOutput(
function getGlobalPackageRoots(method: InstallMethod, packageName: string, npmCommand?: string[]): string[] { function getGlobalPackageRoots(method: InstallMethod, packageName: string, npmCommand?: string[]): string[] {
switch (method) { switch (method) {
case "npm": { case "npm": {
const npm = getNpmCommand(npmCommand);
const configured = !!npmCommand?.length; const configured = !!npmCommand?.length;
if (configured && npm.command === "bun") { const [command = "npm", ...npmArgs] = npmCommand ?? [];
const bunBin = readCommandOutput(npm.command, [...npm.args, "pm", "bin", "-g"], { if (configured && command === "bun") {
const bunBin = readCommandOutput(command, [...npmArgs, "pm", "bin", "-g"], {
requireSuccess: true, requireSuccess: true,
}); });
const roots = [join(homedir(), ".bun", "install", "global", "node_modules")]; const roots = [join(homedir(), ".bun", "install", "global", "node_modules")];
@@ -145,7 +140,7 @@ function getGlobalPackageRoots(method: InstallMethod, packageName: string, npmCo
} }
return roots; return roots;
} }
const root = readCommandOutput(npm.command, [...npm.args, "root", "-g"], { const root = readCommandOutput(command, [...npmArgs, "root", "-g"], {
requireSuccess: configured, requireSuccess: configured,
}); });
const inferred = configured ? undefined : getInferredNpmInstall(packageName); const inferred = configured ? undefined : getInferredNpmInstall(packageName);