Skip to content

Commit

Permalink
pipes default values through to json-schema (#9039)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitropoulos committed Aug 29, 2024
1 parent 06a26de commit 765cbf3
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 106 deletions.
6 changes: 4 additions & 2 deletions packages/turbo-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
"build": "tsc && pnpm generate-schema",
"lint": "eslint src/",
"lint:prettier": "prettier -c . --cache",
"generate-schema": "node scripts/generate-schema.mjs"
"generate-schema": "tsx scripts/generate-schema.ts"
},
"devDependencies": {
"@turbo/eslint-config": "workspace:*",
"@turbo/tsconfig": "workspace:*",
"ts-json-schema-generator": "2.3.0"
"@types/node": "^20",
"ts-json-schema-generator": "2.3.0",
"tsx": "^4.19.0"
},
"files": [
"src",
Expand Down
89 changes: 60 additions & 29 deletions packages/turbo-types/schemas/schema.json

Large diffs are not rendered by default.

71 changes: 48 additions & 23 deletions packages/turbo-types/schemas/schema.v1.json

Large diffs are not rendered by default.

89 changes: 60 additions & 29 deletions packages/turbo-types/schemas/schema.v2.json

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions packages/turbo-types/scripts/generate-schema.mjs

This file was deleted.

52 changes: 52 additions & 0 deletions packages/turbo-types/scripts/generate-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env node

import { writeFileSync } from "node:fs";
import { join } from "node:path";
import { createGenerator } from "ts-json-schema-generator";

const __dirname = new URL(".", import.meta.url).pathname;
const packageRoot = join(__dirname, "..", "src");

/**
* post-process the schema recursively to:
* 1. replace any key named `defaultValue` with `default`
* 1. remove any backticks from the value
* 1. attempt to parsing the value as JSON (falling back, if not)
*/
const postProcess = <T>(item: T): T => {
if (typeof item !== "object" || item === null) {
return item;
}
if (Array.isArray(item)) {
return item.map(postProcess) as unknown as T;
}
return Object.fromEntries(
Object.entries(item).map(([key, value]) => {
if (key === "defaultValue" && typeof value === "string") {
const replaced = value.replaceAll(/`/g, "");
try {
return ["default", JSON.parse(replaced)];
} catch (e) {
return ["default", replaced];
}
}
return [key, postProcess(value)];
})
) as T;
};

const create = (fileName: string, typeName: string) => {
const generator = createGenerator({
path: join(packageRoot, "index.ts"),
tsconfig: join(__dirname, "../tsconfig.json"),
type: "Schema",
extraTags: ["defaultValue"],
});
const schema = postProcess(generator.createSchema(typeName));
const filePath = join(__dirname, "..", "schemas", fileName);
writeFileSync(filePath, JSON.stringify(schema, null, 2));
};

create("schema.v1.json", "SchemaV1");
create("schema.v2.json", "Schema");
create("schema.json", "Schema");
4 changes: 3 additions & 1 deletion packages/turbo-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "@turbo/tsconfig/library.json",
"compilerOptions": {
"rootDir": "."
"rootDir": ".",
"module": "ESNext",
"lib": ["ESNext"],
}
}
Loading

0 comments on commit 765cbf3

Please sign in to comment.