feat: 基于 process.argv 推断命令前缀,修复 bunx 误检测
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -404,3 +404,4 @@ temp
|
|||||||
.worktrees
|
.worktrees
|
||||||
!src/**/*
|
!src/**/*
|
||||||
docs/superpowers
|
docs/superpowers
|
||||||
|
tests/**/__tmp*
|
||||||
|
|||||||
@@ -2,14 +2,24 @@ import type { RuneConfig } from "../types.ts";
|
|||||||
|
|
||||||
export const DEFAULT_PREFIX = "bunx @lanyuanxiaoyao/rune";
|
export const DEFAULT_PREFIX = "bunx @lanyuanxiaoyao/rune";
|
||||||
|
|
||||||
export function inferFromEnvironment(
|
export function inferFromProcess(argv: string[], userAgent: string | undefined): string | null {
|
||||||
execPath: string,
|
const scriptPath = argv[1]?.replace(/\\/g, "/") ?? "";
|
||||||
userAgent: string | undefined,
|
|
||||||
): string | null {
|
if (scriptPath.includes(".bun") && scriptPath.includes("cache")) {
|
||||||
if (execPath.includes("bun")) return "bunx @lanyuanxiaoyao/rune";
|
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("pnpm")) return "pnpx @lanyuanxiaoyao/rune";
|
||||||
if (userAgent?.includes("yarn")) return "yarn dlx @lanyuanxiaoyao/rune";
|
if (userAgent?.includes("yarn")) return "yarn dlx @lanyuanxiaoyao/rune";
|
||||||
if (userAgent?.includes("npm")) return "npx @lanyuanxiaoyao/rune";
|
if (userAgent?.includes("npm")) return "npx @lanyuanxiaoyao/rune";
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,9 +38,7 @@ export async function checkCommandAvailable(command: string): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function detectCommandPrefix(): Promise<string | null> {
|
export async function detectCommandPrefix(): Promise<string | null> {
|
||||||
if (await checkCommandAvailable("rune")) return "rune";
|
const inferred = inferFromProcess(process.argv, process.env.npm_config_user_agent);
|
||||||
|
|
||||||
const inferred = inferFromEnvironment(process.execPath, process.env.npm_config_user_agent);
|
|
||||||
if (inferred) return inferred;
|
if (inferred) return inferred;
|
||||||
|
|
||||||
for (const pm of ["bunx", "pnpx", "npx"]) {
|
for (const pm of ["bunx", "pnpx", "npx"]) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { describe, it, expect } from "bun:test";
|
import { describe, it, expect } from "bun:test";
|
||||||
import {
|
import {
|
||||||
inferFromEnvironment,
|
inferFromProcess,
|
||||||
getPmPrefix,
|
getPmPrefix,
|
||||||
DEFAULT_PREFIX,
|
DEFAULT_PREFIX,
|
||||||
getFallbackNote,
|
getFallbackNote,
|
||||||
@@ -8,29 +8,105 @@ import {
|
|||||||
} from "../../src/core/pm.ts";
|
} from "../../src/core/pm.ts";
|
||||||
import type { RuneConfig } from "../../src/types.ts";
|
import type { RuneConfig } from "../../src/types.ts";
|
||||||
|
|
||||||
describe("inferFromEnvironment", () => {
|
describe("inferFromProcess", () => {
|
||||||
it("execPath 包含 bun 时返回 bunx 前缀", () => {
|
it("argv 包含 bun 缓存路径时返回 bunx 前缀", () => {
|
||||||
const result = inferFromEnvironment("/home/user/.bun/bin/bun", undefined);
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/bun", "/home/user/.bun/cache/@lanyuanxiaoyao-rune/bin/rune.ts"],
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
expect(result).toBe("bunx @lanyuanxiaoyao/rune");
|
expect(result).toBe("bunx @lanyuanxiaoyao/rune");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("npm_config_user_agent 包含 pnpm 时返回 pnpx 前缀", () => {
|
it("argv Windows 风格 bun 缓存路径时返回 bunx 前缀", () => {
|
||||||
const result = inferFromEnvironment("/usr/bin/node", "pnpm/9.0.0");
|
const result = inferFromProcess(
|
||||||
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
[
|
||||||
|
"C:\\Users\\user\\.bun\\bin\\bun.exe",
|
||||||
|
"C:\\Users\\user\\.bun\\cache\\@lanyuanxiaoyao-rune\\bin\\rune.ts",
|
||||||
|
],
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
expect(result).toBe("bunx @lanyuanxiaoyao/rune");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("npm_config_user_agent 包含 npm 时返回 npx 前缀", () => {
|
it("argv 包含 _npx 路径时返回 npx 前缀", () => {
|
||||||
const result = inferFromEnvironment("/usr/bin/node", "npm/10.0.0");
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/node", "/home/user/.npm/_npx/abc123/node_modules/.bin/rune"],
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
expect(result).toBe("npx @lanyuanxiaoyao/rune");
|
expect(result).toBe("npx @lanyuanxiaoyao/rune");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("pnpm 优先于 npm(userAgent 同时包含两者时)", () => {
|
it("argv 包含 npm-cache 路径时返回 npx 前缀", () => {
|
||||||
const result = inferFromEnvironment("/usr/bin/node", "pnpm/9.0.0 npm/10.0.0");
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/node", "/home/user/.npm-cache/_npx/abc123/bin/rune"],
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
expect(result).toBe("npx @lanyuanxiaoyao/rune");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("argv 包含 .pnpm 路径时返回 pnpx 前缀", () => {
|
||||||
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/node", "/home/user/.pnpm/store/@lanyuanxiaoyao-rune/bin/rune"],
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("无法推断时返回 null", () => {
|
it("argv 无法推断但 userAgent 包含 bun 时返回 bunx 前缀", () => {
|
||||||
const result = inferFromEnvironment("/usr/bin/node", undefined);
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/bun", "/home/user/projects/app/run.ts"],
|
||||||
|
"bun/1.0.0",
|
||||||
|
);
|
||||||
|
expect(result).toBe("bunx @lanyuanxiaoyao/rune");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("argv 无法推断但 userAgent 包含 pnpm 时返回 pnpx 前缀", () => {
|
||||||
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||||
|
"pnpm/9.0.0",
|
||||||
|
);
|
||||||
|
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("argv 无法推断但 userAgent 包含 yarn 时返回 yarn dlx 前缀", () => {
|
||||||
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||||
|
"yarn/4.0.0",
|
||||||
|
);
|
||||||
|
expect(result).toBe("yarn dlx @lanyuanxiaoyao/rune");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("argv 无法推断但 userAgent 包含 npm 时返回 npx 前缀", () => {
|
||||||
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||||
|
"npm/10.0.0",
|
||||||
|
);
|
||||||
|
expect(result).toBe("npx @lanyuanxiaoyao/rune");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("userAgent 同时包含 pnpm 和 npm 时 pnpm 优先", () => {
|
||||||
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||||
|
"pnpm/9.0.0 npm/10.0.0",
|
||||||
|
);
|
||||||
|
expect(result).toBe("pnpx @lanyuanxiaoyao/rune");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("argv 和 userAgent 都无法推断时返回 null", () => {
|
||||||
|
const result = inferFromProcess(
|
||||||
|
["/usr/local/bin/node", "/home/user/projects/app/run.ts"],
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("argv 为空数组且无 userAgent 时返回 null", () => {
|
||||||
|
const result = inferFromProcess([], undefined);
|
||||||
|
expect(result).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("argv 只有一个元素且无 userAgent 时返回 null", () => {
|
||||||
|
const result = inferFromProcess(["/usr/local/bin/node"], undefined);
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user