Generic muse

This commit is contained in:
Endeavorance 2025-04-01 15:43:48 -04:00
parent c66dd4d39c
commit c1166680a8
31 changed files with 412 additions and 2221 deletions

View file

@ -1,62 +1,29 @@
import { watch } from "node:fs/promises";
import chalk from "chalk";
import {
CLIError,
MuseError,
compileMarkdownInPlaybill,
loadFromBinding,
resolveBindingPath,
} from "#lib";
import { renderPlaybillToHTML } from "#render/html";
import {
type CLIArguments,
getAbsoluteDirname,
parseCLIArguments,
USAGE,
} from "#util";
import { MuseError } from "./errors";
import { loadBinding, type Binding } from "./binding";
import { type CLIArguments, parseCLIArguments, USAGE } from "./args";
enum ExitCode {
Success = 0,
Error = 1,
}
async function processBinding({ inputFilePath, options }: CLIArguments) {
async function processBinding({ inputFilePath }: CLIArguments) {
// Load the binding
const bindingPath = await resolveBindingPath(inputFilePath);
const binding = await loadFromBinding(bindingPath);
const binding = await loadBinding(inputFilePath);
// If --check is specified, exit early
if (options.check) {
console.log(chalk.green("Playbill validated successfully"));
return ExitCode.Success;
// Run the data through all processors
const processedSteps: Binding[] = [binding];
for (const processor of binding.processors) {
const processedStep = await processor.process(binding);
processedSteps.push(processedStep);
}
if (options.markdown) {
await compileMarkdownInPlaybill(binding);
}
const finalState = processedSteps[processedSteps.length - 1];
const serialized = JSON.stringify(finalState.entries, null, 2);
// Serialize (default: JSON)
let serializedPlaybill = "";
switch (options.renderer) {
case "json":
serializedPlaybill = binding.playbill.serialize();
break;
case "html":
serializedPlaybill = await renderPlaybillToHTML(binding);
break;
default:
throw new CLIError(`Unknown renderer: ${options.renderer}`);
}
// Write to disk if --outfile is specified
if (options.outfile !== "") {
await Bun.write(options.outfile, serializedPlaybill);
return ExitCode.Success;
}
// Otherwise, write to stdout
console.log(serializedPlaybill);
// Otherwise
console.log(serialized);
return ExitCode.Success;
}
@ -70,39 +37,9 @@ async function main(): Promise<number> {
return ExitCode.Success;
}
let lastProcessResult = await processBinding(cliArguments);
const lastProcessResult = await processBinding(cliArguments);
if (options.watch) {
const watchDir = getAbsoluteDirname(cliArguments.inputFilePath);
console.log(`Watching ${watchDir} for changes...`);
const watcher = watch(watchDir, {
recursive: true,
});
for await (const event of watcher) {
console.log(
`Detected ${event.eventType} on ${event.filename}. Reprocessing...`,
);
try {
lastProcessResult = await processBinding(cliArguments);
if (lastProcessResult === ExitCode.Error) {
console.error(`Error processing ${event.filename}`);
} else {
console.log("Reprocessed changes");
}
} catch (error) {
console.error(`Error processing ${event.filename}`);
console.error(error);
}
}
return ExitCode.Success;
}
return ExitCode.Success;
return lastProcessResult;
}
try {