Files
searcher/scripts/generate-swagger-docs.mjs
T
2026-06-06 00:49:07 +08:00

78 lines
2.3 KiB
JavaScript

#!/usr/bin/env node
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const rootDir = dirname(dirname(fileURLToPath(import.meta.url)));
const swaggerSource = join(rootDir, "swagger.yaml");
const publicSwaggerYaml = join(rootDir, "public", "swagger.yaml");
const publicSwaggerHtml = join(rootDir, "public", "swagger.html");
const docsSwaggerYaml = join(rootDir, "docs", "api", "swagger.yaml");
const docsSwaggerHtml = join(rootDir, "docs", "api", "swagger.html");
const swaggerYaml = readFileSync(swaggerSource, "utf8");
function createSwaggerHtml({ specUrl }) {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Wallora Searcher API Docs</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" />
<style>
body {
margin: 0;
background: #ffffff;
}
.swagger-ui .topbar {
display: none;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
<script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-standalone-preset.js"></script>
<script>
window.addEventListener("load", () => {
window.ui = SwaggerUIBundle({
url: ${JSON.stringify(specUrl)},
dom_id: "#swagger-ui",
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout"
});
});
</script>
</body>
</html>
`;
}
function writeGeneratedFile(path, content) {
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, content);
}
writeGeneratedFile(publicSwaggerYaml, swaggerYaml);
writeGeneratedFile(
publicSwaggerHtml,
createSwaggerHtml({ specUrl: "/swagger.yaml" }),
);
writeGeneratedFile(docsSwaggerYaml, swaggerYaml);
writeGeneratedFile(
docsSwaggerHtml,
createSwaggerHtml({ specUrl: "./swagger.yaml" }),
);
console.log("Generated Swagger docs:");
console.log(`- ${publicSwaggerHtml}`);
console.log(`- ${publicSwaggerYaml}`);
console.log(`- ${docsSwaggerHtml}`);
console.log(`- ${docsSwaggerYaml}`);