Skip to content

Commit 816fc30

Browse files
committed
Add command for turning JSON schemas into TypeScript typings
1 parent 9ce56a2 commit 816fc30

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

json-schemas.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
5+
import { globSync } from "glob";
6+
import { compileFromFile } from 'json-schema-to-typescript';
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = path.dirname(__filename);
10+
11+
const SRC_DIR = path.join(__dirname, "schemas");
12+
const OUT_DIR = path.join(__dirname, "src");
13+
14+
async function generateTypings() {
15+
const schemas = globSync(`${SRC_DIR}/*.json`);
16+
for (const schema of schemas) {
17+
const outPath = path.join(
18+
OUT_DIR,
19+
`${path.basename(schema, ".json")}.d.ts`,
20+
);
21+
const ts = await compileFromFile(schema, {
22+
bannerComment:
23+
"/* This file was automatically generated by `npm run generate:schemas`. Do not edit by hand. */",
24+
});
25+
fs.writeFileSync(outPath, ts, "utf-8");
26+
}
27+
}
28+
29+
await generateTypings();

package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"ava": "npm run transpile && ava --serial --verbose",
1313
"test": "npm run ava -- src/",
1414
"test-debug": "npm run test -- --timeout=20m",
15-
"transpile": "tsc --build --verbose"
15+
"transpile": "npm run generate:schemas && tsc --build --verbose",
16+
"generate:schemas": "node json-schemas.mjs"
1617
},
1718
"ava": {
1819
"typescript": {
@@ -78,6 +79,7 @@
7879
"eslint-plugin-import": "2.29.1",
7980
"eslint-plugin-no-async-foreach": "^0.1.1",
8081
"glob": "^11.0.3",
82+
"json-schema-to-typescript": "^15.0.4",
8183
"nock": "^14.0.10",
8284
"sinon": "^21.0.0",
8385
"typescript": "^5.9.3"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://json-schema.org/draft/2020-12/schema",
3-
"title": "CodeQL Database Configuration",
3+
"title": "UserConfig",
44
"description": "Format of the config file supplied by the user for CodeQL analysis",
55
"type": "object",
66
"properties": {

0 commit comments

Comments
 (0)