newCloud/scripts/add_api_names.js
ylj20011123 f651d3c91b update
2026-02-28 18:56:16 +08:00

106 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 从多个 Swagger API 文件提取接口中文名称,写入 api_mapping_merged.md
* 规则根据完整URL中的关键字匹配不同的API源文件
*/
const fs = require('fs');
const path = require('path');
// API 源配置每项包含文件名和URL中需要匹配的关键字
const API_SOURCES = [
{ file: 'allApi.text', urlKeyword: 'EShangApiMain' },
{ file: 'CommercialApiALLAPI.text', urlKeyword: 'CommercialApi' },
];
// 1. 解析所有 API 源文件,构建 { urlKeyword: { exact: {}, lower: {} } } 映射
function parseApiFile(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const lines = content.split('\n').map(l => l.trim()).filter(l => l);
const exact = {};
const lower = {};
for (let i = 0; i < lines.length - 1; i++) {
const match = lines[i].match(/^(get|post)\s+(\/\S+)$/i);
if (match) {
const apiPath = match[2];
const nextLine = lines[i + 1];
if (nextLine && !/^(get|post)\s+\//i.test(nextLine)) {
if (!exact[apiPath]) exact[apiPath] = nextLine;
const lp = apiPath.toLowerCase();
if (!lower[lp]) lower[lp] = nextLine;
}
}
}
return { exact, lower };
}
const sourceMaps = {};
for (const src of API_SOURCES) {
const filePath = path.join(__dirname, '..', 'config', src.file);
if (!fs.existsSync(filePath)) {
console.log(`⚠️ 文件不存在,跳过: ${src.file}`);
continue;
}
sourceMaps[src.urlKeyword] = parseApiFile(filePath);
console.log(`📖 ${src.file} 解析完成: ${Object.keys(sourceMaps[src.urlKeyword].exact).length} 个接口映射 (关键字: ${src.urlKeyword})`);
}
// 2. 处理 api_mapping_merged.md
const mdPath = path.join(__dirname, '..', 'config', 'api_mapping_merged.md');
const mdContent = fs.readFileSync(mdPath, 'utf-8');
const lines = mdContent.split('\n');
const newLines = [];
let addedCount = 0;
let skippedCount = 0;
let notFoundCount = 0;
for (let i = 0; i < lines.length; i++) {
newLines.push(lines[i]);
// 查找 - **接口**: `/xxx/xxx` 这一行
const interfaceMatch = lines[i].match(/^- \*\*接口\*\*:\s*`([^`]+)`/);
if (!interfaceMatch) continue;
const apiPath = interfaceMatch[1];
// 检查下一行是否已有接口名称(避免重复)
if (i + 1 < lines.length && lines[i + 1].includes('**接口名称**')) {
skippedCount++;
continue;
}
// 往后找完整URL行确定匹配哪个API源
let matchedKeyword = null;
for (let j = i + 1; j < Math.min(i + 5, lines.length); j++) {
if (lines[j].includes('**完整URL**')) {
for (const src of API_SOURCES) {
if (lines[j].includes(src.urlKeyword)) {
matchedKeyword = src.urlKeyword;
break;
}
}
break;
}
if (lines[j].match(/^####\s/)) break;
}
if (!matchedKeyword || !sourceMaps[matchedKeyword]) continue;
// 在对应映射中查找: 先精确匹配,再忽略大小写匹配
const maps = sourceMaps[matchedKeyword];
const chineseName = maps.exact[apiPath] || maps.lower[apiPath.toLowerCase()];
if (chineseName) {
newLines.push(`- **接口名称**: \`${chineseName}\``);
addedCount++;
} else {
notFoundCount++;
}
}
fs.writeFileSync(mdPath, newLines.join('\n'), 'utf-8');
console.log(`\n✅ 完成!`);
console.log(` 已添加接口名称: ${addedCount}`);
console.log(` 已跳过(已有名称): ${skippedCount}`);
console.log(` 未找到匹配: ${notFoundCount}`);