-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild-schemas.ts
More file actions
59 lines (47 loc) · 1.99 KB
/
build-schemas.ts
File metadata and controls
59 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import fs from 'fs';
import path from 'path';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import * as Protocol from '../src/index';
const OUT_DIR = path.resolve(__dirname, '../json-schema');
// Clean output directory ensures no stale files remain
if (fs.existsSync(OUT_DIR)) {
console.log(`Cleaning output directory: ${OUT_DIR}`);
fs.rmSync(OUT_DIR, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
}
// Ensure output directory exists
if (!fs.existsSync(OUT_DIR)) {
fs.mkdirSync(OUT_DIR, { recursive: true });
}
console.log(`Generating JSON Schemas to ${OUT_DIR}...`);
let count = 0;
// Protocol now exports namespaces (Data, UI, System, AI, API)
// We need to iterate through each namespace
for (const [namespaceName, namespaceExports] of Object.entries(Protocol)) {
if (typeof namespaceExports === 'object' && namespaceExports !== null) {
// Create category subdirectory (e.g., data, ui, system, ai, api)
const categoryDir = path.join(OUT_DIR, namespaceName.toLowerCase());
if (!fs.existsSync(categoryDir)) {
fs.mkdirSync(categoryDir, { recursive: true });
}
console.log(`\n[${namespaceName}]`);
// Iterate over all exports in each namespace
for (const [key, value] of Object.entries(namespaceExports)) {
// Check if it looks like a Zod Schema
if (value instanceof z.ZodType) {
const schemaName = key.endsWith('Schema') ? key.replace('Schema', '') : key;
// Convert to JSON Schema
const jsonSchema = zodToJsonSchema(value, {
name: schemaName,
$refStrategy: "none" // We want self-contained schemas for now
});
const fileName = `${schemaName}.json`;
const filePath = path.join(categoryDir, fileName);
fs.writeFileSync(filePath, JSON.stringify(jsonSchema, null, 2));
console.log(`✓ ${namespaceName.toLowerCase()}/${fileName}`);
count++;
}
}
}
}
console.log(`\nSuccessfully generated ${count} schemas.`);