@gent-js/gent
Version:
template-based data generator.
107 lines (106 loc) • 4.18 kB
JavaScript
import { Command, Option } from "commander";
import { packageEnv } from "../generated/packageEnv.js";
import { FAILED, SUCCEEDED } from "./cliConsts.js";
import { DEFAULT_TEMPLATE_WEIGHT } from "./consts.js";
import { run } from "./run.js";
import { determineTemplateModeByFile, isNonNullObject, normalizeProgramOptions, parseAndResolveFilePath, parseString, tryReadFile, } from "./utils.js";
const templateOption = new Option("-t --template <template-file>", "path to template file");
const metaOption = new Option("-m --meta <meta-file>", "path to meta file");
const countOption = new Option("-c --count <number>", "number of logs to generate.").default(1);
const fromOption = new Option("-s --start <date-expression>", "from date");
const toOption = new Option("-e --end <date-expression>", "end date");
const outOption = new Option("-o --out <path>", "path to output files.").default("./out.log");
const debugOption = new Option("-d --debug", "debug flat")
.default(false)
.hideHelp(true);
function main() {
const program = new Command();
program
.name(packageEnv.name)
.version(packageEnv.version)
.description(packageEnv.description)
.showHelpAfterError()
.addOption(templateOption)
.addOption(metaOption)
.addOption(fromOption)
.addOption(toOption)
.addOption(countOption)
.addOption(outOption)
.addOption(debugOption)
.action(async (...args) => {
const [options] = args;
if (!isNonNullObject(options)) {
program.error("Invalid option values.", { exitCode: FAILED });
return;
}
const cwd = process.cwd();
const meta = parseString(options["meta"]);
const template = parseString(options["template"]);
let rawProgramOptions;
if (meta !== undefined) {
const resolvedFilePath = parseAndResolveFilePath(meta, cwd);
if (resolvedFilePath === undefined) {
program.error(`failed to resolve meta file path.(${meta})`, {
exitCode: FAILED,
});
return;
}
let fileContent;
try {
fileContent = await tryReadFile(resolvedFilePath);
}
catch (error) {
console.log(error);
}
if (fileContent === undefined) {
program.error("failed to read meta file.", { exitCode: FAILED });
return;
}
try {
rawProgramOptions = JSON.parse(fileContent);
}
catch (error) {
console.log(error);
rawProgramOptions = undefined;
}
if (rawProgramOptions === undefined) {
program.error("failed to parse meta file.", { exitCode: FAILED });
return;
}
}
else if (template !== undefined) {
const mode = determineTemplateModeByFile(template);
const templateOptions = {
mode: mode,
path: template,
weight: DEFAULT_TEMPLATE_WEIGHT,
};
rawProgramOptions = {
debug: options["debug"],
from: options["start"],
to: options["end"],
count: options["count"],
out: options["out"],
templates: [templateOptions],
};
}
else {
program.error("You must specify either template or meta option at least.", { exitCode: FAILED });
return;
}
const programOptions = normalizeProgramOptions(rawProgramOptions, cwd);
if (programOptions === undefined) {
program.error("invalid options.", { exitCode: FAILED });
return;
}
const resultCode = await run(programOptions);
if (resultCode !== SUCCEEDED) {
program.error("Command has failed", { exitCode: resultCode });
}
});
program.parseAsync(process.argv).catch((error) => {
console.error(error);
});
}
main();