2022-11-10 13:06:16 +01:00
|
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
2022-12-09 22:30:27 +01:00
|
|
|
|
const { parseSchema } = require('json-schema-to-zod')
|
2022-11-10 13:06:16 +01:00
|
|
|
|
const { schema } = require('../config.schema.json')
|
|
|
|
|
const { format } = require('prettier')
|
|
|
|
|
const { join, basename } = require('path')
|
|
|
|
|
const prettierConfig = require('../prettier.config')
|
2022-11-17 13:19:22 +01:00
|
|
|
|
const { writeFileSync, readFileSync } = require('fs')
|
2022-11-10 13:06:16 +01:00
|
|
|
|
|
|
|
|
|
const file = join(__dirname, '../src/generated/config_boundary.ts')
|
|
|
|
|
|
|
|
|
|
console.log(`Starting code generation for ${file}`)
|
|
|
|
|
|
2022-12-09 22:30:27 +01:00
|
|
|
|
const zodSchema = parseSchema(schema, false)
|
2022-11-10 13:06:16 +01:00
|
|
|
|
|
|
|
|
|
const code = format(
|
|
|
|
|
`
|
|
|
|
|
// Auto-generated by "${join(basename(__dirname), basename(__filename))}", don’t manually edit
|
|
|
|
|
|
|
|
|
|
import { z } from 'zod'
|
|
|
|
|
|
|
|
|
|
export const ConfigBoundary = ${zodSchema}
|
|
|
|
|
`,
|
|
|
|
|
{ filepath: 'codegen.ts', ...prettierConfig },
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
writeFileSync(file, code)
|
|
|
|
|
|
2022-11-17 13:19:22 +01:00
|
|
|
|
const note = 'AUTOGENERATED CONFIG DOCS'
|
|
|
|
|
const comment = (...strings) => `<!-- ${strings.join(' ')} -->`
|
|
|
|
|
const readmePath = join(__dirname, '../README.md')
|
|
|
|
|
const readme = readFileSync(readmePath).toString()
|
|
|
|
|
const regex = new RegExp(`${comment(note, 'BEGIN')}.*${comment(note, 'END')}`, 'mgs')
|
|
|
|
|
if (!readme.match(regex)) {
|
|
|
|
|
console.log('Could not update README.md')
|
|
|
|
|
process.exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeFileSync(
|
|
|
|
|
readmePath,
|
|
|
|
|
readme.replace(
|
|
|
|
|
regex,
|
|
|
|
|
`${comment(note, 'BEGIN')}\n\`\`\`json5\n${generateDocs(schema)}\n\`\`\`\n${comment(note, 'END')}`,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
function generateDocs(schema) {
|
|
|
|
|
const doc = indent(
|
|
|
|
|
Object.entries(schema.properties)
|
|
|
|
|
.map(([property, definition]) => {
|
|
|
|
|
const lines = []
|
|
|
|
|
|
|
|
|
|
if (definition.title) {
|
|
|
|
|
lines.push(`// ${definition.title}`)
|
|
|
|
|
}
|
|
|
|
|
if (definition.description) {
|
|
|
|
|
lines.push(`//\n// ${wordwrap(definition.description, 80, '\n// ')}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines.push(`${JSON.stringify(property)}: ${JSON.stringify('<' + definition.type + '>')}`)
|
|
|
|
|
|
|
|
|
|
return lines.join('\n')
|
|
|
|
|
})
|
|
|
|
|
.join(',\n\n\n'),
|
|
|
|
|
6,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return `{
|
|
|
|
|
// ...
|
|
|
|
|
"platforms": [
|
|
|
|
|
{
|
|
|
|
|
"platform": "PrometheusExporter",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
${doc}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wordwrap(word, length, wrap = '\n') {
|
|
|
|
|
const wrapped = []
|
|
|
|
|
while (word.length > length) {
|
|
|
|
|
const cut = word.substring(0, length)
|
|
|
|
|
const pos = cut.lastIndexOf(' ')
|
|
|
|
|
wrapped.push(cut.substring(0, pos))
|
|
|
|
|
word = word.substring(pos + 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wrapped.push(word)
|
|
|
|
|
|
|
|
|
|
return wrapped.join(wrap)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function indent(string, indent) {
|
|
|
|
|
return string.replace(/^(.+)$/gm, `${' '.repeat(indent)}$1`)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 13:06:16 +01:00
|
|
|
|
console.log(`Finished code generation for ${file}`)
|