2021-11-07 17:17:48 +01:00
|
|
|
import ScriptUtils from "./ScriptUtils"
|
|
|
|
import { readFileSync, writeFileSync } from "fs"
|
|
|
|
|
2022-05-17 01:46:59 +02:00
|
|
|
/**
|
|
|
|
* Extracts the data from the scheme file and writes them in a flatter structure
|
|
|
|
*/
|
2021-11-07 17:52:05 +01:00
|
|
|
|
2022-07-18 02:25:05 +02:00
|
|
|
export type JsonSchemaType =
|
|
|
|
| string
|
|
|
|
| { $ref: string; description: string }
|
|
|
|
| { type: string }
|
|
|
|
| JsonSchemaType[]
|
|
|
|
|
|
|
|
export interface JsonSchema {
|
2022-01-31 00:39:54 +01:00
|
|
|
description?: string
|
2022-07-18 02:25:05 +02:00
|
|
|
type?: JsonSchemaType
|
2022-01-31 00:39:54 +01:00
|
|
|
properties?: any
|
2022-02-28 17:17:38 +01:00
|
|
|
items?: JsonSchema
|
2022-05-17 01:46:59 +02:00
|
|
|
allOf?: JsonSchema[]
|
2022-01-31 00:39:54 +01:00
|
|
|
anyOf: JsonSchema[]
|
|
|
|
enum: JsonSchema[]
|
|
|
|
$ref: string
|
|
|
|
}
|
|
|
|
|
|
|
|
function WalkScheme<T>(
|
|
|
|
onEach: (schemePart: JsonSchema) => T,
|
|
|
|
scheme: JsonSchema,
|
|
|
|
fullScheme: JsonSchema & { definitions?: any } = undefined,
|
|
|
|
path: string[] = [],
|
|
|
|
isHandlingReference = []
|
|
|
|
): { path: string[]; t: T }[] {
|
|
|
|
const results: { path: string[]; t: T }[] = []
|
|
|
|
if (scheme === undefined) {
|
|
|
|
return []
|
|
|
|
}
|
2022-05-17 01:46:59 +02:00
|
|
|
|
2022-01-31 00:39:54 +01:00
|
|
|
if (scheme["$ref"] !== undefined) {
|
|
|
|
const ref = scheme["$ref"]
|
|
|
|
const prefix = "#/definitions/"
|
|
|
|
if (!ref.startsWith(prefix)) {
|
|
|
|
throw "References is not relative!"
|
|
|
|
}
|
|
|
|
const definitionName = ref.substr(prefix.length)
|
|
|
|
if (isHandlingReference.indexOf(definitionName) >= 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const loadedScheme = fullScheme.definitions[definitionName]
|
2022-02-28 17:17:38 +01:00
|
|
|
return WalkScheme(onEach, loadedScheme, fullScheme, path, [
|
|
|
|
...isHandlingReference,
|
|
|
|
definitionName,
|
|
|
|
])
|
2022-01-31 00:39:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fullScheme = fullScheme ?? scheme
|
|
|
|
var t = onEach(scheme)
|
2022-02-09 22:34:02 +01:00
|
|
|
if (t !== undefined) {
|
|
|
|
results.push({
|
2022-02-28 17:17:38 +01:00
|
|
|
path,
|
2022-02-09 22:34:02 +01:00
|
|
|
t,
|
|
|
|
})
|
|
|
|
}
|
2022-02-28 17:17:38 +01:00
|
|
|
|
|
|
|
function walk(v: JsonSchema) {
|
2022-01-31 00:39:54 +01:00
|
|
|
if (v === undefined) {
|
|
|
|
return
|
|
|
|
}
|
2022-02-28 17:17:38 +01:00
|
|
|
results.push(...WalkScheme(onEach, v, fullScheme, path, isHandlingReference))
|
2022-01-31 00:39:54 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 17:17:38 +01:00
|
|
|
function walkEach(scheme: JsonSchema[]) {
|
2022-01-31 00:39:54 +01:00
|
|
|
if (scheme === undefined) {
|
|
|
|
return
|
|
|
|
}
|
2022-05-17 01:46:59 +02:00
|
|
|
|
2022-02-28 17:17:38 +01:00
|
|
|
scheme.forEach((v) => walk(v))
|
2022-01-31 00:39:54 +01:00
|
|
|
}
|
|
|
|
|
2022-02-09 22:34:02 +01:00
|
|
|
{
|
2022-02-28 17:17:38 +01:00
|
|
|
walkEach(scheme.enum)
|
|
|
|
walkEach(scheme.anyOf)
|
2022-05-17 01:46:59 +02:00
|
|
|
walkEach(scheme.allOf)
|
|
|
|
|
2022-02-28 17:17:38 +01:00
|
|
|
if (Array.isArray(scheme.items)) {
|
|
|
|
walkEach(<any>scheme.items)
|
|
|
|
} else {
|
|
|
|
walk(<any>scheme.items)
|
2022-01-31 00:39:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const key in scheme.properties) {
|
|
|
|
const prop = scheme.properties[key]
|
2022-02-28 17:17:38 +01:00
|
|
|
results.push(
|
|
|
|
...WalkScheme(onEach, prop, fullScheme, [...path, key], isHandlingReference)
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-01-31 00:39:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2022-02-09 22:34:02 +01:00
|
|
|
function extractMeta(typename: string, path: string) {
|
|
|
|
const themeSchema = JSON.parse(
|
|
|
|
readFileSync("./Docs/Schemas/" + typename + ".schema.json", "UTF-8")
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-02-09 22:34:02 +01:00
|
|
|
const withTypes = WalkScheme((schemePart) => {
|
|
|
|
if (schemePart.description === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const typeHint = schemePart.description
|
|
|
|
.split("\n")
|
|
|
|
.find((line) => line.trim().toLocaleLowerCase().startsWith("type:"))
|
|
|
|
?.substr("type:".length)
|
|
|
|
?.trim()
|
2022-02-28 17:17:38 +01:00
|
|
|
const type = schemePart.items?.anyOf ?? schemePart.type ?? schemePart.anyOf
|
2022-05-17 01:46:59 +02:00
|
|
|
return { typeHint, type, description: schemePart.description }
|
2022-02-09 22:34:02 +01:00
|
|
|
}, themeSchema)
|
|
|
|
|
2022-02-28 17:17:38 +01:00
|
|
|
const paths = withTypes.map(({ path, t }) => ({ path, ...t }))
|
|
|
|
writeFileSync("./assets/" + path + ".json", JSON.stringify(paths, null, " "))
|
|
|
|
console.log("Written meta to ./assets/" + path)
|
2022-02-09 22:34:02 +01:00
|
|
|
}
|
|
|
|
|
2021-11-07 17:52:05 +01:00
|
|
|
function main() {
|
|
|
|
const allSchemas = ScriptUtils.readDirRecSync("./Docs/Schemas").filter((pth) =>
|
|
|
|
pth.endsWith("JSC.ts")
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2021-11-07 17:52:05 +01:00
|
|
|
for (const path of allSchemas) {
|
|
|
|
const dir = path.substring(0, path.lastIndexOf("/"))
|
|
|
|
const name = path.substring(path.lastIndexOf("/"), path.length - "JSC.ts".length)
|
|
|
|
let content = readFileSync(path, "UTF-8")
|
|
|
|
content = content.substring("export default ".length)
|
|
|
|
let parsed = JSON.parse(content)
|
|
|
|
parsed["additionalProperties"] = false
|
|
|
|
|
|
|
|
for (const key in parsed.definitions) {
|
|
|
|
const def = parsed.definitions[key]
|
|
|
|
if (def.type === "object") {
|
|
|
|
def["additionalProperties"] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeFileSync(dir + "/" + name + ".schema.json", JSON.stringify(parsed, null, " "), "UTF8")
|
|
|
|
}
|
2022-01-31 00:39:54 +01:00
|
|
|
|
2022-02-09 22:34:02 +01:00
|
|
|
extractMeta("LayoutConfigJson", "layoutconfigmeta")
|
|
|
|
extractMeta("TagRenderingConfigJson", "tagrenderingconfigmeta")
|
2022-02-28 17:17:38 +01:00
|
|
|
extractMeta("QuestionableTagRenderingConfigJson", "questionabletagrenderingconfigmeta")
|
2021-11-07 17:52:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main()
|