feat: 状态扫描器及测试
This commit is contained in:
55
src/core/scanner.ts
Normal file
55
src/core/scanner.ts
Normal 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 [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user