54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
/**
|
||
* 由 Zod 定義產生 JSON Schema,給 Python 管線與其他語言的前端使用。
|
||
* 執行:pnpm schema
|
||
*/
|
||
import { mkdirSync, writeFileSync } from "node:fs";
|
||
import { dirname, join } from "node:path";
|
||
import { fileURLToPath } from "node:url";
|
||
import { z } from "zod";
|
||
import {
|
||
ApiError,
|
||
CellsFile,
|
||
CreateMatchRequest,
|
||
CreateMatchResponse,
|
||
DataPackManifest,
|
||
HistoryFile,
|
||
PeopleFile,
|
||
PlayerView,
|
||
ScenarioSummary,
|
||
SourceList,
|
||
SuggestResult,
|
||
TownsFile,
|
||
TurnResult,
|
||
TurnSubmission,
|
||
VillagesFile,
|
||
} from "../src/index.ts";
|
||
|
||
const outDir = join(dirname(fileURLToPath(import.meta.url)), "..", "schema");
|
||
mkdirSync(outDir, { recursive: true });
|
||
|
||
const schemas: Record<string, z.ZodType> = {
|
||
"manifest.schema.json": DataPackManifest,
|
||
"villages.schema.json": VillagesFile,
|
||
"towns.schema.json": TownsFile,
|
||
"cells.schema.json": CellsFile,
|
||
"history.schema.json": HistoryFile,
|
||
"sources.schema.json": SourceList,
|
||
"people.schema.json": PeopleFile,
|
||
"player-view.schema.json": PlayerView,
|
||
"turn-submission.schema.json": TurnSubmission,
|
||
"scenario-summary.schema.json": ScenarioSummary,
|
||
"create-match.schema.json": CreateMatchRequest,
|
||
"create-match-response.schema.json": CreateMatchResponse,
|
||
"turn-result.schema.json": TurnResult,
|
||
"suggest-result.schema.json": SuggestResult,
|
||
"api-error.schema.json": ApiError,
|
||
};
|
||
|
||
for (const [file, schema] of Object.entries(schemas)) {
|
||
const json = z.toJSONSchema(schema, { target: "draft-2020-12" });
|
||
writeFileSync(join(outDir, file), `${JSON.stringify(json, null, 2)}\n`);
|
||
}
|
||
|
||
process.stdout.write(`已產生 ${Object.keys(schemas).length} 份 JSON Schema 到 ${outDir}\n`);
|