-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcompile-database.ts
More file actions
106 lines (87 loc) · 2.9 KB
/
compile-database.ts
File metadata and controls
106 lines (87 loc) · 2.9 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import fs from "node:fs";
import { readdir } from "node:fs/promises";
import path from "node:path";
import { YAML } from "bun";
import {
type Database,
DatabaseSchema,
fileNameToId,
type Meta,
MetaSchema,
type Resource,
ResourceSchema,
} from ".";
type DatabaseMetadata = {
id: string;
} & Meta;
async function buildDatabase() {
const metaDir = path.join(__dirname, "./metadata");
const resourcesDir = path.join(__dirname, "./resources");
const allMetaFiles = (await readdir(metaDir, { recursive: true })).toSorted();
const allResourceFiles = (
await readdir(resourcesDir, { recursive: true })
).toSorted();
const database = {
metadata: [] as DatabaseMetadata[],
resources: [] as Resource[],
} satisfies Database;
let hasErrors = false;
for (const file of allMetaFiles.filter((f) => f.endsWith(".yaml"))) {
const filePath = path.join(metaDir, file);
const fileContent = await fs.promises.readFile(filePath, "utf-8");
const entityId = fileNameToId(file);
const data = YAML.parse(fileContent);
const result = MetaSchema.safeParse(data);
if (!result.success) {
console.error(`Validation Error in Metadata: ${file}`);
console.error(result.error.issues);
hasErrors = true;
} else {
database.metadata.push({ id: entityId, ...result.data });
}
}
const validTags = new Set(database.metadata.map((meta) => meta.id));
for (const file of allResourceFiles.filter((f) => f.endsWith(".yaml"))) {
const filePath = path.join(resourcesDir, file);
const fileContent = await fs.promises.readFile(filePath, "utf-8");
const data = YAML.parse(fileContent);
const result = ResourceSchema.safeParse(data);
if (!result.success) {
console.error(`Validation Error in Resource: ${file}`);
console.error(result.error.issues);
hasErrors = true;
continue;
}
const invalidTags = result.data.teaches.filter(
(tag) => !validTags.has(tag),
);
if (invalidTags.length > 0) {
console.error(`Relational Error in Resource: ${file}`);
console.error(`Unknown tags in 'teaches': [${invalidTags.join(", ")}]`);
console.error(`Ensure a corresponding metadata file exists.`);
hasErrors = true;
continue;
}
database.resources.push(result.data);
}
if (hasErrors) {
console.error("Build failed due to validation errors.");
process.exit(1);
}
// verify the entire database against the schema before writing
const finalResult = DatabaseSchema.safeParse(database);
if (!finalResult.success) {
console.error("Final Database Validation Error:");
console.error(finalResult.error.issues);
process.exit(1);
}
const outputPath = path.join(__dirname, "./database.json");
await fs.promises.writeFile(outputPath, JSON.stringify(database, null, 2));
console.log(
`Success! Compiled ${database.metadata.length} entities and ${database.resources.length} resources.`,
);
}
buildDatabase().catch((err) => {
console.error("Unexpected error during build:", err);
process.exit(1);
});