refactor(cli): Share the content-index walk

Extract readContentIndex from buildRegistry so the validator can read raw files while the server keeps the scanned registry, both from one walk.
This commit is contained in:
2026-09-12 16:38:05 +08:00
parent f33761e24f
commit d1384c2ccc
2 changed files with 54 additions and 33 deletions
+2 -33
View File
@@ -13,6 +13,7 @@ import {
deriveCompletions,
type ContentRegistry,
} from "../content-registry.js";
import { readContentIndex } from "../content-index.js";
import type { CompletionsPayload } from "../completions/types.js";
interface ContentIndex {
@@ -86,39 +87,7 @@ function getBestIP(): string {
* 扫描目录内的 .md 等文件,构建内容注册表(路径索引 + 每文档内联内容)
*/
export function buildRegistry(dir: string): ContentRegistry {
const index: Record<string, string> = {};
function scan(currentPath: string, relativePath: string) {
const entries = readdirSync(currentPath);
for (const entry of entries) {
if (entry.startsWith(".") || entry === "node_modules") continue;
const fullPath = join(currentPath, entry);
const relPath = relativePath ? join(relativePath, entry) : entry;
const normalizedRelPath = normalizePathKey(relPath);
const stats = statSync(fullPath);
if (stats.isDirectory()) {
scan(fullPath, relPath);
} else if (
entry.endsWith(".md") ||
entry.endsWith(".csv") ||
entry.endsWith(".yarn") ||
entry.endsWith(".svg")
) {
try {
index[normalizedRelPath] = readFileSync(fullPath, "utf-8");
} catch (e) {
console.error(`读取文件失败:${fullPath}`, e);
}
}
}
}
scan(dir, "");
return buildRegistryFromIndex(index);
return buildRegistryFromIndex(readContentIndex(dir));
}
/**