Skip to content

Instantly share code, notes, and snippets.

@Blithe-Chiang
Last active October 9, 2024 06:39
Show Gist options
  • Save Blithe-Chiang/4cdeae04f73a215e943d12d8b4150e87 to your computer and use it in GitHub Desktop.
Save Blithe-Chiang/4cdeae04f73a215e943d12d8b4150e87 to your computer and use it in GitHub Desktop.
demo
import fs from "node:fs";
import path from "node:path";
import { parse } from "@babel/parser";
import traverse from "@babel/traverse";
import { csvWrite } from "./utils.js";
// 解析文件内容为 AST
const parseFile = (code) => {
return parse(code, {
sourceType: "module",
plugins: ["typescript"],
});
};
// 提取注释
const extractComments = (path) => {
let description = "无描述";
if (path.node && path.node.leadingComments) {
const comment = path.node.leadingComments[0];
if (
comment &&
(comment.type === "CommentBlock" || comment.type === "CommentLine")
) {
description = comment.value.trim().replace(/^\*\s*/, "");
}
}
return description;
};
// 提取 API 调用及描述
const extractApiCalls = (ast) => {
const apiCalls = [];
traverse.default(ast, {
CallExpression(path) {
const callee = path.node.callee;
const functionNames = ["getTpl", "postTpl", "listTpl"];
// 检查是否是 getTpl, postTpl, listTpl 函数调用
if (callee.type === "Identifier" && functionNames.includes(callee.name)) {
const args = path.node.arguments;
// 确保有至少一个参数
if (args.length > 0) {
// 处理路径参数
let fullPath = "";
if (args[0].type === "StringLiteral") {
fullPath = args[0].value;
} else if (
args[0].type === "BinaryExpression" &&
args[0].operator === "+"
) {
// 处理字符串拼接
const left = args[0].left;
const right = args[0].right;
if (left.type === "StringLiteral") {
fullPath += left.value;
}
if (right.type === "StringLiteral") {
fullPath += right.value;
}
}
// 获取描述:前面最近的注释
let description = "无描述";
let currentPath = path.getFunctionParent();
while (currentPath) {
if (currentPath.node && currentPath.node.leadingComments) {
description = extractComments(currentPath);
break;
}
currentPath = currentPath.parentPath;
}
const pathParts = fullPath.replace(/^\//, "").split("/");
const [gatewayType, ...rest] = pathParts;
const apiPath = "/" + rest.join("/");
apiCalls.push([description, gatewayType, apiPath]);
}
}
},
});
return apiCalls;
};
const gatewayFile = process.argv[2] || "gateway.ts";
// 读取文件内容并解析
fs.readFile(path.resolve(gatewayFile), "utf8", (err, code) => {
if (err) {
console.error("读取文件时出错:", err);
return;
}
try {
const ast = parseFile(code);
const apiCalls = extractApiCalls(ast);
// 生成 CSV 输出文件
const outputFileName = `output_${path.basename(
gatewayFile,
".ts",
)}_${Date.now()}.csv`;
csvWrite(
["名称", "网关类型", "网关地址"],
apiCalls,
path.resolve(outputFileName),
);
console.log("CSV 文件已生成:", outputFileName);
} catch (error) {
console.error("处理文件时出错:", error);
}
});
import { getTpl, postTpl } from '../utils/request/tpl';
const api = {
// 转运仓签收
sign: (params = {}) =>
postTpl('/jy/ware/v1/packages/sign', params, {
// noLoading: true,
msg: false,
}),
// 包裹查询-详细版
searchdetail: (params = {}) =>
getTpl('/jy/ware/v1/packages/search/detail', params, { noLoading: true }),
// 包裹查询-简单信息
searchshort: (params = {}) =>
getTpl('/jy/ware/v1/packages/search/short', params, { noLoading: true }),
//转运仓入库
packagesenter: (params = {}) =>
postTpl('/jy/ware/v1/packages/enter', params, { noLoading: true }),
// 出库单查询
shipmentsSearch: (params = {}) => {
return getTpl('/jy/ware/v1/shipments/search', params, {
msg: false,
codeName: 'resultCode',
})
},
//打包
pack: (params = {}) => postTpl('/jy/ware/v1/shipments/pack', params),
//出库
out: (params = {}) => postTpl('/jy/ware/v1/shipments/out', params),
// 生成标签
package_create: (params = {}) =>
postTpl('/jy/ware/v1/packages/create', params, { noLoading: true }),
};
export default api;
{
"name": "tool",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"license": "MIT",
"dependencies": {
"@babel/core": "^7.25.2",
"@babel/parser": "^7.25.3",
"@babel/traverse": "^7.25.3",
"axios": "^1.7.2",
"csv-parser": "^3.0.0",
"csv-writer": "^1.6.0",
"lodash": "^4.17.21",
"read-excel-file": "^5.8.3",
"sheetjs-style": "^0.15.8"
},
"devDependencies": {
"@types/lodash": "^4.17.7",
"@types/node": "^22.1.0",
"prettier": "^3.3.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment