Organize new setup
This commit is contained in:
parent
3682a7a763
commit
16660823ea
11 changed files with 165 additions and 142 deletions
155
src/cli/index.ts
Normal file
155
src/cli/index.ts
Normal file
|
@ -0,0 +1,155 @@
|
|||
import { parseArgs } from "node:util";
|
||||
import chalk from "chalk";
|
||||
import { type Binding, loadBinding } from "#core/binding";
|
||||
import { MuseError } from "#errors";
|
||||
import { version } from "../../package.json";
|
||||
|
||||
interface CLIFlags {
|
||||
help: boolean;
|
||||
verbose: boolean;
|
||||
stdout: boolean;
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
function parseCLIArguments(argv: string[]): [string, CLIFlags] {
|
||||
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 [args[0] ?? "./", options];
|
||||
}
|
||||
|
||||
async function processBinding(
|
||||
inputFilePath: string,
|
||||
flags: CLIFlags,
|
||||
{ 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, description } = processor;
|
||||
|
||||
let processorLogLine = chalk.bold(`↪ ${name}`);
|
||||
|
||||
if (description && description.length > 0) {
|
||||
processorLogLine += chalk.dim(` (${description})`);
|
||||
}
|
||||
|
||||
log(processorLogLine);
|
||||
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 (flags.stdout) {
|
||||
console.log(serialized);
|
||||
}
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
async function main(): Promise<number> {
|
||||
const [inputFilePath, flags] = parseCLIArguments(Bun.argv.slice(2));
|
||||
|
||||
// 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(inputFilePath, flags, {
|
||||
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