49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { promises } from "fs";
|
|
const { readdir, writeFile } = promises;
|
|
import { dirname, join } from "path";
|
|
import { createGenerator } from "ts-json-schema-generator";
|
|
|
|
async function genSchema(path: string, typename: string) {
|
|
const gen = createGenerator({
|
|
path: path,
|
|
type: typename,
|
|
tsconfig: "tsconfig.json",
|
|
});
|
|
const schema = gen.createSchema(typename);
|
|
if (schema.definitions != undefined) {
|
|
const definitions = schema.definitions;
|
|
const definition = definitions[typename];
|
|
if (typeof definition == "object") {
|
|
let property = definition.properties;
|
|
if (property) {
|
|
property["$schema"] = {
|
|
type: "string",
|
|
};
|
|
}
|
|
}
|
|
}
|
|
const text = JSON.stringify(schema);
|
|
await writeFile(join(dirname(path), `${typename}.schema.json`), text);
|
|
}
|
|
function capitalize(s: string) {
|
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
}
|
|
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 => {
|
|
const name = x.name;
|
|
const m = /(.+)\.ts/.exec(name);
|
|
if (m !== null) {
|
|
const typename = m[1];
|
|
return genSchema(join(path, typename), capitalize(typename));
|
|
}
|
|
});
|
|
await Promise.all(works);
|
|
const subdir = direntry.filter(x => x.isDirectory()).map(x => x.name);
|
|
for (const x of subdir) {
|
|
await setToALL(join(path, x));
|
|
}
|
|
}
|
|
setToALL("src");
|