UNPKG

@paima/schema2typebox

Version:

Creates typebox code from JSON schemas

83 lines (75 loc) 3.14 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HELP_TEXT = exports.DEFAULT_OUTPUT_FILE_NAME = exports.DEFAULT_INPUT_FILE_NAME = exports.createOutputPath = exports.createInputPath = exports.runCli = void 0; const glob_1 = require("glob"); const minimist_1 = __importDefault(require("minimist")); const node_fs_1 = require("node:fs"); 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", protobuf: "p", }, default: { input: exports.DEFAULT_INPUT_FILE_NAME, output: exports.DEFAULT_OUTPUT_FILE_NAME, }, }); if (args.help) { return process.stdout.write(exports.HELP_TEXT); } const inputPath = (0, exports.createInputPath)(args.input); const isGlob = inputPath.includes("*"); const typeboxCode = await (0, programmatic_usage_1.schema2typebox)({ input: isGlob ? (await (0, glob_1.glob)(inputPath)).map((file) => { return (0, node_fs_1.readFileSync)(file, "utf-8"); }) : (0, node_fs_1.readFileSync)(inputPath, "utf-8"), protobuf: args.protobuf, }); if (args["output-stdout"]) { return process.stdout.write(typeboxCode); } return (0, node_fs_1.writeFileSync)((0, exports.createOutputPath)(args.output), typeboxCode, "utf-8"); }; exports.runCli = runCli; const createInputPath = (inputFileName) => { return process.cwd() + `/${inputFileName}`; }; exports.createInputPath = createInputPath; const createOutputPath = (outputFileName) => { return process.cwd() + `/${outputFileName}`; }; exports.createOutputPath = createOutputPath; exports.DEFAULT_INPUT_FILE_NAME = "schema.json"; exports.DEFAULT_OUTPUT_FILE_NAME = "generated-typebox.ts"; exports.HELP_TEXT = ` 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 "${exports.DEFAULT_INPUT_FILE_NAME}". -o, --output Specifies the relative path to generated file that will contain the typebox code. Defaults to "${exports.DEFAULT_OUTPUT_FILE_NAME}". --output-stdout Does not generate an output file and prints the generated code to stdout instead. Has precedence over -o/--output. --protobuf Modify output to fix protoschema-jsonschema issues. `; //# sourceMappingURL=cli.js.map