|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Build Swagger UI HTML file |
| 5 | + * |
| 6 | + * Copies Swagger UI files and creates an HTML file that loads the OpenAPI specification. |
| 7 | + */ |
| 8 | + |
| 9 | +const fs = require('fs'); |
| 10 | +const path = require('path'); |
| 11 | + |
| 12 | +const specPath = path.join(__dirname, '..', 'ddi-rest.yaml'); |
| 13 | +const outputPath = path.join(__dirname, '..', 'dist', 'index.html'); |
| 14 | +const distDir = path.dirname(outputPath); |
| 15 | +const swaggerUiPath = path.join(__dirname, '..', 'node_modules', 'swagger-ui', 'dist'); |
| 16 | + |
| 17 | +// Ensure dist directory exists |
| 18 | +if (!fs.existsSync(distDir)) { |
| 19 | + fs.mkdirSync(distDir, { recursive: true }); |
| 20 | +} |
| 21 | + |
| 22 | +// Check if swagger-ui is installed |
| 23 | +if (!fs.existsSync(swaggerUiPath)) { |
| 24 | + console.error('❌ Error: swagger-ui not found. Please run: npm install'); |
| 25 | + process.exit(1); |
| 26 | +} |
| 27 | + |
| 28 | +// Copy YAML file to dist |
| 29 | +const distSpecPath = path.join(distDir, 'ddi-rest.yaml'); |
| 30 | +fs.copyFileSync(specPath, distSpecPath); |
| 31 | +console.log('📄 Copied ddi-rest.yaml'); |
| 32 | + |
| 33 | +// Copy Swagger UI files to dist |
| 34 | +const swaggerUiFiles = [ |
| 35 | + 'swagger-ui-bundle.js', |
| 36 | + 'swagger-ui-standalone-preset.js', |
| 37 | + 'swagger-ui.css' |
| 38 | +]; |
| 39 | + |
| 40 | +swaggerUiFiles.forEach(file => { |
| 41 | + const src = path.join(swaggerUiPath, file); |
| 42 | + const dest = path.join(distDir, file); |
| 43 | + if (fs.existsSync(src)) { |
| 44 | + fs.copyFileSync(src, dest); |
| 45 | + console.log(`📄 Copied ${file}`); |
| 46 | + } else { |
| 47 | + console.warn(`⚠️ Warning: ${file} not found at ${src}`); |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +// Create HTML file |
| 52 | +const html = `<!DOCTYPE html> |
| 53 | +<html lang="en"> |
| 54 | +<head> |
| 55 | + <meta charset="UTF-8"> |
| 56 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 57 | + <title>DDI RESTful API Documentation</title> |
| 58 | + <link rel="stylesheet" type="text/css" href="./swagger-ui.css" /> |
| 59 | + <style> |
| 60 | + html { |
| 61 | + box-sizing: border-box; |
| 62 | + overflow: -moz-scrollbars-vertical; |
| 63 | + overflow-y: scroll; |
| 64 | + } |
| 65 | + *, *:before, *:after { |
| 66 | + box-sizing: inherit; |
| 67 | + } |
| 68 | + body { |
| 69 | + margin: 0; |
| 70 | + background: #fafafa; |
| 71 | + } |
| 72 | + </style> |
| 73 | +</head> |
| 74 | +<body> |
| 75 | + <div id="swagger-ui"></div> |
| 76 | + <script src="./swagger-ui-bundle.js"></script> |
| 77 | + <script src="./swagger-ui-standalone-preset.js"></script> |
| 78 | + <script> |
| 79 | + window.onload = function() { |
| 80 | + SwaggerUIBundle({ |
| 81 | + url: "./ddi-rest.yaml", |
| 82 | + dom_id: '#swagger-ui', |
| 83 | + presets: [ |
| 84 | + SwaggerUIBundle.presets.apis, |
| 85 | + SwaggerUIStandalonePreset |
| 86 | + ], |
| 87 | + layout: "StandaloneLayout", |
| 88 | + deepLinking: true, |
| 89 | + showExtensions: true, |
| 90 | + showCommonExtensions: true |
| 91 | + }); |
| 92 | + }; |
| 93 | + </script> |
| 94 | +</body> |
| 95 | +</html>`; |
| 96 | + |
| 97 | +fs.writeFileSync(outputPath, html, 'utf8'); |
| 98 | +console.log(`✅ Swagger UI built successfully: ${outputPath}`); |
| 99 | + |
0 commit comments