-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pipes default values through to json-schema (#9039)
- Loading branch information
1 parent
06a26de
commit 765cbf3
Showing
8 changed files
with
499 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
} | ||
} |
Oops, something went wrong.