Initial commit

This commit is contained in:
Endeavorance 2025-02-12 10:31:22 -05:00
commit 4e31f18045
15 changed files with 779 additions and 0 deletions

36
src/resource.ts Normal file
View file

@ -0,0 +1,36 @@
import YAML from "yaml";
import {
type AnyResource,
AnyResourceSchema,
ResourceMap,
} from "./playbill-schema";
export async function loadResourceFile(
filePath: string,
): Promise<AnyResource[]> {
try {
const file = Bun.file(filePath);
const text = await file.text();
const parsedDocs = YAML.parseAllDocuments(text);
const collection: AnyResource[] = [];
for (const doc of parsedDocs) {
if (doc.errors.length > 0) {
throw new Error(`Error parsing ${filePath}: ${doc.errors}`);
}
const raw = doc.toJS();
const parsed = AnyResourceSchema.parse(raw);
const type = parsed.$define;
const schemaToUse = ResourceMap[type];
collection.push(schemaToUse.parse(parsed));
}
return collection;
} catch (error) {
throw new Error(`Error loading ${filePath}: ${error}`);
}
}