ionian/gen_conf_schema.ts

49 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-06-01 14:18:53 +09:00
import { promises } from "fs";
2021-01-15 18:43:36 +09:00
const { readdir, writeFile } = promises;
2023-06-01 14:18:53 +09:00
import { dirname, join } from "path";
import { createGenerator } from "ts-json-schema-generator";
2021-01-15 18:43:36 +09:00
2023-06-01 14:18:53 +09:00
async function genSchema(path: string, typename: string) {
2021-01-15 18:43:36 +09:00
const gen = createGenerator({
2023-06-01 14:18:53 +09:00
path: path,
type: typename,
tsconfig: "tsconfig.json",
2021-01-15 18:43:36 +09:00
});
2023-06-01 14:18:53 +09:00
const schema = gen.createSchema(typename);
if (schema.definitions != undefined) {
2021-01-15 18:43:36 +09:00
const definitions = schema.definitions;
const definition = definitions[typename];
2023-06-01 14:18:53 +09:00
if (typeof definition == "object") {
2021-01-15 18:43:36 +09:00
let property = definition.properties;
2023-06-01 14:18:53 +09:00
if (property) {
property["$schema"] = {
type: "string",
2021-01-15 18:43:36 +09:00
};
}
}
}
const text = JSON.stringify(schema);
2023-06-01 14:18:53 +09:00
await writeFile(join(dirname(path), `${typename}.schema.json`), text);
2021-01-15 18:43:36 +09:00
}
2023-06-01 14:18:53 +09:00
function capitalize(s: string) {
2021-01-15 18:43:36 +09:00
return s.charAt(0).toUpperCase() + s.slice(1);
}
2023-06-01 14:18:53 +09:00
async function setToALL(path: string) {
console.log(`scan ${path}`);
const direntry = await readdir(path, { withFileTypes: true });
const works = direntry.filter(x => x.isFile() && x.name.endsWith("Config.ts")).map(x => {
2021-01-15 18:43:36 +09:00
const name = x.name;
const m = /(.+)\.ts/.exec(name);
2023-06-01 14:18:53 +09:00
if (m !== null) {
2021-01-15 18:43:36 +09:00
const typename = m[1];
2023-06-01 14:18:53 +09:00
return genSchema(join(path, typename), capitalize(typename));
2021-01-15 18:43:36 +09:00
}
2023-06-01 14:18:53 +09:00
});
2021-01-15 18:43:36 +09:00
await Promise.all(works);
2023-06-01 14:18:53 +09:00
const subdir = direntry.filter(x => x.isDirectory()).map(x => x.name);
for (const x of subdir) {
await setToALL(join(path, x));
2021-01-15 18:43:36 +09:00
}
}
2023-06-01 14:18:53 +09:00
setToALL("src");