UNPKG

@spec2ts/jsonschema

Version:

Utility to convert JSON Schemas to Typescript using TypeScript native compiler

62 lines (60 loc) 1.77 kB
import { n as parseSchemaFile } from "./schema-parser-D7kSv_ZQ.mjs"; import { cli, printer } from "@spec2ts/core"; //#region src/cli/command.ts const usage = "$0 <input..>"; const describe = "Generate TypeScript types from JSON Schemas"; function builder(argv) { return argv.positional("input", { array: true, type: "string", describe: "Path to JSON Schema(s) to convert to TypeScript", demandOption: true }).option("output", { type: "string", alias: "o", describe: "Output directory for generated types" }).option("ext", { type: "string", alias: "e", describe: "Output extension for generated types", choices: [".d.ts", ".ts"] }).option("cwd", { type: "string", alias: "c", describe: "Root directory for resolving $refs" }).option("avoidAny", { type: "boolean", describe: "Avoid the `any` type and use `unknown` instead" }).option("enableDate", { choices: [ true, "strict", "lax" ], describe: "Build `Date` for format `date` and `date-time`" }).option("banner", { type: "string", alias: "b", describe: "Comment prepended to the top of each generated file" }); } async function handler(options) { const files = await cli.findFiles(options.input); for (const file of files) { const ast = await parseSchemaFile(file, options); const content = printer.printNodes(ast); const output = cli.getOutputPath(file, options); await cli.mkdirp(output); await cli.writeFile(output, (options.banner || defaultBanner()) + "\n\n" + content); } } function defaultBanner() { return `/** * DO NOT MODIFY * Generated using @spec2ts/jsonschema. * See https://www.npmjs.com/package/@spec2ts/jsonschema */ /* eslint-disable */`; } //#endregion export { usage as i, describe as n, handler as r, builder as t };