schema.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bun
  2. import { z } from "zod"
  3. import { Config } from "@/config/config"
  4. import { zodObject } from "@opencode-ai/core/effect-zod"
  5. import { TuiConfig } from "../src/cli/cmd/tui/config/tui"
  6. function generate(schema: z.ZodType) {
  7. const result = z.toJSONSchema(schema, {
  8. io: "input", // Generate input shape (treats optional().default() as not required)
  9. /**
  10. * We'll use the `default` values of the field as the only value in `examples`.
  11. * This will ensure no docs are needed to be read, as the configuration is
  12. * self-documenting.
  13. *
  14. * See https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.9.5
  15. */
  16. override(ctx) {
  17. const schema = ctx.jsonSchema
  18. // Preserve strictness: set additionalProperties: false for objects
  19. if (
  20. schema &&
  21. typeof schema === "object" &&
  22. schema.type === "object" &&
  23. schema.additionalProperties === undefined
  24. ) {
  25. schema.additionalProperties = false
  26. }
  27. // Add examples and default descriptions for string fields with defaults
  28. if (schema && typeof schema === "object" && "type" in schema && schema.type === "string" && schema?.default) {
  29. if (!schema.examples) {
  30. schema.examples = [schema.default]
  31. }
  32. schema.description = [schema.description || "", `default: \`${String(schema.default)}\``]
  33. .filter(Boolean)
  34. .join("\n\n")
  35. .trim()
  36. }
  37. },
  38. }) as Record<string, unknown> & {
  39. allowComments?: boolean
  40. allowTrailingCommas?: boolean
  41. }
  42. // used for json lsps since config supports jsonc
  43. result.allowComments = true
  44. result.allowTrailingCommas = true
  45. return result
  46. }
  47. const configFile = process.argv[2]
  48. const tuiFile = process.argv[3]
  49. console.log(configFile)
  50. await Bun.write(configFile, JSON.stringify(generate(zodObject(Config.Info).strict().meta({ ref: "Config" })), null, 2))
  51. if (tuiFile) {
  52. console.log(tuiFile)
  53. await Bun.write(tuiFile, JSON.stringify(generate(TuiConfig.JsonSchemaInfo), null, 2))
  54. }