Clean up
This commit is contained in:
parent
45580288e1
commit
8b276cc226
5 changed files with 145 additions and 119 deletions
4
bun.lock
4
bun.lock
|
@ -4,7 +4,7 @@
|
|||
"": {
|
||||
"name": "@proscenium/muse",
|
||||
"dependencies": {
|
||||
"@proscenium/playbill": "0.0.2",
|
||||
"@proscenium/playbill": "0.0.5",
|
||||
"chalk": "^5.4.1",
|
||||
"yaml": "^2.7.0",
|
||||
"zod": "^3.24.1",
|
||||
|
@ -37,7 +37,7 @@
|
|||
|
||||
"@biomejs/cli-win32-x64": ["@biomejs/cli-win32-x64@1.9.4", "", { "os": "win32", "cpu": "x64" }, "sha512-8Y5wMhVIPaWe6jw2H+KlEm4wP/f7EW3810ZLmDlrEEy5KvBsb9ECEfu/kMWD484ijfQ8+nIi0giMgu9g1UAuuA=="],
|
||||
|
||||
"@proscenium/playbill": ["@proscenium/playbill@0.0.2", "https://git.astral.camp/api/packages/proscenium/npm/%40proscenium%2Fplaybill/-/0.0.2/playbill-0.0.2.tgz", { "peerDependencies": { "typescript": "^5" } }, "sha512-zoYCHbm7DGmDRbB+tkQGPQTA5eR0xDHfyv+cp+3JfLy8n/6hOJvx+pG51emD1jBP+CecUE819nFriJqDf+ow0A=="],
|
||||
"@proscenium/playbill": ["@proscenium/playbill@0.0.5", "https://git.astral.camp/api/packages/proscenium/npm/%40proscenium%2Fplaybill/-/0.0.5/playbill-0.0.5.tgz", { "peerDependencies": { "typescript": "^5" } }, "sha512-4TzEe3LpZ2WAep/OWtuNoGiRb2NilfBBN9KQ65/sYSkmBq4Wtdploqo/7+R1Twja/AvOBGgw+Art12beywhqtQ=="],
|
||||
|
||||
"@types/bun": ["@types/bun@1.2.3", "", { "dependencies": { "bun-types": "1.2.3" } }, "sha512-054h79ipETRfjtsCW9qJK8Ipof67Pw9bodFWmkfkaUaRiIQ1dIV2VTlheshlBx3mpKr0KeK8VqnMMCtgN9rQtw=="],
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
"chalk": "^5.4.1",
|
||||
"yaml": "^2.7.0",
|
||||
"zod": "^3.24.1",
|
||||
"@proscenium/playbill": "0.0.2"
|
||||
"@proscenium/playbill": "0.0.5"
|
||||
},
|
||||
"scripts": {
|
||||
"fmt": "bunx --bun biome check --fix",
|
||||
|
|
|
@ -3,31 +3,36 @@ import { Glob } from "bun";
|
|||
import YAML from "yaml";
|
||||
import z from "zod";
|
||||
|
||||
const BindingSchema = z.object({
|
||||
const BindingFileSchema = z.object({
|
||||
$binding: z.literal("playbill"),
|
||||
id: z.string(),
|
||||
name: z.string().default("Unnamed playbill"),
|
||||
author: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
version: z.string(),
|
||||
files: z.array(z.string()).default(["**/*.yaml", "**/*.md"]),
|
||||
});
|
||||
|
||||
type Binding = z.infer<typeof BindingSchema>;
|
||||
type BindingFileContent = z.infer<typeof BindingFileSchema>;
|
||||
|
||||
export interface PlaybillBinding {
|
||||
info: Binding;
|
||||
bindingPath: string;
|
||||
files: string[];
|
||||
_raw: BindingFileContent;
|
||||
bindingFilePath: string;
|
||||
includedFiles: string[];
|
||||
|
||||
id: string;
|
||||
name: string;
|
||||
author: string;
|
||||
version: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export async function loadBindingFile(
|
||||
export function parseBindingFile(
|
||||
text: string,
|
||||
filePath: string,
|
||||
): Promise<PlaybillBinding> {
|
||||
const file = Bun.file(filePath);
|
||||
const text = await file.text();
|
||||
): PlaybillBinding {
|
||||
const parsed = YAML.parse(text);
|
||||
const binding = BindingSchema.parse(parsed);
|
||||
|
||||
const binding = BindingFileSchema.parse(parsed);
|
||||
const fileGlobs = binding.files;
|
||||
const bindingFileDirname = filePath.split("/").slice(0, -1).join("/");
|
||||
|
||||
|
@ -52,8 +57,14 @@ export async function loadBindingFile(
|
|||
});
|
||||
|
||||
return {
|
||||
info: binding,
|
||||
bindingPath: filePath,
|
||||
files: filteredFilePaths,
|
||||
_raw: binding,
|
||||
bindingFilePath: filePath,
|
||||
includedFiles: filteredFilePaths,
|
||||
|
||||
id: binding.id,
|
||||
name: binding.name,
|
||||
author: binding.author ?? "Anonymous",
|
||||
description: binding.description ?? "",
|
||||
version: binding.version,
|
||||
};
|
||||
}
|
||||
|
|
106
src/index.ts
106
src/index.ts
|
@ -1,20 +1,34 @@
|
|||
import { parseArgs } from "node:util";
|
||||
import {
|
||||
type AnyResource,
|
||||
DirectiveError,
|
||||
ValidatedPlaybillSchema,
|
||||
compileScript,
|
||||
getEmptyPlaybill,
|
||||
} from "@proscenium/playbill";
|
||||
import chalk from "chalk";
|
||||
import { loadBindingFile } from "./binding";
|
||||
import { parseBindingFile } from "./binding";
|
||||
import { loadResourceFile } from "./resource";
|
||||
import { usage } from "./usage";
|
||||
|
||||
class CLIError extends Error {}
|
||||
enum ExitCode {
|
||||
Success = 0,
|
||||
Error = 1,
|
||||
}
|
||||
|
||||
async function main(): Promise<boolean> {
|
||||
// Parse command line arguments
|
||||
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({
|
||||
args: Bun.argv.slice(2),
|
||||
options: {
|
||||
|
@ -53,11 +67,13 @@ async function main(): Promise<boolean> {
|
|||
allowPositionals: true,
|
||||
});
|
||||
|
||||
// -- HELP TEXT -- //
|
||||
if (options.help) {
|
||||
console.log(usage);
|
||||
return true;
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
// -- ARG VALIDATION -- //
|
||||
if (options.check && options.write) {
|
||||
throw new CLIError("Cannot use --check and --write together");
|
||||
}
|
||||
|
@ -74,30 +90,22 @@ async function main(): Promise<boolean> {
|
|||
}
|
||||
}
|
||||
|
||||
// -- BINDING FILE -- //
|
||||
const bindingPath = args[0] ?? "./binding.yaml";
|
||||
|
||||
verboseLog(`Building Playbill with binding: ${bindingPath}`);
|
||||
|
||||
// Check if the binding file exists
|
||||
const bindingFileCheck = Bun.file(bindingPath);
|
||||
const bindingFileExists = await bindingFileCheck.exists();
|
||||
|
||||
if (!bindingFileExists) {
|
||||
throw new Error(`Binding file not found: ${bindingPath}`);
|
||||
}
|
||||
|
||||
const binding = await loadBindingFile(bindingPath);
|
||||
const bindingFileContent = await loadFileOrFail(bindingPath);
|
||||
const binding = parseBindingFile(bindingFileContent, bindingPath);
|
||||
|
||||
verboseLog(
|
||||
`↳ Binding loaded with ${binding.files.length} associated resources`,
|
||||
`↳ Binding loaded with ${binding.includedFiles.length} associated resources`,
|
||||
);
|
||||
|
||||
const loadedResources: AnyResource[] = [];
|
||||
|
||||
// -- RESOURCE FILES -- //
|
||||
verboseLog("Loading resources");
|
||||
|
||||
// Load resources listed in the binding file
|
||||
for (const filepath of binding.files) {
|
||||
const loadedResources: AnyResource[] = [];
|
||||
for (const filepath of binding.includedFiles) {
|
||||
verboseLog(`↳ Loading ${filepath}`);
|
||||
const loaded = await loadResourceFile(filepath);
|
||||
verboseLog(` ↳ Loaded ${loaded.length} resources`);
|
||||
|
@ -106,13 +114,14 @@ async function main(): Promise<boolean> {
|
|||
|
||||
verboseLog("Constructing playbill");
|
||||
|
||||
// Consjtruct the playbill object
|
||||
// -- COMPILE RESOURCES --//
|
||||
const playbill = getEmptyPlaybill();
|
||||
|
||||
playbill.id = binding.info.id;
|
||||
playbill.name = binding.info.name;
|
||||
playbill.author = binding.info.author ?? "Anonymous";
|
||||
playbill.version = binding.info.version;
|
||||
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) {
|
||||
|
@ -145,66 +154,49 @@ async function main(): Promise<boolean> {
|
|||
}
|
||||
}
|
||||
|
||||
// Evaluate directives in descriptions for all resources
|
||||
verboseLog("Processing descriptions");
|
||||
for (const resource of loadedResources) {
|
||||
try {
|
||||
resource.description = compileScript(resource.description, playbill);
|
||||
} catch (error) {
|
||||
if (error instanceof DirectiveError) {
|
||||
console.error(
|
||||
`Error processing directives in ${resource.$define}/${resource.id}`,
|
||||
);
|
||||
console.error(error.message);
|
||||
} else {
|
||||
console.error(error);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the playbill object
|
||||
// -- VALDATE PLAYBILL -- //
|
||||
verboseLog("Validating playbill");
|
||||
const validatedPlaybill = ValidatedPlaybillSchema.safeParse(playbill);
|
||||
|
||||
if (!validatedPlaybill.success) {
|
||||
console.error("Error validating playbill");
|
||||
console.error(validatedPlaybill.error.errors);
|
||||
return false;
|
||||
return ExitCode.Error;
|
||||
}
|
||||
|
||||
verboseLog("Playbill validated");
|
||||
|
||||
// If --check is set, exit here
|
||||
// -- EXIT EARLY IF JUST CHECKING VALIDATION --//
|
||||
if (options.check) {
|
||||
console.log(chalk.green("Playbill validated successfully"));
|
||||
return true;
|
||||
return ExitCode.Error;
|
||||
}
|
||||
|
||||
// -- SERIALIZE TO JSON --//
|
||||
const finalizedPlaybill = JSON.stringify(
|
||||
validatedPlaybill.data,
|
||||
null,
|
||||
options.minify ? undefined : 2,
|
||||
);
|
||||
|
||||
// -- WRITE TO DISK OR STDOUT --//
|
||||
if (options.write) {
|
||||
await Bun.write(options.outfile, finalizedPlaybill);
|
||||
return true;
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
console.log(finalizedPlaybill);
|
||||
return true;
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
try {
|
||||
const success = await main();
|
||||
if (success) {
|
||||
process.exit(0);
|
||||
} else {
|
||||
process.exit(1);
|
||||
}
|
||||
const exitCode = await main();
|
||||
process.exit(exitCode);
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
console.error("Uncaught error:");
|
||||
console.error(chalk.red(error.message));
|
||||
console.error(error.stack);
|
||||
} else {
|
||||
console.error("An unknown error occurred");
|
||||
console.error(error);
|
||||
|
|
|
@ -3,30 +3,18 @@ import {
|
|||
AnyResourceSchema,
|
||||
ResourceMap,
|
||||
} from "@proscenium/playbill";
|
||||
import YAML from "yaml";
|
||||
import YAML, { YAMLParseError } from "yaml";
|
||||
import { extractFrontmatter, extractMarkdown } from "./markdown";
|
||||
|
||||
type FileFormat = "yaml" | "markdown";
|
||||
export class ResourceFileError extends Error {}
|
||||
export class NullResourceError extends Error {}
|
||||
export class ResourceFileError extends Error { }
|
||||
export class NullResourceError extends Error { }
|
||||
|
||||
/**
|
||||
* Load and process all documents in a yaml file at the given path
|
||||
* @param filePath - The path to the yaml file
|
||||
* @returns An array of validated resources
|
||||
*
|
||||
* @throws ResourceFileError
|
||||
* @throws NullResourceError
|
||||
*/
|
||||
export async function loadResourceFile(
|
||||
filePath: string,
|
||||
): Promise<AnyResource[]> {
|
||||
const file = Bun.file(filePath);
|
||||
const format: FileFormat =
|
||||
filePath.split(".").pop() === "md" ? "markdown" : "yaml";
|
||||
const text = await file.text();
|
||||
function determineFileFormat(filePath: string): FileFormat {
|
||||
return filePath.split(".").pop() === "md" ? "markdown" : "yaml";
|
||||
}
|
||||
|
||||
if (format === "yaml") {
|
||||
function loadYamlResourceFile(filePath: string, text: string): AnyResource[] {
|
||||
const parsedDocs = YAML.parseAllDocuments(text);
|
||||
|
||||
if (parsedDocs.some((doc) => doc.toJS() === null)) {
|
||||
|
@ -55,8 +43,13 @@ export async function loadResourceFile(
|
|||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
|
||||
function loadMarkdownResourceFile(
|
||||
filePath: string,
|
||||
text: string,
|
||||
): AnyResource[] {
|
||||
try {
|
||||
const frontmatter = extractFrontmatter(text);
|
||||
const markdown = extractMarkdown(text);
|
||||
|
||||
|
@ -67,4 +60,34 @@ export async function loadResourceFile(
|
|||
|
||||
const parsed = AnyResourceSchema.parse(together);
|
||||
return [parsed];
|
||||
} catch (e) {
|
||||
if (e instanceof YAMLParseError) {
|
||||
throw new ResourceFileError(`Error parsing frontmatter in ${filePath}`);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and process all documents in a yaml file at the given path
|
||||
* @param filePath - The path to the yaml file
|
||||
* @returns An array of validated resources
|
||||
*
|
||||
* @throws ResourceFileError
|
||||
* @throws NullResourceError
|
||||
*/
|
||||
export async function loadResourceFile(
|
||||
filePath: string,
|
||||
): Promise<AnyResource[]> {
|
||||
const file = Bun.file(filePath);
|
||||
const text = await file.text();
|
||||
const format = determineFileFormat(filePath);
|
||||
|
||||
switch (format) {
|
||||
case "yaml":
|
||||
return loadYamlResourceFile(filePath, text);
|
||||
case "markdown":
|
||||
return loadMarkdownResourceFile(filePath, text);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue