Files
Rune-Spec/src/commands/init.ts

69 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 CONFIG_TEMPLATE = `# Rune 配置文件
#
# 未配置的阶段将使用内置默认配置。
# 阶段顺序discuss -> plan -> build -> archive
#
# 可配置阶段:
# discuss - 探索阶段:深度思考、调查代码库、对比方案
# plan - 规划阶段:生成设计文档和任务清单
# build - 构建阶段:按任务清单逐步实现
# archive - 归档阶段:确认完成并归档变更
#
# 示例 - 自定义讨论阶段提示词:
# stages:
# discuss:
# prompt: |
# 进入探索模式。深度思考,自由发散。跟随对话走向。
#
# 示例 - 自定义规划阶段的文档模板:
# stages:
# plan:
# documents:
# - name: design
# prompt: 生成设计文档
# template: |
# # {{change-name}} 设计文档
# - name: task
# prompt: 生成任务清单
# depend: [design]
# template: |
# # {{change-name}} 任务清单
`;
export const SUPPORTED_TOOLS: Record<string, (root: string) => Promise<void>> = {
opencode: injectOpenCode,
"claude-code": injectClaudeCode,
};
export async function runInit(projectRoot: string, tools: string[]): Promise<void> {
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, CONFIG_TEMPLATE, "utf-8");
}
for (const tool of tools) {
await SUPPORTED_TOOLS[tool](projectRoot);
}
}