import { existsSync } from "node:fs"; import { mkdir, writeFile } from "node:fs/promises"; 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, "claude-code": injectClaudeCode, }; export async function runInit( projectRoot: string, tools: string[], ): Promise { for (const tool of tools) { if (!SUPPORTED_TOOLS[tool]) { throw new CommandError(`不支持的工具: ${tool}`, { hint: `支持的工具: ${Object.keys(SUPPORTED_TOOLS).join(", ")}`, }); } } const runeDir = join(projectRoot, RUNE_DIR); await mkdir(runeDir, { recursive: true }); await mkdir(join(runeDir, CHANGES_DIR), { recursive: true }); await mkdir(join(runeDir, ARCHIVE_DIR), { recursive: true }); const configPath = join(runeDir, CONFIG_FILE); if (!existsSync(configPath)) { await writeFile(configPath, "stages: {}\n", "utf-8"); } for (const tool of tools) { await SUPPORTED_TOOLS[tool](projectRoot); } }