33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
import { execFileSync } from "node:child_process";
|
|
import { mkdirSync, writeFileSync } from "node:fs";
|
|
import { fileURLToPath } from "node:url";
|
|
import path from "node:path";
|
|
|
|
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const openApiUrl = process.env.OPENAPI_URL || "http://localhost:5090/openapi/v1.json";
|
|
const response = await fetch(openApiUrl);
|
|
if (!response.ok) {
|
|
throw new Error(`OpenAPI 下载失败:${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
const document = await response.json();
|
|
const generatedDir = path.join(projectRoot, ".generated");
|
|
const apiDir = path.join(projectRoot, "src", "api");
|
|
mkdirSync(generatedDir, { recursive: true });
|
|
mkdirSync(apiDir, { recursive: true });
|
|
|
|
const snapshotPath = path.join(generatedDir, "openapi.json");
|
|
writeFileSync(snapshotPath, `${JSON.stringify(document)}\n`);
|
|
|
|
const cli = path.join(
|
|
projectRoot,
|
|
"node_modules",
|
|
".bin",
|
|
process.platform === "win32" ? "openapi-typescript.cmd" : "openapi-typescript",
|
|
);
|
|
execFileSync(cli, [snapshotPath, "-o", path.join(apiDir, "schema.generated.d.ts")], {
|
|
stdio: "inherit",
|
|
});
|
|
|
|
console.log(`已生成前端 OpenAPI 类型,来源:${openApiUrl}`);
|