-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-swagger.js
54 lines (45 loc) · 1.48 KB
/
auto-swagger.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
const fs = require('fs');
const path = require('path');
// 遍历目录并获取所有路由文件
function getRouteFiles(dir) {
const files = fs.readdirSync(dir);
const routeFiles = files.filter(file => file.endsWith('.js'));
return routeFiles.map(file => path.join(dir, file));
}
// 为指定路由文件添加 Swagger 注释
function addSwaggerComments(filePath) {
const routeContent = fs.readFileSync(filePath, 'utf8');
const lines = routeContent.split('\n');
// 检查是否已经存在 Swagger 注释
const hasSwaggerComments = lines.some(line => line.trim().startsWith('* @swagger'));
// 如果不存在 Swagger 注释,则添加注释
if (!hasSwaggerComments) {
const swaggerComments = `
/**
* @swagger
* /example:
* get:
* description: Example route
* responses:
* 200:
* description: Success
*/
`;
// 插入 Swagger 注释到文件开头
lines.unshift(swaggerComments);
// 更新路由文件
fs.writeFileSync(filePath, lines.join('\n'), 'utf8');
console.log(`Swagger comments added to ${filePath}`);
} else {
console.log(`Swagger comments already exist in ${filePath}`);
}
}
// 获取所有路由文件并为每个文件添加 Swagger 注释
function autoAddSwaggerComments(routeDir) {
const routeFiles = getRouteFiles(routeDir);
routeFiles.forEach(addSwaggerComments);
}
// 指定路由文件目录
const routeDir = path.join(__dirname, 'routes');
// 自动添加 Swagger 注释
autoAddSwaggerComments(routeDir);