Organize new setup

This commit is contained in:
Endeavorance 2025-04-03 10:16:08 -04:00
parent 3682a7a763
commit 16660823ea
11 changed files with 165 additions and 142 deletions

View file

@ -1,150 +0,0 @@
import { parseArgs } from "node:util";
import chalk from "chalk";
import { version } from "../package.json";
import { loadBinding } from "./binding";
import { MuseError } from "./errors";
import type { Binding, CLIArguments } from "./types";
interface Loggers {
log: (msg: string) => void;
verbose: (msg: string) => void;
}
enum ExitCode {
Success = 0,
Error = 1,
}
export const USAGE = `
${chalk.bold("muse")} - Compile and process troves of data
${chalk.dim(`v${version}`)}
Usage:
muse [/path/to/binding.yaml] <options>
Options:
--stdout -s Output final data to stdout
--verbose, -v Enable verbose logging
--help, -h Show this help message
`.trim();
/**
* Given an array of CLI arguments, parse them into a structured object
*
* @param argv The arguments to parse
* @returns The parsed CLI arguments
*
* @throws {CLIError} if the arguments are invalid
*/
export function parseCLIArguments(argv: string[]): CLIArguments {
const { values: options, positionals: args } = parseArgs({
args: argv,
options: {
help: {
short: "h",
default: false,
type: "boolean",
},
verbose: {
short: "v",
default: false,
type: "boolean",
},
stdout: {
short: "s",
default: false,
type: "boolean",
},
},
strict: true,
allowPositionals: true,
});
return {
inputFilePath: args[0] ?? "./binding.yaml",
flags: {
help: options.help,
verbose: options.verbose,
stdout: options.stdout,
},
};
}
async function processBinding(
{ inputFilePath, flags }: 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 lastStep = processedSteps[processedSteps.length - 1];
const { process, name } = processor;
log(chalk.bold(`${name}`) + chalk.dim(` (${processor.description})`));
const thisStep = await process(lastStep, binding.options, flags);
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);
if (flags.stdout) {
console.log(serialized);
}
return ExitCode.Success;
}
async function main(): Promise<number> {
const cliArguments = parseCLIArguments(Bun.argv.slice(2));
const { flags } = cliArguments;
// If --help is specified, print usage and exit
if (flags.help) {
console.log(USAGE);
return ExitCode.Success;
}
const logFn = !flags.stdout ? console.log : () => {};
const verboseFn = flags.verbose
? (msg: string) => {
console.log(chalk.dim(msg));
}
: () => {};
const lastProcessResult = await processBinding(cliArguments, {
log: logFn,
verbose: verboseFn,
});
return lastProcessResult;
}
try {
const exitCode = await main();
process.exit(exitCode);
} catch (error) {
if (error instanceof MuseError) {
console.error(chalk.red(error.fmt()));
} else {
console.error("An unexpected error occurred");
console.error(error);
}
process.exit(1);
}