2025-01-24 18:51:49 +00:00
|
|
|
import path, { dirname } from "path";
|
|
|
|
|
import { mkdir, rm, writeFile } from "fs/promises";
|
|
|
|
|
import $RefParser from "@apidevtools/json-schema-ref-parser";
|
|
|
|
|
import { compileFromFile } from "json-schema-to-typescript";
|
|
|
|
|
import { glob } from "glob";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const BANNER_COMMENT = '// THIS IS A AUTO-GENERATED FILE. DO NOT MODIFY MANUALLY!\n';
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
const cwd = process.cwd();
|
|
|
|
|
const schemasBasePath = path.resolve(`${cwd}/../../schemas`);
|
2025-02-12 23:04:15 +00:00
|
|
|
const outDirRoot = path.resolve(`${cwd}/src`);
|
|
|
|
|
const schemas = await glob(`${schemasBasePath}/**/*.json`);
|
|
|
|
|
|
2025-01-24 18:51:49 +00:00
|
|
|
await Promise.all(schemas.map(async (schemaPath) => {
|
|
|
|
|
const name = path.parse(schemaPath).name;
|
|
|
|
|
const version = path.basename(path.dirname(schemaPath));
|
2025-02-12 23:04:15 +00:00
|
|
|
const outDir = path.join(outDirRoot, version);
|
2025-01-24 18:51:49 +00:00
|
|
|
|
|
|
|
|
await mkdir(outDir, { recursive: true });
|
|
|
|
|
|
|
|
|
|
// Generate schema
|
|
|
|
|
const schema = JSON.stringify(await $RefParser.bundle(schemaPath), null, 2);
|
|
|
|
|
await writeFile(
|
|
|
|
|
path.join(outDir, `${name}.schema.ts`),
|
|
|
|
|
BANNER_COMMENT +
|
|
|
|
|
'const schema = ' +
|
|
|
|
|
schema +
|
|
|
|
|
` as const;\nexport { schema as ${name}Schema };`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Generate types
|
|
|
|
|
const content = await compileFromFile(schemaPath, {
|
|
|
|
|
bannerComment: BANNER_COMMENT,
|
|
|
|
|
cwd: dirname(schemaPath),
|
|
|
|
|
ignoreMinAndMaxItems: true,
|
|
|
|
|
declareExternallyReferenced: true,
|
|
|
|
|
unreachableDefinitions: true,
|
|
|
|
|
});
|
|
|
|
|
await writeFile(
|
|
|
|
|
path.join(outDir, `${name}.type.ts`),
|
|
|
|
|
content,
|
|
|
|
|
)
|
|
|
|
|
}));
|
|
|
|
|
})();
|