Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pipes default values through to json-schema #9039

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
dimitropoulos marked this conversation as resolved.
Show resolved Hide resolved
},
"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)
dimitropoulos marked this conversation as resolved.
Show resolved Hide resolved
*/
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
Loading