feat: init 命令检测包管理器并写入 metadata.command

This commit is contained in:
2026-06-09 20:18:04 +08:00
parent a5c8263412
commit 589eaa120e
2 changed files with 39 additions and 5 deletions

View File

@@ -1,10 +1,12 @@
import { existsSync } from "node:fs";
import { mkdir, writeFile } from "node:fs/promises";
import { mkdir, writeFile, readFile, appendFile } 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";
import { detectCommandPrefix, getPmPrefix } from "../core/pm.ts";
import { parse as parseYaml } from "yaml";
const CONFIG_TEMPLATE = `# Rune 配置文件
#
@@ -38,11 +40,29 @@ const CONFIG_TEMPLATE = `# Rune 配置文件
# # {{change-name}} 任务清单
`;
export const SUPPORTED_TOOLS: Record<string, (root: string) => Promise<void>> = {
export const SUPPORTED_TOOLS: Record<string, (root: string, command?: string) => Promise<void>> = {
opencode: injectOpenCode,
"claude-code": injectClaudeCode,
};
async function ensureMetadataCommand(configPath: string, command: string): Promise<void> {
const content = await readFile(configPath, "utf-8");
const parsed = parseYaml(content) as { metadata?: { command?: string } } | null;
if (parsed?.metadata?.command) return;
if (parsed?.metadata) {
const lines = content.split("\n");
const metaIdx = lines.findIndex((l) => l.trim() === "metadata:");
if (metaIdx >= 0) {
lines.splice(metaIdx + 1, 0, ` command: "${command}"`);
await writeFile(configPath, lines.join("\n"), "utf-8");
return;
}
}
await appendFile(configPath, `\nmetadata:\n command: "${command}"\n`);
}
export async function runInit(projectRoot: string, tools: string[]): Promise<void> {
for (const tool of tools) {
if (!SUPPORTED_TOOLS[tool]) {
@@ -62,7 +82,13 @@ export async function runInit(projectRoot: string, tools: string[]): Promise<voi
await writeFile(configPath, CONFIG_TEMPLATE, "utf-8");
}
const command = await detectCommandPrefix();
if (command) {
await ensureMetadataCommand(configPath, command);
}
const prefix = command ?? getPmPrefix();
for (const tool of tools) {
await SUPPORTED_TOOLS[tool](projectRoot);
await SUPPORTED_TOOLS[tool](projectRoot, prefix === "rune" ? undefined : prefix);
}
}