Update readme and clean up
This commit is contained in:
parent
6a3157762a
commit
435d555394
17 changed files with 320 additions and 486 deletions
94
src/index.ts
94
src/index.ts
|
@ -1,94 +0,0 @@
|
|||
import chalk from "chalk";
|
||||
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, 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 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);
|
||||
|
||||
if (options.stdout) {
|
||||
console.log(serialized);
|
||||
}
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
async function main(): Promise<number> {
|
||||
const cliArguments = parseCLIArguments(Bun.argv.slice(2));
|
||||
const { options } = cliArguments;
|
||||
|
||||
// If --help is specified, print usage and exit
|
||||
if (options.help) {
|
||||
console.log(USAGE);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue