Continued work
This commit is contained in:
parent
10c84d2dce
commit
3e2ab6ec73
3 changed files with 38 additions and 57 deletions
|
@ -1,9 +1,8 @@
|
|||
import path from "node:path";
|
||||
import {
|
||||
IDSchema,
|
||||
PlaybillSchema,
|
||||
type Playbill,
|
||||
type UnknownResource,
|
||||
UnvalidatedPlaybillSchema,
|
||||
type UnknownComponent,
|
||||
type UnvalidatedPlaybill,
|
||||
} from "@proscenium/playbill";
|
||||
import { Glob } from "bun";
|
||||
import z from "zod";
|
||||
|
@ -11,29 +10,17 @@ import { FileNotFoundError } from "#lib/errors";
|
|||
import { loadYAMLFileOrFail } from "#util/files";
|
||||
import { loadResourceFile } from "./resource";
|
||||
|
||||
const HTMLRenderOptionsSchema = z.object({
|
||||
styles: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
const PageDefinitionSchema = z.object({
|
||||
id: z.string(),
|
||||
title: z.string(),
|
||||
resources: z.array(IDSchema),
|
||||
});
|
||||
|
||||
const BindingFileSchema = z.object({
|
||||
$binding: z.literal("playbill"),
|
||||
extends: z.string().optional(),
|
||||
extend: z.string().optional(),
|
||||
|
||||
id: z.string(),
|
||||
name: z.string().default("Unnamed playbill"),
|
||||
author: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
version: z.string(),
|
||||
version: z.string().default("0.0.0"),
|
||||
|
||||
files: z.array(z.string()).default(["**/*.yaml", "**/*.md"]),
|
||||
|
||||
html: HTMLRenderOptionsSchema.optional(),
|
||||
|
||||
pages: PageDefinitionSchema.array().optional(),
|
||||
styles: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
type BindingFileContent = z.infer<typeof BindingFileSchema>;
|
||||
|
@ -53,14 +40,13 @@ export interface PlaybillBinding {
|
|||
version: string;
|
||||
description: string;
|
||||
|
||||
html?: z.infer<typeof HTMLRenderOptionsSchema>;
|
||||
pages?: z.infer<typeof PageDefinitionSchema>[];
|
||||
styles?: string[];
|
||||
|
||||
playbill: Playbill;
|
||||
playbill: UnvalidatedPlaybill;
|
||||
}
|
||||
|
||||
export async function resolveBindingPath(bindingPath: string): Promise<string> {
|
||||
// If the path does not specify a filename, use binding.yamk
|
||||
// If the path does not specify a filename, use binding.yaml
|
||||
const inputLocation = Bun.file(bindingPath);
|
||||
|
||||
// If it is a directory, try again seeking a binding.yaml inside
|
||||
|
@ -85,16 +71,22 @@ export async function loadFromBinding(
|
|||
const yamlContent = await loadYAMLFileOrFail(resolvedBindingPath);
|
||||
const binding = BindingFileSchema.parse(yamlContent);
|
||||
const fileGlobs = binding.files;
|
||||
const bindingFileDirname = bindingPath.split("/").slice(0, -1).join("/");
|
||||
const bindingFileDirname = path.dirname(resolvedBindingPath);
|
||||
|
||||
let basePlaybill: Playbill | null = null;
|
||||
let playbill: UnvalidatedPlaybill = UnvalidatedPlaybillSchema.parse({
|
||||
$define: "playbill",
|
||||
id: "blank",
|
||||
name: "Unnamed playbill",
|
||||
description: "Unnamed Playbill",
|
||||
version: "0.0.1",
|
||||
});
|
||||
|
||||
// If this is extending another binding, load that first
|
||||
if (binding.extends) {
|
||||
const pathFromHere = path.resolve(bindingFileDirname, binding.extends);
|
||||
if (binding.extend) {
|
||||
const pathFromHere = path.resolve(bindingFileDirname, binding.extend);
|
||||
const extendBindingPath = await resolveBindingPath(pathFromHere);
|
||||
const loadedBinding = await loadFromBinding(extendBindingPath);
|
||||
basePlaybill = loadedBinding.playbill;
|
||||
playbill = loadedBinding.playbill;
|
||||
}
|
||||
|
||||
const allFilePaths: string[] = [];
|
||||
|
@ -118,23 +110,12 @@ export async function loadFromBinding(
|
|||
});
|
||||
|
||||
// -- LOAD ASSOCIATED RESOURCE FILES -- //
|
||||
const loadedResources: UnknownResource[] = [];
|
||||
const loadedResources: UnknownComponent[] = [];
|
||||
for (const filepath of filePathsWithoutBindingFile) {
|
||||
const loaded = await loadResourceFile(filepath);
|
||||
loadedResources.push(...loaded);
|
||||
}
|
||||
|
||||
// -- COMPILE PLAYBILL FROM RESOURCES --//
|
||||
const playbill =
|
||||
basePlaybill ??
|
||||
PlaybillSchema.parse({
|
||||
$define: "playbill",
|
||||
id: "blank",
|
||||
name: "Unnamed playbill",
|
||||
description: "Unnamed Playbill",
|
||||
version: "0.0.1",
|
||||
});
|
||||
|
||||
playbill.id = binding.id;
|
||||
playbill.name = binding.name;
|
||||
playbill.author = binding.author ?? "Anonymous";
|
||||
|
@ -162,7 +143,7 @@ export async function loadFromBinding(
|
|||
description: binding.description ?? "",
|
||||
version: binding.version,
|
||||
|
||||
html: binding.html,
|
||||
styles: binding.styles,
|
||||
|
||||
playbill,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue