import { readFileSync, existsSync, readdirSync, statSync } from "node:fs"; import { resolve, join } from "node:path"; import type { Rule } from "../../core/types.js"; import { parsers } from "../../parsers/index.js"; import { findAgentFiles } from "../../core/files.js"; export async function scanProject(root: string): Promise { const absRoot = resolve(root); console.log(`Scanning ${absRoot} for AI agent configs...\n`); let foundAny = false; const seenFiles = new Set(); for (const [agentName, parser] of Object.entries(parsers)) { // AGENTS.md is shared by several agents — attribute it once (first match wins). const files = findAgentFiles(absRoot, agentName).filter((f) => !seenFiles.has(f)); if (files.length === 0) continue; files.forEach((f) => seenFiles.add(f)); foundAny = true; const allRules: Rule[] = []; for (const file of files) { const content = readFileSync(file, "utf-8"); allRules.push(...parser.parse(content, file)); } const categoryCounts = new Map(); for (const r of allRules) { categoryCounts.set(r.category, (categoryCounts.get(r.category) ?? 0) + 1); } console.log(` ✅ ${agentName.toUpperCase()} — ${files.length} file(s), ${allRules.length} rules`); for (const file of files) { console.log(` 📄 ${file.replace(absRoot, ".")}`); } const cats = [...categoryCounts.entries()].map(([c, n]) => `${c}(${n})`).join(", "); console.log(` Categories: ${cats}\n`); } if (!foundAny) { console.log(" 这个目录里没有找到任何 Agent 规则文件。"); console.log(" 请换一个项目目录(通常包含 CLAUDE.md / AGENTS.md / .cursor/rules)。"); console.log(" Supported:"); for (const [name, parser] of Object.entries(parsers)) { console.log(` ${name}: ${parser.filePatterns.join(", ")}`); } console.log("\n Tip: run `agentlens install` to set up AgentLens for your agents."); } else { console.log(" 💡 Run `agentlens move --from --to ` to migrate rules."); console.log(" 💡 Run `agentlens drift` to check for config drift."); } }