fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.8 KiB

const fs = require("fs");
const path = require("path");
const depts = {};
async function handle(filename) {
// 找clzName
const code = fs.readFileSync(filename);
const inheritRegResult = /BI\.(.*?)\s=\sBI\.inherit\(/.exec(code);
let clzName;
if (inheritRegResult) {
clzName = inheritRegResult[1];
// 把
} else {
const clzRegResult = /export\sclass\s(.*?)\sextend/.exec(code);
if (clzRegResult) {
clzName = clzRegResult[1];
} else {
return;
}
const xtypeResult = /static xtype = (.*?)(;|\s)/.exec(code);
// 找一下 xtype
if (xtypeResult) {
depts[xtypeResult[1]] = {
clzName,
clzPath: filename,
};
} else {
// console.log(`${filename} 没有 xtype`);
}
}
depts[clzName] = filename;
}
function isExist(filePath) {
return fs.existsSync(filePath);
}
async function bfs(filename) {
const stat = fs.statSync(filename);
const isDir = stat.isDirectory();
if (isDir) {
const files = fs.readdirSync(filename);
for (let i = 0; i < files.length; i++) {
const file = files[i];
await bfs(path.resolve(filename, file));
}
} else {
await handle(filename);
}
}
async function initDepts() {
// dfs 构建依赖关系
await bfs(path.resolve("src"));
}
function search(src, module) {
let clzName = module;
let clzPath = depts[module];
if (!depts[clzName]) {
return "";
}
if (clzName.indexOf("\"") >= 0) {
clzName = depts[module].clzName;
clzPath = depts[module].clzPath;
}
const dstName = path.basename(clzPath).replace(/.js$/g, "");
const dstPath = path.normalize(clzPath).split("src")[1].split("\\").join("/").split("/");
const srcPath = path.normalize(src).split("src")[1].split("\\").join("/").split("/");
// console.log("src", src);
// console.log("dst", depts[clzName]);
dstPath.shift();
dstPath.pop();
srcPath.shift();
srcPath.pop();
const findDstIndexPath = (dstArr, startIndex) => {
let i = startIndex;
while (!isExist(path.resolve("src", dstArr.slice(0, i + 1).join("/"), "index.js")) && i < dstArr.length) {
i++;
}
if (i < dstArr.length) {
return dstArr.slice(startIndex, i + 1).join("/");
} else {
return `${dstArr.slice(startIndex).join("/")}/${dstName}`;
}
};
// 不同包
if (dstPath[0] !== srcPath[0]) {
return `@/${dstPath[0]}`;
}
// 同包
let i = 0;
while (dstPath[i] === srcPath[i] && i < dstPath.length && i < srcPath.length) {
i++;
}
// i 代表同名的位置
i--;
// 没有匹配完
if (i < srcPath.length) {
let result = "";
// 回溯,向上找,回到目录 i
for (let j = srcPath.length - 1; j > i; j--) {
result += "../";
}
// 匹配过的下一个位置
i++;
if (i >= dstPath.length) {
// 越界
return `${result}${dstName}`;
} else {
// 看看这个目录下有没有 index
return result + findDstIndexPath(dstPath, i);
}
} else if (i === srcPath.length) {
if (i === dstPath.length) {
return dstName;
} else if (i < dstPath.length) {
return findDstIndexPath(dstPath, i);
}
}
}
// search(process.argv[2], "Text").then(res => {
// console.log(res);
// });
exports.initDepts = initDepts;
exports.search = search;
exports.depts = depts;