From c63912dc0d529cb6a294c139e5e57c9f5f681dfc Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Mon, 8 Jun 2026 22:34:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20init=20=E5=91=BD=E4=BB=A4=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20CommandError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/init.ts | 5 ++++- tests/commands/init.test.ts | 13 +++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index 6d4993a..88aaca9 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -4,6 +4,7 @@ import { join } from "node:path"; import { CHANGES_DIR, ARCHIVE_DIR, RUNE_DIR, CONFIG_FILE } from "../types.ts"; import { injectOpenCode } from "../adapters/opencode.ts"; import { injectClaudeCode } from "../adapters/claude-code.ts"; +import { CommandError } from "../cli/errors.ts"; const SUPPORTED_TOOLS: Record Promise> = { opencode: injectOpenCode, @@ -16,7 +17,9 @@ export async function runInit( ): Promise { for (const tool of tools) { if (!SUPPORTED_TOOLS[tool]) { - throw new Error(`不支持的工具: ${tool}`); + throw new CommandError(`不支持的工具: ${tool}`, { + hint: `支持的工具: ${Object.keys(SUPPORTED_TOOLS).join(", ")}`, + }); } } diff --git a/tests/commands/init.test.ts b/tests/commands/init.test.ts index c06d008..3053d76 100644 --- a/tests/commands/init.test.ts +++ b/tests/commands/init.test.ts @@ -3,6 +3,7 @@ import { existsSync } from "node:fs"; import { mkdir, rm, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { runInit } from "../../src/commands/init.ts"; +import { CommandError } from "../../src/cli/errors.ts"; const TMP_DIR = join(import.meta.dir, "__tmp_init_test__"); @@ -57,9 +58,13 @@ describe("runInit", () => { expect(content).toBe("自定义内容"); }); - it("不支持的工具名抛出错误", async () => { - await expect(runInit(TMP_DIR, ["unknown-tool"])).rejects.toThrow( - "不支持的工具", - ); + it("不支持的工具名抛出 CommandError", async () => { + try { + await runInit(TMP_DIR, ["unknown-tool"]); + expect.unreachable("应抛出错误"); + } catch (e) { + expect(e).toBeInstanceOf(CommandError); + expect((e as CommandError).message).toContain("不支持的工具: unknown-tool"); + } }); });