Initial commit
This commit is contained in:
commit
4e31f18045
15 changed files with 779 additions and 0 deletions
36
src/resource.ts
Normal file
36
src/resource.ts
Normal 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}`);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue