|
1 | 1 | #!/usr/bin/env node |
2 | | -import fs from "node:fs"; |
3 | | -import path from "node:path"; |
4 | | -import { fileURLToPath } from "node:url"; |
5 | 2 | import { computeBaseConfigSchemaResponse } from "../src/config/schema-base.js"; |
6 | | -import { formatGeneratedModule } from "./lib/format-generated-module.mjs"; |
7 | 3 |
|
8 | | -const GENERATED_BY = "scripts/generate-base-config-schema.ts"; |
9 | | -const DEFAULT_OUTPUT_PATH = "src/config/schema.base.generated.ts"; |
10 | | -const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
11 | | - |
12 | | -function readIfExists(filePath: string): string | null { |
13 | | - try { |
14 | | - return fs.readFileSync(filePath, "utf8"); |
15 | | - } catch { |
16 | | - return null; |
17 | | - } |
18 | | -} |
19 | | - |
20 | | -function formatTypeScriptModule(source: string, outputPath: string): string { |
21 | | - return formatGeneratedModule(source, { |
22 | | - repoRoot: REPO_ROOT, |
23 | | - outputPath, |
24 | | - errorLabel: "base config schema", |
| 4 | +export function checkBaseConfigSchema(): void { |
| 5 | + computeBaseConfigSchemaResponse({ |
| 6 | + generatedAt: "2026-05-05T00:00:00.000Z", |
25 | 7 | }); |
26 | 8 | } |
27 | 9 |
|
28 | | -export function renderBaseConfigSchemaModule(params?: { generatedAt?: string }): string { |
29 | | - const payload = computeBaseConfigSchemaResponse({ |
30 | | - generatedAt: params?.generatedAt ?? new Date().toISOString(), |
31 | | - }); |
32 | | - return formatTypeScriptModule( |
33 | | - `// Auto-generated by ${GENERATED_BY}. Do not edit directly. |
34 | | -
|
35 | | -import type { BaseConfigSchemaResponse } from "./schema-base.js"; |
36 | | -
|
37 | | -export const GENERATED_BASE_CONFIG_SCHEMA: BaseConfigSchemaResponse = ${JSON.stringify(payload, null, 2)}; |
38 | | -`, |
39 | | - DEFAULT_OUTPUT_PATH, |
40 | | - ); |
41 | | -} |
42 | | - |
43 | | -export function writeBaseConfigSchemaModule(params?: { |
44 | | - repoRoot?: string; |
45 | | - outputPath?: string; |
46 | | - check?: boolean; |
47 | | -}): { changed: boolean; wrote: boolean; outputPath: string } { |
48 | | - const repoRoot = path.resolve(params?.repoRoot ?? REPO_ROOT); |
49 | | - const outputPath = path.resolve(repoRoot, params?.outputPath ?? DEFAULT_OUTPUT_PATH); |
50 | | - const current = readIfExists(outputPath); |
51 | | - const generatedAt = |
52 | | - current?.match(/generatedAt:\s*"([^"]+)"/u)?.[1] ?? |
53 | | - current?.match(/"generatedAt":\s*"([^"]+)"/u)?.[1] ?? |
54 | | - new Date().toISOString(); |
55 | | - const next = renderBaseConfigSchemaModule({ generatedAt }); |
56 | | - const changed = current !== next; |
57 | | - |
58 | | - if (params?.check) { |
59 | | - return { changed, wrote: false, outputPath }; |
60 | | - } |
61 | | - |
62 | | - if (changed) { |
63 | | - fs.writeFileSync(outputPath, next, "utf8"); |
64 | | - } |
65 | | - return { changed, wrote: changed, outputPath }; |
66 | | -} |
67 | | - |
68 | 10 | const args = new Set(process.argv.slice(2)); |
69 | 11 | if (args.has("--check") && args.has("--write")) { |
70 | 12 | throw new Error("Use either --check or --write, not both."); |
71 | 13 | } |
72 | 14 |
|
73 | 15 | if (import.meta.url === new URL(process.argv[1] ?? "", "file://").href) { |
74 | | - const result = writeBaseConfigSchemaModule({ check: args.has("--check") }); |
75 | | - if (result.changed) { |
76 | | - if (args.has("--check")) { |
77 | | - console.error( |
78 | | - `[base-config-schema] stale generated output at ${path.relative(process.cwd(), result.outputPath)}`, |
79 | | - ); |
80 | | - process.exitCode = 1; |
81 | | - } else { |
82 | | - console.log(`[base-config-schema] wrote ${path.relative(process.cwd(), result.outputPath)}`); |
83 | | - } |
| 16 | + checkBaseConfigSchema(); |
| 17 | + if (args.has("--write")) { |
| 18 | + console.log("[base-config-schema] runtime-computed; no generated file to write"); |
| 19 | + } else { |
| 20 | + console.log("[base-config-schema] ok"); |
84 | 21 | } |
85 | 22 | } |
0 commit comments