feat: 状态扫描器及测试

This commit is contained in:
2026-06-08 17:20:11 +08:00
parent a6ab990bf6
commit 44e41e496b
2 changed files with 134 additions and 0 deletions

55
src/core/scanner.ts Normal file
View File

@@ -0,0 +1,55 @@
import { readdir, stat, readFile } from "node:fs/promises";
import { join } from "node:path";
import type { ChangeStatus } from "../types.ts";
import { getChangesDir, getArchiveDir } from "./config.ts";
import { parseTasks } from "./task-parser.ts";
export async function scanChanges(
projectRoot: string,
): Promise<ChangeStatus[]> {
const changesDir = getChangesDir(projectRoot);
const results: ChangeStatus[] = [];
try {
const entries = await readdir(changesDir);
for (const entry of entries) {
const entryPath = join(changesDir, entry);
const entryStat = await stat(entryPath);
if (!entryStat.isDirectory()) continue;
const docs = await readdir(entryPath);
const documents = docs.filter((d) => d.endsWith(".md"));
let taskProgress: { completed: number; total: number } | null = null;
const taskFile = docs.find((d) => d === "task.md");
if (taskFile) {
const content = await readFile(join(entryPath, taskFile), "utf-8");
const tasks = parseTasks(content);
taskProgress = {
completed: tasks.filter((t) => t.checked).length,
total: tasks.length,
};
}
results.push({ name: entry, documents, taskProgress });
}
} catch {
}
return results;
}
export async function scanArchives(projectRoot: string): Promise<string[]> {
const archiveDir = getArchiveDir(projectRoot);
try {
const entries = await readdir(archiveDir);
const dirs: string[] = [];
for (const entry of entries) {
const entryStat = await stat(join(archiveDir, entry));
if (entryStat.isDirectory()) dirs.push(entry);
}
return dirs;
} catch {
return [];
}
}