homebridge-prometheus-exporter/code-generation/config-scheme-gen.js
Lars Strojny 52efa69bf0
Automate code generation for config schema (#16)
Use a fork of https://github.com/lstrojny/json-schema-to-zod to generate
the boundary check for the config automatically.
2022-11-10 13:06:16 +01:00

29 lines
813 B
JavaScript
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
const { parseSchema } = require('json-schema-to-zod-with-defaults')
const { schema } = require('../config.schema.json')
const { format } = require('prettier')
const { join, basename } = require('path')
const prettierConfig = require('../prettier.config')
const { writeFileSync } = require('fs')
const file = join(__dirname, '../src/generated/config_boundary.ts')
console.log(`Starting code generation for ${file}`)
const zodSchema = parseSchema(schema, true)
const code = format(
`
// Auto-generated by "${join(basename(__dirname), basename(__filename))}", dont manually edit
import { z } from 'zod'
export const ConfigBoundary = ${zodSchema}
`,
{ filepath: 'codegen.ts', ...prettierConfig },
)
writeFileSync(file, code)
console.log(`Finished code generation for ${file}`)