-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f47ec90
commit 0200173
Showing
3 changed files
with
86 additions
and
32 deletions.
There are no files selected for viewing
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,79 @@ | ||
import * as tsj from "ts-json-schema-generator"; | ||
import path from "path"; | ||
import fs from "fs/promises"; | ||
|
||
const tsConfigPath = "tsconfig.json"; | ||
const targetDir = "src/lib/resources/schemas/"; | ||
const schemaList = [ | ||
{ | ||
"typeName": "ResourceFile", | ||
"srcPath": "src/lib/resources/resource-types.ts", | ||
"allowAdditionalProperties": false | ||
},{ | ||
"typeName": "SigninResponse", | ||
"srcPath": "src/lib/resources/network-types.ts", | ||
"allowAdditionalProperties": true | ||
}, { | ||
"typeName": "ResendMFADeviceResponse", | ||
"srcPath": "src/lib/resources/network-types.ts", | ||
"allowAdditionalProperties": true | ||
}, { | ||
"typeName": "ResendMFAPhoneResponse", | ||
"srcPath": "src/lib/resources/network-types.ts", | ||
"allowAdditionalProperties": true | ||
}, { | ||
"typeName": "TrustResponse", | ||
"srcPath": "src/lib/resources/network-types.ts", | ||
"allowAdditionalProperties": true | ||
}, { | ||
"typeName": "SetupResponse", | ||
"srcPath": "src/lib/resources/network-types.ts", | ||
"allowAdditionalProperties": true | ||
}, { | ||
"typeName": "PhotosSetupResponse", | ||
"srcPath": "src/lib/resources/network-types.ts", | ||
"allowAdditionalProperties": true | ||
}, | ||
] | ||
|
||
await fs.mkdir(targetDir, {recursive: true}); | ||
|
||
await Promise.all(schemaList.map(async (schemaConfig) => { | ||
const outputFilename = schemaConfig.typeName | ||
.replace(/(?<!^)[A-Z][a-z]/g, (str) => `-${str}`) // Match big to small transitions, except at the beginning of the string and prefix them with a dash | ||
.replace(/[a-z][A-Z]/g, (str) => `${str.charAt(0)}-${str.charAt(1)}`) // Match small to big transitions and insert a dash between them | ||
.toLowerCase() + ".json"; // Make everything lower case and append file extension | ||
|
||
const outputPath = path.join(targetDir, outputFilename); | ||
|
||
const srcStat = await fs.stat(schemaConfig.srcPath) | ||
|
||
try { | ||
const outputStat = await fs.stat(outputPath) | ||
if (srcStat.mtimeMs < outputStat.mtimeMs) { | ||
console.log(`Skipping ${schemaConfig.srcPath} as it is older than ${outputPath}`); | ||
return; | ||
} | ||
} catch (err) { | ||
console.log(`No existing schema for ${schemaConfig.typeName} found at ${outputPath}`); | ||
} | ||
|
||
const config: tsj.Config = { | ||
path: schemaConfig.srcPath, | ||
tsconfig: tsConfigPath, | ||
type: schemaConfig.typeName, | ||
additionalProperties: schemaConfig.allowAdditionalProperties, | ||
jsDoc: "extended", | ||
discriminatorType: "json-schema", | ||
skipTypeCheck: true, | ||
}; | ||
|
||
const schema = tsj.createGenerator(config).createSchema(config.type); | ||
|
||
await fs.writeFile(outputPath, | ||
JSON.stringify(schema, null, 2), | ||
{encoding: "utf-8"} | ||
); | ||
|
||
console.log(`Wrote schema for type ${schemaConfig.typeName} to ${outputPath}`); | ||
})); |
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