diff --git a/src/core/pm.ts b/src/core/pm.ts new file mode 100644 index 0000000..bf32cc8 --- /dev/null +++ b/src/core/pm.ts @@ -0,0 +1,58 @@ +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"; + if (userAgent?.includes("pnpm")) return "pnpx @lanyuanxiaoyao/rune"; + if (userAgent?.includes("npm")) return "npx @lanyuanxiaoyao/rune"; + return null; +} + +export async function checkCommandAvailable(command: string): Promise { + const which = process.platform === "win32" ? "where" : "which"; + try { + const proc = Bun.spawn([which, command], { + stdout: "ignore", + stderr: "ignore", + }); + const exitCode = await proc.exited; + return exitCode === 0; + } catch { + return false; + } +} + +export async function detectCommandPrefix(): Promise { + if (await checkCommandAvailable("rune")) return "rune"; + + const inferred = inferFromEnvironment(process.execPath, process.env.npm_config_user_agent); + if (inferred) return inferred; + + for (const pm of ["bunx", "pnpx", "npx"]) { + if (await checkCommandAvailable(pm)) return `${pm} @lanyuanxiaoyao/rune`; + } + + return null; +} + +export function getPmPrefix(config?: RuneConfig): string { + return config?.metadata?.command ?? DEFAULT_PREFIX; +} + +export function getFallbackNote(): string { + return `如果没有安装 bun,可使用 \`pnpx @lanyuanxiaoyao/rune\` 或 \`npx @lanyuanxiaoyao/rune\` 替代`; +} + +export function applyCommandPrefix(text: string, config?: RuneConfig): string { + const prefix = getPmPrefix(config); + const hasCommand = /\brune(?=\s)/.test(text); + const result = text.replace(/\brune(?=\s)/g, prefix); + if (!config?.metadata?.command && hasCommand) { + return result + "\n\n" + getFallbackNote(); + } + return result; +} diff --git a/src/types.ts b/src/types.ts index e5f62dc..a6af50b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -36,6 +36,9 @@ export interface StagesConfig { export interface RuneConfig { stages: StagesConfig; + metadata?: { + command?: string; + }; } export interface TaskItem { diff --git a/tests/core/pm.test.ts b/tests/core/pm.test.ts new file mode 100644 index 0000000..3e0374c --- /dev/null +++ b/tests/core/pm.test.ts @@ -0,0 +1,112 @@ +import { describe, it, expect } from "bun:test"; +import { + inferFromEnvironment, + getPmPrefix, + DEFAULT_PREFIX, + getFallbackNote, + applyCommandPrefix, +} from "../../src/core/pm.ts"; +import type { RuneConfig } from "../../src/types.ts"; + +describe("inferFromEnvironment", () => { + it("execPath 包含 bun 时返回 bunx 前缀", () => { + const result = inferFromEnvironment("/home/user/.bun/bin/bun", undefined); + expect(result).toBe("bunx @lanyuanxiaoyao/rune"); + }); + + it("npm_config_user_agent 包含 pnpm 时返回 pnpx 前缀", () => { + const result = inferFromEnvironment("/usr/bin/node", "pnpm/9.0.0"); + expect(result).toBe("pnpx @lanyuanxiaoyao/rune"); + }); + + it("npm_config_user_agent 包含 npm 时返回 npx 前缀", () => { + const result = inferFromEnvironment("/usr/bin/node", "npm/10.0.0"); + expect(result).toBe("npx @lanyuanxiaoyao/rune"); + }); + + it("pnpm 优先于 npm(userAgent 同时包含两者时)", () => { + const result = inferFromEnvironment("/usr/bin/node", "pnpm/9.0.0 npm/10.0.0"); + expect(result).toBe("pnpx @lanyuanxiaoyao/rune"); + }); + + it("无法推断时返回 null", () => { + const result = inferFromEnvironment("/usr/bin/node", undefined); + expect(result).toBeNull(); + }); +}); + +describe("getPmPrefix", () => { + it("有配置时返回 config.metadata.command", () => { + const config: RuneConfig = { + stages: {}, + metadata: { command: "rune" }, + }; + expect(getPmPrefix(config)).toBe("rune"); + }); + + it("配置中无 metadata 时返回默认前缀", () => { + const config: RuneConfig = { stages: {} }; + expect(getPmPrefix(config)).toBe(DEFAULT_PREFIX); + }); + + it("不传配置时返回默认前缀", () => { + expect(getPmPrefix()).toBe(DEFAULT_PREFIX); + expect(getPmPrefix(undefined)).toBe("bunx @lanyuanxiaoyao/rune"); + }); +}); + +describe("getFallbackNote", () => { + it("返回降级说明文本", () => { + const note = getFallbackNote(); + expect(note).toContain("pnpx @lanyuanxiaoyao/rune"); + expect(note).toContain("npx @lanyuanxiaoyao/rune"); + }); +}); + +describe("applyCommandPrefix", () => { + it("有配置时替换 rune 为配置前缀", () => { + const config: RuneConfig = { + stages: {}, + metadata: { command: "rune" }, + }; + const result = applyCommandPrefix("执行 rune status 查看", config); + expect(result).toBe("执行 rune status 查看"); + }); + + it("无配置时替换为默认前缀", () => { + const result = applyCommandPrefix("执行 rune status 查看", undefined); + expect(result).toContain("bunx @lanyuanxiaoyao/rune status"); + expect(result).not.toContain("执行 rune status"); + }); + + it("无配置且文本含命令时追加降级说明", () => { + const result = applyCommandPrefix("执行 rune status 查看", undefined); + expect(result).toContain("pnpx @lanyuanxiaoyao/rune"); + expect(result).toContain("如果没有安装 bun"); + }); + + it("有配置时不追加降级说明", () => { + const config: RuneConfig = { + stages: {}, + metadata: { command: "bunx @lanyuanxiaoyao/rune" }, + }; + const result = applyCommandPrefix("执行 rune status 查看", config); + expect(result).not.toContain("如果没有安装 bun"); + }); + + it("不替换 /rune- 形式(编辑器斜杠命令)", () => { + const text = "使用 /rune-plan 进入规划阶段,然后 rune build 开始构建"; + const config: RuneConfig = { + stages: {}, + metadata: { command: "bunx @lanyuanxiaoyao/rune" }, + }; + const result = applyCommandPrefix(text, config); + expect(result).toContain("/rune-plan"); + expect(result).toContain("bunx @lanyuanxiaoyao/rune build"); + }); + + it("文本不含 rune 命令时无配置也不追加降级说明", () => { + const result = applyCommandPrefix("纯文本无命令", undefined); + expect(result).toBe("纯文本无命令"); + }); +});