|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < srcPath.length) {
|
|
|
|
let result = "";
|
|
|
|
const rawI = i;
|
|
|
|
|
|
|
|
// 回溯
|
|
|
|
for (let j = 0; j < srcPath.length - rawI; j++) {
|
|
|
|
result += "../";
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
// dstPath 也没有了
|
|
|
|
if (i < dstPath.length) {
|
|
|
|
return result + findDstIndexPath(dstPath, i);
|
|
|
|
} else if (i === dstPath.length) {
|
|
|
|
return `${result}${dstName}`;
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|