import * as fs from "fs/promises" import * as fsSync from "fs" import * as path from "path" import { compile } from "json-schema-to-typescript" const BANNER = `/* * This file is autogenerated */\n\n` const dirname = __dirname async function main() { let schemasPath = path.join(dirname, "../../", "crates/theme/schemas") let schemaFiles = (await fs.readdir(schemasPath)).filter((x) => x.endsWith(".json") ) let compiledTypes = new Set() for (let filename of schemaFiles) { let filePath = path.join(schemasPath, filename) const fileContents = await fs.readFile(filePath) let schema = JSON.parse(fileContents.toString()) let compiled = await compile(schema, schema.title, { bannerComment: "", }) let eachType = compiled.split("export") for (let type of eachType) { if (!type) { continue } compiledTypes.add("export " + type.trim()) } } let output = BANNER + Array.from(compiledTypes).join("\n\n") let outputPath = path.join(dirname, "../../styles/src/types/zed.ts") try { let existing = await fs.readFile(outputPath) if (existing.toString() == output) { // Skip writing if it hasn't changed console.log("Schemas are up to date") return } } catch (e) { // It's fine if there's no output from a previous run. // @ts-ignore if (e.code !== "ENOENT") { throw e } } const typesDic = path.dirname(outputPath) if (!fsSync.existsSync(typesDic)) { await fs.mkdir(typesDic) } await fs.writeFile(outputPath, output) console.log(`Wrote Typescript types to ${outputPath}`) } main().catch((e) => { console.error(e) process.exit(1) })