feat: CLI 入口和 init 命令
This commit is contained in:
39
src/commands/init.ts
Normal file
39
src/commands/init.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { stringify as stringifyYaml } from "yaml";
|
||||
import { defaultConfig } from "../defaults/config.ts";
|
||||
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";
|
||||
|
||||
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 Error(`不支持的工具: ${tool}`);
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
const yaml = stringifyYaml(defaultConfig);
|
||||
await writeFile(configPath, yaml, "utf-8");
|
||||
}
|
||||
|
||||
for (const tool of tools) {
|
||||
await SUPPORTED_TOOLS[tool](projectRoot);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user