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