Skip to content

Commit

Permalink
Tiny spike to see how far we can get with generating JSON Schema from…
Browse files Browse the repository at this point in the history
… our TS types
  • Loading branch information
jeremywiebe committed Apr 16, 2024
1 parent 9c53637 commit d256374
Show file tree
Hide file tree
Showing 5 changed files with 5,310 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"sloc": "^0.2.1",
"storybook": "^7.6.17",
"style-loader": "^3.3.3",
"ts-json-schema-generator": "^2.0.1",
"typescript": "^5.4.2",
"typescript-coverage-report": "^0.7.0",
"vite-plugin-istanbul": "^5.0.0",
Expand All @@ -133,6 +134,7 @@
},
"scripts": {
"gen:parsers": "yarn --cwd packages/kas gen:parsers",
"gen:schema": "utils/generate-schema.ts",
"prebuild": "yarn gen:parsers",
"build": "rollup -c config/build/rollup.config.js",
"build:types": "yarn tsc --build tsconfig-build.json",
Expand Down
6 changes: 6 additions & 0 deletions packages/perseus/src/perseus-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ export type PerseusWidgetsMap = {
[key in `video ${number}`]: VideoWidget;
};

/**
* The PerseusItem is the main Perseus data structure for an exercise. It
* forms a question (with interactive widgets), a set of hints, and answer area
* configuration (such as whether to make a calculator or periodic table
* available to the learner).
*/
export type PerseusItem = {
// The details of the question being asked to the user.
question: PerseusRenderer;
Expand Down
27 changes: 27 additions & 0 deletions utils/generate-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env -S node -r @swc-node/register

import fs from "fs";
import path from "path";

import {createGenerator} from "ts-json-schema-generator";

import type {Config} from "ts-json-schema-generator";

const config: Config = {
schemaId: "https://khanacademy.org/schema/perseus.json",
tsconfig: path.join(__dirname, "..", "tsconfig.json"),
jsDoc: "extended",

// type: "PerseusItem",
};

const outputPath = path.join(__dirname, "generated/schema.json");
fs.mkdirSync(path.dirname(outputPath), {recursive: true});

const schema = createGenerator(config).createSchema("PerseusItem"); // config.type);
const schemaString = JSON.stringify(schema, null, 2);
fs.writeFile(outputPath, schemaString, (err) => {
if (err) {
throw err;
}
});
Loading

0 comments on commit d256374

Please sign in to comment.