Support for updated shapes
This commit is contained in:
parent
9d4907aa25
commit
d8ddff01a6
6 changed files with 58 additions and 65 deletions
|
@ -3,11 +3,12 @@ import { Glob } from "bun";
|
|||
import z from "zod";
|
||||
import {
|
||||
getEmptyPlaybill,
|
||||
type AnyResource,
|
||||
type UnknownResource,
|
||||
type Playbill,
|
||||
} from "@proscenium/playbill";
|
||||
import { loadYAMLFileOrFail } from "./files";
|
||||
import { loadResourceFile } from "./resource";
|
||||
import { FileNotFoundError } from "./errors";
|
||||
|
||||
const BindingFileSchema = z.object({
|
||||
$binding: z.literal("playbill"),
|
||||
|
@ -39,21 +40,42 @@ export interface PlaybillBinding {
|
|||
playbill: Playbill;
|
||||
}
|
||||
|
||||
export async function resolveBindingPath(bindingPath: string): Promise<string> {
|
||||
// If the path does not specify a filename, use binding.yamk
|
||||
const inputLocation = Bun.file(bindingPath);
|
||||
|
||||
// If it is a directory, try again seeking a binding.yaml inside
|
||||
const stat = await inputLocation.stat();
|
||||
if (stat.isDirectory()) {
|
||||
return resolveBindingPath(path.resolve(bindingPath, "binding.yaml"));
|
||||
}
|
||||
|
||||
// If it doesnt exist, bail
|
||||
if (!(await inputLocation.exists())) {
|
||||
throw new FileNotFoundError(bindingPath);
|
||||
}
|
||||
|
||||
// Resolve the path
|
||||
return path.resolve(bindingPath);
|
||||
}
|
||||
|
||||
export async function loadFromBinding(
|
||||
filePath: string,
|
||||
bindingPath: string,
|
||||
): Promise<PlaybillBinding> {
|
||||
const yamlContent = await loadYAMLFileOrFail(filePath);
|
||||
const resolvedBindingPath = await resolveBindingPath(bindingPath);
|
||||
const yamlContent = await loadYAMLFileOrFail(resolvedBindingPath);
|
||||
const binding = BindingFileSchema.parse(yamlContent);
|
||||
const fileGlobs = binding.files;
|
||||
const bindingFileDirname = filePath.split("/").slice(0, -1).join("/");
|
||||
const bindingFileDirname = bindingPath.split("/").slice(0, -1).join("/");
|
||||
|
||||
let basePlaybill: Playbill | null = null;
|
||||
|
||||
// If this is extending another binding, load that first
|
||||
if (binding.extends) {
|
||||
// TODO: Allow extending built playbills in addition to bindings
|
||||
const extendBindingPath = path.resolve(bindingFileDirname, binding.extends);
|
||||
basePlaybill = (await loadFromBinding(extendBindingPath)).playbill;
|
||||
const pathFromHere = path.resolve(bindingFileDirname, binding.extends);
|
||||
const extendBindingPath = await resolveBindingPath(pathFromHere);
|
||||
const loadedBinding = await loadFromBinding(extendBindingPath);
|
||||
basePlaybill = loadedBinding.playbill;
|
||||
}
|
||||
|
||||
const allFilePaths: string[] = [];
|
||||
|
@ -71,13 +93,13 @@ export async function loadFromBinding(
|
|||
allFilePaths.push(...results);
|
||||
}
|
||||
|
||||
const bindingFileAbsolutePath = path.resolve(filePath);
|
||||
const bindingFileAbsolutePath = path.resolve(bindingPath);
|
||||
const filePathsWithoutBindingFile = allFilePaths.filter((filePath) => {
|
||||
return filePath !== bindingFileAbsolutePath;
|
||||
});
|
||||
|
||||
// -- LOAD ASSOCIATED RESOURCE FILES -- //
|
||||
const loadedResources: AnyResource[] = [];
|
||||
const loadedResources: UnknownResource[] = [];
|
||||
for (const filepath of filePathsWithoutBindingFile) {
|
||||
const loaded = await loadResourceFile(filepath);
|
||||
loadedResources.push(...loaded);
|
||||
|
@ -93,43 +115,17 @@ export async function loadFromBinding(
|
|||
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 "guide":
|
||||
playbill.guides.push(resource);
|
||||
break;
|
||||
case "glossary":
|
||||
playbill.glossaries.push(resource);
|
||||
break;
|
||||
}
|
||||
const resourceType = resource.$define;
|
||||
const resourceMap = playbill[resourceType] as Record<
|
||||
string,
|
||||
typeof resource
|
||||
>;
|
||||
resourceMap[resource.id] = resource;
|
||||
}
|
||||
|
||||
return {
|
||||
_raw: binding,
|
||||
bindingFilePath: filePath,
|
||||
bindingFilePath: bindingPath,
|
||||
includedFiles: filePathsWithoutBindingFile,
|
||||
|
||||
id: binding.id,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue