openclaw/scripts/gen-config-schema.ts
CJ Winslow 34f193c657 add config schema generation for IDE autocomplete
- scripts/gen-config-schema.ts: generates JSON schema + TypeScript types from ClawdbotSchema
- `pnpm schema:gen`
- allow $schema key in config for IDE integration
  - Users can now add "$schema": "./schemas/clawdbot.schema.json" to their
  clawdbot.json for autocomplete without validation errors.
- Document schema generation in configuration and scripts docs
2026-01-30 00:13:57 -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 { OpenClawSchema } 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 = OpenClawSchema.toJSONSchema({
target: "draft-07",
unrepresentable: "any",
});
const schemaPath = join(schemasDir, "openclaw.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>, "OpenClawConfig", {
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, "openclaw.d.ts");
await writeFile(dtsPath, dts);
console.log(`Wrote ${dtsPath}`);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});