|
| 1 | +/** |
| 2 | + * Convert JSON schema file to TypeScript file with embedded JSON schema and type definition. |
| 3 | + * |
| 4 | + * Some Javascript runtimes cannot import JSON files as ES module, so embedding the JSON schema is a workaround. |
| 5 | + */ |
| 6 | +import { exec as cbExec } from "node:child_process"; |
| 7 | +import { watch } from "node:fs"; |
| 8 | +import { readFile, writeFile } from "node:fs/promises"; |
| 9 | +import { basename, extname, join } from "node:path"; |
| 10 | +import { promisify } from "node:util"; |
| 11 | + |
| 12 | +import { Command } from "@commander-js/extra-typings"; |
| 13 | +import { compileFromFile } from "json-schema-to-typescript"; |
| 14 | + |
| 15 | +async function format(schemaTsPath) { |
| 16 | + // Format the generated TypeScript file |
| 17 | + // Expect this script to be run from packages/class |
| 18 | + const exec = promisify(cbExec); |
| 19 | + const biomeHome = join(process.cwd(), "../.."); |
| 20 | + const { stdout, stderr } = await exec( |
| 21 | + `pnpm biome format --write packages/class/${schemaTsPath}`, |
| 22 | + { |
| 23 | + cwd: biomeHome, |
| 24 | + }, |
| 25 | + ); |
| 26 | + console.log(stdout); |
| 27 | + console.error(stderr); |
| 28 | +} |
| 29 | + |
| 30 | +async function readJsonSchema(jsonSchemaPath) { |
| 31 | + const jsonSchema = await readFile(jsonSchemaPath, "utf-8"); |
| 32 | + const trimmedJsonSchema = jsonSchema.trim(); |
| 33 | + return trimmedJsonSchema; |
| 34 | +} |
| 35 | + |
| 36 | +function prefixOfJsonSchema(jsonSchemaPath) { |
| 37 | + const fn = basename(jsonSchemaPath, extname(jsonSchemaPath)); |
| 38 | + // json-schema-to-typescript generates type names with base of filename with the first letter capitalized |
| 39 | + const prefix = fn.charAt(0).toUpperCase() + fn.slice(1); |
| 40 | + return prefix; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * |
| 45 | + * @param {string} jsonSchemaPath |
| 46 | + * @param {string} schemaTsPath |
| 47 | + */ |
| 48 | +async function json2ts(jsonSchemaPath, schemaTsPath) { |
| 49 | + // Geneerate TypeScript type definition from JSON schema |
| 50 | + const tsOfJsonSchema = await compileFromFile(jsonSchemaPath, { |
| 51 | + format: false, |
| 52 | + bannerComment: "", |
| 53 | + }); |
| 54 | + |
| 55 | + const prefix = prefixOfJsonSchema(jsonSchemaPath); |
| 56 | + |
| 57 | + // Read JSON schema file |
| 58 | + const trimmedJsonSchema = await readJsonSchema(jsonSchemaPath); |
| 59 | + |
| 60 | + // Combine types and JSON schema into a single TypeScript file |
| 61 | + const body = `\ |
| 62 | +/** |
| 63 | + * This file was automatically generated by "../scripts/json2ts.mjs" script. |
| 64 | + * DO NOT MODIFY IT BY HAND. Instead, modify the JSON schema file "${jsonSchemaPath}", |
| 65 | + * and run "pnpm json2ts" to regenerate this file. |
| 66 | + */ |
| 67 | +import type { JSONSchemaType } from "ajv/dist/2019.js"; |
| 68 | +${tsOfJsonSchema} |
| 69 | +export type JsonSchemaOf${prefix} = JSONSchemaType<${prefix}>; |
| 70 | +/** |
| 71 | + * JSON schema of ${jsonSchemaPath} embedded in a TypeScript file. |
| 72 | + */ |
| 73 | +export const jsonSchemaOf${prefix} = ${trimmedJsonSchema} as unknown as JsonSchemaOf${prefix}; |
| 74 | +`; |
| 75 | + await writeFile(schemaTsPath, body, { flag: "w" }); |
| 76 | + |
| 77 | + await format(schemaTsPath); |
| 78 | +} |
| 79 | + |
| 80 | +function watchJsonSchema(jsonSchemaPath, schemaTsPath) { |
| 81 | + console.log(`Watching ${jsonSchemaPath} for changes`); |
| 82 | + watch(jsonSchemaPath, (event) => { |
| 83 | + if (event !== "change") { |
| 84 | + return; |
| 85 | + } |
| 86 | + console.log( |
| 87 | + `File ${jsonSchemaPath} has been changed, regenerating ${schemaTsPath}`, |
| 88 | + ); |
| 89 | + json2ts(jsonSchemaPath, schemaTsPath).catch((err) => { |
| 90 | + console.error(err); |
| 91 | + }); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +function main() { |
| 96 | + const program = new Command() |
| 97 | + .name("json2ts") |
| 98 | + .description( |
| 99 | + "Convert JSON schema file to TypeScript file with embedded JSON schema and type definition", |
| 100 | + ) |
| 101 | + .option( |
| 102 | + "-i, --input <jsonSchemaPath>", |
| 103 | + "JSON schema file", |
| 104 | + "src/config.json", |
| 105 | + ) |
| 106 | + .option("-o, --output <schemaTsPath>", "Output file path", "src/config.ts") |
| 107 | + .option("--watch", "Watch mode") |
| 108 | + .parse(); |
| 109 | + const options = program.opts(); |
| 110 | + const jsonSchemaPath = options.input; |
| 111 | + const schemaTsPath = options.output; |
| 112 | + |
| 113 | + if (options.watch) { |
| 114 | + watchJsonSchema(jsonSchemaPath, schemaTsPath); |
| 115 | + } else { |
| 116 | + console.log(`Generating ${schemaTsPath} from ${jsonSchemaPath}`); |
| 117 | + json2ts(jsonSchemaPath, schemaTsPath).catch((err) => { |
| 118 | + console.error(err); |
| 119 | + process.exit(1); |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +main(); |
0 commit comments