-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (64 loc) · 2.04 KB
/
index.js
File metadata and controls
74 lines (64 loc) · 2.04 KB
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
const fs = require("fs");
const lodash = require("lodash");
// Read the content of api.json file
// Function to process a single Swagger file
function processSwaggerFile(filePath, baseUrl) {
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
try {
const tagSet = new Set();
const jsonData = JSON.parse(data);
const paths = Object.keys(jsonData.paths);
// Process each path and add tags
const versionRegex = /\/v\d+\/([^/]+)/;
paths.forEach((path) => {
const match = path.match(versionRegex);
let tag;
if (match && match[1]) {
tag = match[1];
} else {
tag = path.split("/")[1];
}
tagSet.add(tag);
if (jsonData.paths[path].tags) {
jsonData.paths[path].tags = [tag];
}
Object.keys(jsonData.paths[path]).forEach((key) => {
if (jsonData.paths[path][key] && jsonData.paths[path][key].tags) {
jsonData.paths[path][key].tags = [tag];
}
});
});
jsonData.tags = Array.from(tagSet, (item) => ({
name: item,
"x-displayName": lodash.startCase(item),
}));
jsonData.servers = [
{
url: baseUrl,
},
];
const result = JSON.stringify(jsonData, null, 2);
fs.writeFile(filePath, result, "utf8", (err) => {
if (err) {
console.error("Error writing to file:", err);
return;
}
console.log(`Processed and updated ${filePath}`);
});
} catch (error) {
console.error("Error parsing JSON:", error);
}
});
}
// List of Swagger files to process
const swaggerFiles = [
{ path: "static/apis/v1/api.json", baseUrl: "https://api.spectrocloud.com" },
// Add the path to your second Swagger file here
{ path: "static/apis/edge-v1/emc-api.json", baseUrl: "https://edge-host-ip:5080" },
];
// Process each Swagger file
swaggerFiles.forEach((file) => processSwaggerFile(file.path, file.baseUrl));