-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-index.js
More file actions
43 lines (35 loc) · 1.52 KB
/
Copy pathbuild-index.js
File metadata and controls
43 lines (35 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
import { dirname, extname, relative, resolve, sep } from "node:path";
import { buildIntentionsIndex } from "./js/indexer-shared.js";
const root = resolve(import.meta.dirname, "..");
const sourceRoot = resolve(root, process.argv[2] ?? "IntentionsDatabase/data/content");
const outputPath = resolve(root, process.argv[3] ?? "IntentionsDatabase/data/index.json");
function walkFiles(dir) {
const entries = readdirSync(dir, { withFileTypes: true });
const files = [];
for (const entry of entries) {
const fullPath = resolve(dir, entry.name);
if (entry.isDirectory()) {
files.push(...walkFiles(fullPath));
continue;
}
const ext = extname(entry.name).toLowerCase();
if (ext === ".yml" || ext === ".yaml" || ext === ".ftl") {
files.push(fullPath);
}
}
return files;
}
const sourceFiles = walkFiles(sourceRoot)
.sort((left, right) => left.localeCompare(right, "en"))
.map(fullPath => ({
path: relative(sourceRoot, fullPath).split(sep).join("/"),
content: readFileSync(fullPath, "utf8")
}));
const index = buildIntentionsIndex(sourceFiles, {
sourceRoot: relative(root, sourceRoot).split(sep).join("/"),
generatedAt: new Date().toISOString()
});
mkdirSync(dirname(outputPath), { recursive: true });
writeFileSync(outputPath, `${JSON.stringify(index, null, 2)}\n`, "utf8");
console.log(`Indexed ${index.scenarios.length} scenarios, ${index.intentions.length} intentions, ${index.ftlEntries.length} FTL entries.`);