Skip to content

Commit 6b120b8

Browse files
adding auto updating schema checker
1 parent 972f086 commit 6b120b8

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

src/scripts/generate-schema.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import fs from "fs";
22
import path from "path";
33
import prettier from "prettier";
44
import { JsonSchema, jsonSchemaToZod } from "json-schema-to-zod";
5+
import { getLatestSchemaVersion } from "./get-latest-schema.js";
56

6-
const schemaVersion = "2.0.0";
7-
const schemaURL = `https://raw.githubusercontent.com/DSACMS/gov-codejson/refs/heads/main/schemas/cms/schema-${schemaVersion}.json`;
7+
const SCHEMA_BASE_URL = "https://raw.githubusercontent.com/DSACMS/gov-codejson/refs/heads/main/schemas/cms";
88
const filePath = "src/types/CodeJSONSchema.ts";
99

1010
function allowEmptyUrls(zodCode: string): string {
@@ -63,6 +63,10 @@ async function formatFile(filePath: string) {
6363
}
6464

6565
async function generateSchema() {
66+
const schemaVersion = await getLatestSchemaVersion();
67+
console.log(`Latest schema version: ${schemaVersion}`);
68+
69+
const schemaURL = `${SCHEMA_BASE_URL}/schema-${schemaVersion}.json`;
6670
console.log(`Fetching JSON schema from GitHub...`);
6771
const response = await fetch(schemaURL);
6872

@@ -80,6 +84,7 @@ async function generateSchema() {
8084

8185
const fileContent = `
8286
// DO NOT EDIT - AUTOMATICALLY GENERATED FILE!!!
87+
// Schema Version: ${schemaVersion}
8388
8489
import { z } from "zod";
8590

src/scripts/get-latest-schema.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const GITHUB_API_URL =
2+
"https://api.github.com/repos/DSACMS/gov-codejson/contents/schemas/cms";
3+
4+
export async function getLatestSchemaVersion(): Promise<string> {
5+
const response = await fetch(GITHUB_API_URL, {
6+
headers: {
7+
Accept: "application/vnd.github.v3+json",
8+
...(process.env.GITHUB_TOKEN && {
9+
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
10+
}),
11+
},
12+
});
13+
14+
if (!response.ok) {
15+
throw new Error(`Failed to fetch schema list: ${response.status}`);
16+
}
17+
18+
const files = (await response.json()) as Array<{ name: string }>;
19+
20+
const versions = files
21+
.map((f) => f.name.match(/^schema-(\d+\.\d+\.\d+)\.json$/)?.[1])
22+
.filter(Boolean) as string[];
23+
24+
if (versions.length === 0) {
25+
throw new Error("No schema versions found");
26+
}
27+
28+
const versionNumber = versions.sort((a, b) => b.localeCompare(a, undefined, { numeric: true }))[0];
29+
// console.log(versionNumber)
30+
return versionNumber
31+
}
32+
33+
// try {
34+
// await getLatestSchemaVersion();
35+
// } catch (error) {
36+
// console.error(`GET operation failed!`);
37+
38+
// if (error instanceof Error) {
39+
// console.error(`Error: ${error.message}`);
40+
// } else {
41+
// console.error(`Unknown error:`, error);
42+
// }
43+
// process.exit(1);
44+
// }

0 commit comments

Comments
 (0)