feima-shortcuts 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/generateApi/index.js +70 -3
package/package.json
CHANGED
package/src/generateApi/index.js
CHANGED
|
@@ -5,6 +5,62 @@ const path = require("path");
|
|
|
5
5
|
|
|
6
6
|
const getTemplate = require("./orval.config.ts.template");
|
|
7
7
|
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 交换 Swagger JSON 中 tags 的 name 和 description,
|
|
11
|
+
* 并同步更新 paths 下引用的 tag。
|
|
12
|
+
*
|
|
13
|
+
* @param {object} swaggerJson Swagger JSON 对象
|
|
14
|
+
* @returns {object} 修改后的 Swagger JSON
|
|
15
|
+
*/
|
|
16
|
+
function swapSwaggerTags(swaggerJson) {
|
|
17
|
+
if (!swaggerJson || !Array.isArray(swaggerJson.tags)) {
|
|
18
|
+
throw new Error("无效的 Swagger JSON:缺少 tags 数组");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 建立映射表:旧 name => 新 name(原来的 description)
|
|
22
|
+
const tagMap = {};
|
|
23
|
+
|
|
24
|
+
swaggerJson.tags = swaggerJson.tags.map(tag => {
|
|
25
|
+
const oldName = tag.name;
|
|
26
|
+
const newName = tag.description || oldName; // 防止 description 为空
|
|
27
|
+
const newDesc = oldName;
|
|
28
|
+
|
|
29
|
+
tagMap[oldName] = newName;
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
...tag,
|
|
33
|
+
name: newName,
|
|
34
|
+
description: newDesc,
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// 更新 paths 下的 tags
|
|
39
|
+
if (swaggerJson.paths) {
|
|
40
|
+
for (const pathKey of Object.keys(swaggerJson.paths)) {
|
|
41
|
+
const pathObj = swaggerJson.paths[pathKey];
|
|
42
|
+
for (const methodKey of Object.keys(pathObj)) {
|
|
43
|
+
const method = pathObj[methodKey];
|
|
44
|
+
if (Array.isArray(method.tags)) {
|
|
45
|
+
method.tags = method.tags.map(tag => tagMap[tag] || tag);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return swaggerJson;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const handleSwaggerJson = async (url, filePath) => {
|
|
55
|
+
const res = await fetch(url);
|
|
56
|
+
if (!res.ok) throw new Error(`下载失败:${res.status}`);
|
|
57
|
+
console.log('🔗 下载成功');
|
|
58
|
+
const data = await res.json();
|
|
59
|
+
const swaggerJson = swapSwaggerTags(data);
|
|
60
|
+
fs.writeFileSync(filePath, JSON.stringify(swaggerJson, null, 2));
|
|
61
|
+
console.log('📝 已写入文件', filePath);
|
|
62
|
+
};
|
|
63
|
+
|
|
8
64
|
exports.run = () => {
|
|
9
65
|
const program = new Command();
|
|
10
66
|
|
|
@@ -34,13 +90,16 @@ exports.run = () => {
|
|
|
34
90
|
console.error("请输入基础URL");
|
|
35
91
|
return;
|
|
36
92
|
}
|
|
93
|
+
|
|
94
|
+
const tmpSwaggerFileName = "tmp-swagger.json";
|
|
95
|
+
await handleSwaggerJson(input, path.resolve(process.cwd(), tmpSwaggerFileName));
|
|
37
96
|
|
|
38
97
|
// 确保 ./orvalConfig.ts 使用内置的 requestStr(放在项目根目录)
|
|
39
98
|
const orvalConfig = path.resolve(process.cwd(), "orval.config.ts");
|
|
40
99
|
try {
|
|
41
100
|
fs.writeFileSync(
|
|
42
101
|
orvalConfig,
|
|
43
|
-
getTemplate(
|
|
102
|
+
getTemplate(`./${tmpSwaggerFileName}`, output, baseUrl),
|
|
44
103
|
"utf8"
|
|
45
104
|
);
|
|
46
105
|
console.log("📝 已写入 ./orvalConfig.ts");
|
|
@@ -58,7 +117,7 @@ exports.run = () => {
|
|
|
58
117
|
shell: false,
|
|
59
118
|
});
|
|
60
119
|
|
|
61
|
-
const
|
|
120
|
+
const cleanupTempFiles = () => {
|
|
62
121
|
try {
|
|
63
122
|
if (fs.existsSync(orvalConfig)) {
|
|
64
123
|
fs.unlinkSync(orvalConfig);
|
|
@@ -67,10 +126,18 @@ exports.run = () => {
|
|
|
67
126
|
} catch (e) {
|
|
68
127
|
console.warn("⚠️ 清理临时文件失败:", e.message);
|
|
69
128
|
}
|
|
129
|
+
try {
|
|
130
|
+
if (fs.existsSync(tmpSwaggerFileName)) {
|
|
131
|
+
fs.unlinkSync(tmpSwaggerFileName);
|
|
132
|
+
console.log("🧹 已清理临时文件 ./tmp-swagger.json");
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.warn("⚠️ 清理临时文件失败:", error.message);
|
|
136
|
+
}
|
|
70
137
|
};
|
|
71
138
|
|
|
72
139
|
child.on("close", (code) => {
|
|
73
|
-
|
|
140
|
+
cleanupTempFiles();
|
|
74
141
|
if (code === 0) {
|
|
75
142
|
console.log(`✅ OpenAPI 代码已生成到 ${output}`);
|
|
76
143
|
process.exit(0);
|