|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +import { execSync } from 'child_process'; |
| 4 | +import { readFileSync, writeFileSync } from 'fs'; |
| 5 | +import { join } from 'path'; |
| 6 | + |
| 7 | +// Legacy schema versions that should remain as JSON Schema draft-07 |
| 8 | +const LEGACY_SCHEMAS = ['2024-11-05', '2025-03-26', '2025-06-18']; |
| 9 | + |
| 10 | +// All schema versions to generate |
| 11 | +const ALL_SCHEMAS = [...LEGACY_SCHEMAS, 'draft']; |
| 12 | + |
| 13 | +// Check if we're in check mode (validate existing schemas match generated ones) |
| 14 | +const CHECK_MODE = process.argv.includes('--check'); |
| 15 | + |
| 16 | +/** |
| 17 | + * Apply JSON Schema 2020-12 transformations to a schema file |
| 18 | + */ |
| 19 | +function applyJsonSchema202012Transformations(schemaPath: string): void { |
| 20 | + let content = readFileSync(schemaPath, 'utf-8'); |
| 21 | + |
| 22 | + // Replace $schema URL |
| 23 | + content = content.replace( |
| 24 | + /http:\/\/json-schema\.org\/draft-07\/schema#/g, |
| 25 | + 'https://json-schema.org/draft/2020-12/schema' |
| 26 | + ); |
| 27 | + |
| 28 | + // Replace "definitions": with "$defs": |
| 29 | + content = content.replace( |
| 30 | + /"definitions":/g, |
| 31 | + '"$defs":' |
| 32 | + ); |
| 33 | + |
| 34 | + // Replace #/definitions/ with #/$defs/ |
| 35 | + content = content.replace( |
| 36 | + /#\/definitions\//g, |
| 37 | + '#/$defs/' |
| 38 | + ); |
| 39 | + |
| 40 | + writeFileSync(schemaPath, content, 'utf-8'); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Generate JSON schema for a specific version |
| 45 | + */ |
| 46 | +function generateSchema(version: string, check: boolean = false): boolean { |
| 47 | + const schemaDir = join('schema', version); |
| 48 | + const schemaTs = join(schemaDir, 'schema.ts'); |
| 49 | + const schemaJson = join(schemaDir, 'schema.json'); |
| 50 | + |
| 51 | + if (check) { |
| 52 | + console.log(`Checking schema for ${version}...`); |
| 53 | + |
| 54 | + // Read existing schema |
| 55 | + const existingSchema = readFileSync(schemaJson, 'utf-8'); |
| 56 | + |
| 57 | + // Generate schema to stdout and capture it |
| 58 | + try { |
| 59 | + const generated = execSync( |
| 60 | + `npx typescript-json-schema --defaultNumberType integer --required --skipLibCheck "${schemaTs}" "*"`, |
| 61 | + { encoding: 'utf-8' } |
| 62 | + ); |
| 63 | + |
| 64 | + let expectedSchema = generated; |
| 65 | + |
| 66 | + // Apply transformations for non-legacy schemas |
| 67 | + if (!LEGACY_SCHEMAS.includes(version)) { |
| 68 | + expectedSchema = expectedSchema.replace( |
| 69 | + /http:\/\/json-schema\.org\/draft-07\/schema#/g, |
| 70 | + 'https://json-schema.org/draft/2020-12/schema' |
| 71 | + ); |
| 72 | + expectedSchema = expectedSchema.replace(/"definitions":/g, '"$defs":'); |
| 73 | + expectedSchema = expectedSchema.replace(/#\/definitions\//g, '#/$defs/'); |
| 74 | + } |
| 75 | + |
| 76 | + // Compare |
| 77 | + if (existingSchema.trim() !== expectedSchema.trim()) { |
| 78 | + console.error(` ✗ Schema ${version} is out of date!`); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + console.log(` ✓ Schema ${version} is up to date`); |
| 83 | + return true; |
| 84 | + } catch (error) { |
| 85 | + console.error(`Failed to check schema for ${version}`); |
| 86 | + throw error; |
| 87 | + } |
| 88 | + } else { |
| 89 | + console.log(`Generating schema for ${version}...`); |
| 90 | + |
| 91 | + // Run typescript-json-schema |
| 92 | + try { |
| 93 | + execSync( |
| 94 | + `npx typescript-json-schema --defaultNumberType integer --required --skipLibCheck "${schemaTs}" "*" -o "${schemaJson}"`, |
| 95 | + { stdio: 'inherit' } |
| 96 | + ); |
| 97 | + } catch (error) { |
| 98 | + console.error(`Failed to generate schema for ${version}`); |
| 99 | + throw error; |
| 100 | + } |
| 101 | + |
| 102 | + // Apply transformations for non-legacy schemas |
| 103 | + if (!LEGACY_SCHEMAS.includes(version)) { |
| 104 | + console.log(`Applying JSON Schema 2020-12 transformations to ${version}...`); |
| 105 | + applyJsonSchema202012Transformations(schemaJson); |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Main function |
| 114 | + */ |
| 115 | +function main(): void { |
| 116 | + if (CHECK_MODE) { |
| 117 | + console.log('Checking JSON schemas...\n'); |
| 118 | + |
| 119 | + let allValid = true; |
| 120 | + for (const version of ALL_SCHEMAS) { |
| 121 | + const valid = generateSchema(version, true); |
| 122 | + if (!valid) { |
| 123 | + allValid = false; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + console.log(); |
| 128 | + if (!allValid) { |
| 129 | + console.error('Error: Some schemas are out of date. Run: npm run generate:schema:json'); |
| 130 | + process.exit(1); |
| 131 | + } else { |
| 132 | + console.log('All schemas are up to date!'); |
| 133 | + } |
| 134 | + } else { |
| 135 | + console.log('Generating JSON schemas...\n'); |
| 136 | + |
| 137 | + for (const version of ALL_SCHEMAS) { |
| 138 | + generateSchema(version, false); |
| 139 | + } |
| 140 | + |
| 141 | + console.log('\nSchema generation complete!'); |
| 142 | + console.log(`- (draft-07): ${LEGACY_SCHEMAS.join(', ')}`); |
| 143 | + console.log(`- (2020-12): draft`); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +main(); |
0 commit comments