Add extending by binding

This commit is contained in:
Endeavorance 2025-03-07 15:01:52 -05:00
parent beff321251
commit 49e82d0a13
12 changed files with 155 additions and 102 deletions

View file

@ -1,34 +1,15 @@
import { parseArgs } from "node:util";
import {
type AnyResource,
ValidatedPlaybillSchema,
checkDirectives,
getEmptyPlaybill,
} from "@proscenium/playbill";
import { ValidatedPlaybillSchema, checkDirectives } from "@proscenium/playbill";
import chalk from "chalk";
import { parseBindingFile } from "./binding";
import { loadResourceFile } from "./resource";
import { loadFromBinding } from "./binding";
import { usage } from "./usage";
import { ResourceFileError } from "./errors";
import { CLIError, MuseError } from "./errors";
enum ExitCode {
Success = 0,
Error = 1,
}
class CLIError extends Error { }
async function loadFileOrFail(filePath: string): Promise<string> {
const fileToLoad = Bun.file(filePath);
const fileExists = await fileToLoad.exists();
if (!fileExists) {
throw new Error(`File not found: ${filePath}`);
}
return await fileToLoad.text();
}
async function main(): Promise<number> {
// -- ARGUMENT PARSING -- //
const { values: options, positionals: args } = parseArgs({
@ -97,68 +78,15 @@ async function main(): Promise<number> {
verboseLog(`Building Playbill with binding: ${bindingPath}`);
const bindingFileContent = await loadFileOrFail(bindingPath);
const binding = parseBindingFile(bindingFileContent, bindingPath);
const binding = await loadFromBinding(bindingPath);
verboseLog(
`↳ Binding loaded with ${binding.includedFiles.length} associated resources`,
);
// -- RESOURCE FILES -- //
verboseLog("Loading resources");
const loadedResources: AnyResource[] = [];
for (const filepath of binding.includedFiles) {
verboseLog(`↳ Loading ${filepath}`);
const loaded = await loadResourceFile(filepath);
verboseLog(` ↳ Loaded ${loaded.length} resources`);
loadedResources.push(...loaded);
}
verboseLog("Constructing playbill");
// -- COMPILE RESOURCES --//
const playbill = getEmptyPlaybill();
playbill.id = binding.id;
playbill.name = binding.name;
playbill.author = binding.author;
playbill.version = binding.version;
playbill.description = binding.description;
for (const resource of loadedResources) {
switch (resource.$define) {
case "ability":
playbill.abilities.push(resource);
break;
case "species":
playbill.species.push(resource);
break;
case "entity":
playbill.entities.push(resource);
break;
case "item":
playbill.items.push(resource);
break;
case "tag":
playbill.tags.push(resource);
break;
case "method":
playbill.methods.push(resource);
break;
case "lore":
playbill.lore.push(resource);
break;
case "rule":
playbill.rules.push(resource);
break;
case "playbill":
throw new Error("Cannot load playbills rn dawg");
}
}
// -- VALDATE PLAYBILL -- //
verboseLog("Validating playbill");
const validatedPlaybill = ValidatedPlaybillSchema.safeParse(playbill);
const validatedPlaybill = ValidatedPlaybillSchema.safeParse(binding.playbill);
if (!validatedPlaybill.success) {
console.error("Error validating playbill");
@ -202,7 +130,7 @@ try {
const exitCode = await main();
process.exit(exitCode);
} catch (error) {
if (error instanceof ResourceFileError) {
if (error instanceof MuseError) {
console.error(chalk.red(error.fmt()));
} else {
console.error("An unexpected error occurred");