schema2typebox
Version:
Creates typebox code from JSON schemas
74 lines (67 loc) • 2.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHelpText = exports.runCli = void 0;
const minimist_1 = __importDefault(require("minimist"));
const node_fs_1 = require("node:fs");
const node_stream_1 = require("node:stream");
const package_json_1 = __importDefault(require("../../package.json"));
const programmatic_usage_1 = require("../programmatic-usage");
const runCli = async () => {
const args = (0, minimist_1.default)(process.argv.slice(2), {
alias: {
input: "i",
output: "o",
help: "h",
},
default: {
input: "schema.json",
output: "generated-typebox.ts",
},
});
// TODO: narrow condition.
if (args.help) {
return process.stdout.write(exports.getHelpText.run());
}
const inputFileAsString = (0, node_fs_1.readFileSync)(process.cwd() + `/${args.input ?? "schema.json"}`, "utf-8");
const typeboxCode = await (0, programmatic_usage_1.schema2typebox)({
input: inputFileAsString,
});
const generatedCodeStream = node_stream_1.Readable.from(typeboxCode.split(/(\r\n|\r|\n)/));
if (args["output-stdout"]) {
return generatedCodeStream.pipe(process.stdout);
}
const outputPath = process.cwd() + `/${args.output ?? "generated-typebox.ts"}`;
return generatedCodeStream.pipe((0, node_fs_1.createWriteStream)(outputPath));
};
exports.runCli = runCli;
/**
* Declaring this as function in order to make it better testable.
* Using an object to be able to mock it and track its usage.
*/
exports.getHelpText = {
run: () => {
return `
schema2typebox generates TypeBox code from JSON schemas. The generated
output is formatted based on the prettier config inside your repo (or the
default one, if you don't have one). Version: ${package_json_1.default.version}
Usage:
schema2typebox [ARGUMENTS]
Arguments:
-h, --help
Displays this menu.
-i, --input
Specifies the relative path to the file containing the JSON schema that
will be used to generated typebox code. Defaults to "schema.json".
-o, --output
Specifies the relative path to generated file that will contain the
typebox code. Defaults to "generated-typebox.ts".
--output-stdout
Does not generate an output file and prints the generated code to stdout
instead. Has precedence over -o/--output.
`;
},
};
//# sourceMappingURL=cli.js.map