homebridge-prometheus-exporter/code-generation/hap-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

36 lines
1.1 KiB
JavaScript
Executable file
Raw Permalink 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 hap = require('hap-nodejs')
const { format } = require('prettier')
const prettierConfig = require('../prettier.config')
const { writeFileSync } = require('fs')
const { join, basename } = require('path')
const uuidToServiceMap = {}
const serviceToUuidMap = {}
const file = join(__dirname, '../src/generated/services.ts')
console.log(`Starting code generation for ${file}`)
for (const [name, service] of Object.entries(hap.Service)) {
if (typeof service !== 'function' || typeof service.UUID !== 'string') {
console.log(`Skipping ${typeof service} ${name}`)
continue
}
uuidToServiceMap[service.UUID] = name
serviceToUuidMap[name] = service.UUID
}
const code = format(
`
// Auto-generated by "${join(basename(__dirname), basename(__filename))}", dont manually edit
export const Uuids: Record<string,string> = ${JSON.stringify(uuidToServiceMap)} as const
export const Services: Record<string,string> = ${JSON.stringify(serviceToUuidMap)} as const
`,
{ filepath: 'codegen.ts', ...prettierConfig },
)
writeFileSync(file, code)
console.log(`Finished code generation for ${file}`)