-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (96 loc) · 2.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const path = require("path");
const fs = require("fs");
const root = path.join(__dirname, "/");
function readSync(dirName) {
return fs.readdirSync(path.join(root, dirName));
}
function filter(files, extensions) {
return files.filter((el) => extensions.includes(path.extname(el)));
}
function getAllFilesWithExtensions(dirName, extensions) {
const files = readSync(dirName);
const filteredList = filter(files, extensions).map(
(file) => `${dirName}${file}`
);
const directories = files.filter(
(el) => path.extname(el) === "" && !el.startsWith(".")
);
if (directories.length) {
return directories.reduce((totalArray, currentDir) => {
return [
...totalArray,
...getAllFilesWithExtensions(`${dirName}${currentDir}/`, extensions),
];
}, filteredList);
}
return filteredList;
}
const allSwaggerFiles = getAllFilesWithExtensions("/", [".yaml", ".yml"]);
const formattedSwaggerFiles = allSwaggerFiles.map(
(path) =>
`{ url: location.protocol + '//' + location.host + "${path}", name: "${path}" }`
);
const htmlContent = `
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.0.0-beta.0/swagger-ui.css" />
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}
*,
*:before,
*:after
{
box-sizing: inherit;
}
body
{
margin:0;
background: #fafafa;
}
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.0.0-beta.0/swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.0.0-beta.0/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
"dom_id": "#swagger-ui",
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
validatorUrl: "https://validator.swagger.io/validator",
urls: [
${formattedSwaggerFiles.join(",")}
],
"urls.primaryName": "${allSwaggerFiles[0]}",
})
// End Swagger UI call region
window.ui = ui;
};
</script>
</body>
</html>
`;
fs.writeFile("index.html", htmlContent, function (err) {
if (err) return console.log(err);
});