feat: 基于 process.argv 推断命令前缀,修复 bunx 误检测

This commit is contained in:
2026-06-11 23:06:14 +08:00
parent ddd1c41c00
commit ecccf5eef0
3 changed files with 106 additions and 21 deletions

View File

@@ -2,14 +2,24 @@ import type { RuneConfig } from "../types.ts";
export const DEFAULT_PREFIX = "bunx @lanyuanxiaoyao/rune";
export function inferFromEnvironment(
execPath: string,
userAgent: string | undefined,
): string | null {
if (execPath.includes("bun")) return "bunx @lanyuanxiaoyao/rune";
export function inferFromProcess(argv: string[], userAgent: string | undefined): string | null {
const scriptPath = argv[1]?.replace(/\\/g, "/") ?? "";
if (scriptPath.includes(".bun") && scriptPath.includes("cache")) {
return "bunx @lanyuanxiaoyao/rune";
}
if (scriptPath.includes("_npx") || scriptPath.includes("npm-cache")) {
return "npx @lanyuanxiaoyao/rune";
}
if (scriptPath.includes(".pnpm") || scriptPath.includes("pnpm-store")) {
return "pnpx @lanyuanxiaoyao/rune";
}
if (userAgent?.includes("bun")) return "bunx @lanyuanxiaoyao/rune";
if (userAgent?.includes("pnpm")) return "pnpx @lanyuanxiaoyao/rune";
if (userAgent?.includes("yarn")) return "yarn dlx @lanyuanxiaoyao/rune";
if (userAgent?.includes("npm")) return "npx @lanyuanxiaoyao/rune";
return null;
}
@@ -28,9 +38,7 @@ export async function checkCommandAvailable(command: string): Promise<boolean> {
}
export async function detectCommandPrefix(): Promise<string | null> {
if (await checkCommandAvailable("rune")) return "rune";
const inferred = inferFromEnvironment(process.execPath, process.env.npm_config_user_agent);
const inferred = inferFromProcess(process.argv, process.env.npm_config_user_agent);
if (inferred) return inferred;
for (const pm of ["bunx", "pnpx", "npx"]) {