Add more flags and verbose logging
This commit is contained in:
parent
4e31f18045
commit
542d28cb53
13 changed files with 406 additions and 135 deletions
|
@ -5,32 +5,41 @@ import {
|
|||
ResourceMap,
|
||||
} from "./playbill-schema";
|
||||
|
||||
export class ResourceFileError extends Error { }
|
||||
export class NullResourceError extends Error { }
|
||||
|
||||
export async function loadResourceFile(
|
||||
filePath: string,
|
||||
): Promise<AnyResource[]> {
|
||||
try {
|
||||
const file = Bun.file(filePath);
|
||||
const text = await file.text();
|
||||
const file = Bun.file(filePath);
|
||||
const text = await file.text();
|
||||
|
||||
const parsedDocs = YAML.parseAllDocuments(text);
|
||||
const parsedDocs = YAML.parseAllDocuments(text);
|
||||
|
||||
const collection: AnyResource[] = [];
|
||||
if (parsedDocs.some((doc) => doc.toJS() === null)) {
|
||||
throw new NullResourceError(`Null resource defined in ${filePath}`);
|
||||
}
|
||||
|
||||
for (const doc of parsedDocs) {
|
||||
if (doc.errors.length > 0) {
|
||||
throw new Error(`Error parsing ${filePath}: ${doc.errors}`);
|
||||
}
|
||||
const errors = parsedDocs.flatMap((doc) => doc.errors);
|
||||
|
||||
const raw = doc.toJS();
|
||||
const parsed = AnyResourceSchema.parse(raw);
|
||||
const type = parsed.$define;
|
||||
const schemaToUse = ResourceMap[type];
|
||||
if (errors.length > 0) {
|
||||
throw new ResourceFileError(`Error parsing ${filePath}: ${errors}`);
|
||||
}
|
||||
|
||||
collection.push(schemaToUse.parse(parsed));
|
||||
const collection: AnyResource[] = [];
|
||||
|
||||
for (const doc of parsedDocs) {
|
||||
if (doc.errors.length > 0) {
|
||||
throw new ResourceFileError(`Error parsing ${filePath}: ${doc.errors}`);
|
||||
}
|
||||
|
||||
return collection;
|
||||
} catch (error) {
|
||||
throw new Error(`Error loading ${filePath}: ${error}`);
|
||||
const raw = doc.toJS();
|
||||
const parsed = AnyResourceSchema.parse(raw);
|
||||
const type = parsed.$define;
|
||||
const schemaToUse = ResourceMap[type];
|
||||
|
||||
collection.push(schemaToUse.parse(parsed));
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue