creatrip-agent-rules-builder
Version:
Unified converter for AI coding agent rules across Cursor, Windsurf, and Claude
50 lines (41 loc) • 1.29 kB
text/typescript
import * as fs from "fs";
import * as path from "path";
import { parseTree, getNodeValue } from "jsonc-parser";
import { ParsedContent, AgentConfig } from "./types";
export function parseAgentRulesFile(filePath: string): ParsedContent {
if (!fs.existsSync(filePath)) {
throw new Error(`파일을 찾을 수 없습니다: ${filePath}`);
}
const content = fs.readFileSync(filePath, "utf8");
// JSONC 설정 블록 찾기
const configBlockRegex =
/```(?:jsonc|json)\s+agent-rules-config\s*\n([\s\S]*?)\n```/;
const match = content.match(configBlockRegex);
let config: AgentConfig = {};
let rules = content;
if (match) {
try {
// JSONC 파싱
const configText = match[1];
const tree = parseTree(configText);
if (tree) {
config = getNodeValue(tree) as AgentConfig;
}
// 설정 블록을 제거한 나머지를 규칙으로 사용
rules = content.replace(configBlockRegex, "").trim();
} catch (error) {
throw new Error(
`JSONC 설정 파싱 오류: ${
error instanceof Error ? error.message : String(error)
}`,
);
}
}
return {
rules,
config,
};
}
export function getDefaultRulesFilePath(): string {
return path.resolve(process.cwd(), "AGENTS.md");
}