Reworked support for processing

This commit is contained in:
Endeavorance 2025-04-02 11:45:08 -04:00
parent c1166680a8
commit 6a3157762a
14 changed files with 318 additions and 473 deletions

View file

@ -1,29 +1,56 @@
import chalk from "chalk";
import { MuseError } from "./errors";
import { MuseError } from "./errors.ts";
import { loadBinding, type Binding } from "./binding";
import { type CLIArguments, parseCLIArguments, USAGE } from "./args";
interface Loggers {
log: (msg: string) => void;
verbose: (msg: string) => void;
}
enum ExitCode {
Success = 0,
Error = 1,
}
async function processBinding({ inputFilePath }: CLIArguments) {
async function processBinding(
{ inputFilePath, options }: CLIArguments,
{ log, verbose }: Loggers,
) {
// Load the binding
const binding = await loadBinding(inputFilePath);
verbose(`Binding ${binding.bindingPath}`);
const stepWord = binding.processors.length === 1 ? "step" : "steps";
log(
`Processing ${binding.entries.length} entries with ${binding.processors.length} ${stepWord}`,
);
const processStart = performance.now();
// 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);
const lastStep = processedSteps[processedSteps.length - 1];
const { process, name } = processor;
log(chalk.bold(`${name}`) + chalk.dim(` (${processor.description})`));
const thisStep = await process(lastStep, binding.options);
processedSteps.push(thisStep);
}
const processEnd = performance.now();
const processTime = ((processEnd - processStart) / 1000).toFixed(2);
verbose(`Processing completed in ${processTime}s`);
const finalState = processedSteps[processedSteps.length - 1];
const serialized = JSON.stringify(finalState.entries, null, 2);
// Otherwise
console.log(serialized);
if (options.stdout) {
console.log(serialized);
}
return ExitCode.Success;
}
@ -37,7 +64,17 @@ async function main(): Promise<number> {
return ExitCode.Success;
}
const lastProcessResult = await processBinding(cliArguments);
const logFn = !options.stdout ? console.log : () => {};
const verboseFn = options.verbose
? (msg: string) => {
console.log(chalk.dim(msg));
}
: () => {};
const lastProcessResult = await processBinding(cliArguments, {
log: logFn,
verbose: verboseFn,
});
return lastProcessResult;
}