openclaw/scripts/gen-config-schema.ts
CJ Winslow f15409bc1e add config schema generation for IDE autocomplete
- scripts/gen-config-schema.ts: generates JSON schema + TypeScript types from ClawdbotSchema
- schemas/clawdbot.schema.json: for $schema reference in config files
- schemas/clawdbot.d.ts: full types without digging into code
- pnpm schema:gen / schema:check scripts (mirrors protocol:check pattern)
- CI: add schema:check to catch drift
2026-01-26 23:43:55 -08:00

51 lines
1.5 KiB
TypeScript

#!/usr/bin/env bun
/**
* Generates schemas/clawdbot.schema.json and schemas/clawdbot.d.ts
* from the zod schema source.
*
* Usage: bun scripts/gen-config-schema.ts
*/
import { writeFile, mkdir } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { compile } from "json-schema-to-typescript";
import { ClawdbotSchema } from "../src/config/zod-schema.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, "..");
const schemasDir = join(rootDir, "schemas");
async function main() {
await mkdir(schemasDir, { recursive: true });
// Generate JSON schema from zod
const jsonSchema = ClawdbotSchema.toJSONSchema({
target: "draft-07",
unrepresentable: "any",
});
const schemaPath = join(schemasDir, "clawdbot.schema.json");
await writeFile(schemaPath, JSON.stringify(jsonSchema, null, 2));
console.log(`Wrote ${schemaPath}`);
// Generate TypeScript types from JSON schema
const dts = await compile(jsonSchema as Record<string, unknown>, "ClawdbotConfig", {
bannerComment: `/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/`,
additionalProperties: false,
});
const dtsPath = join(schemasDir, "clawdbot.d.ts");
await writeFile(dtsPath, dts);
console.log(`Wrote ${dtsPath}`);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});