* Generate JSON schema for a specific version
(version: string, check: boolean = false)
| 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)) { |
no test coverage detected