Browse Source

KERNEL-14076 feat:脚本可以自动处理依赖了

es6
Treecat 2 years ago
parent
commit
dd9db4d1fb
  1. 149
      es6.js
  2. 128
      es6.xtype.js

149
es6.js

@ -2,6 +2,7 @@ const fs = require("fs");
const path = require("path");
const prettier = require("prettier");
const { exec } = require("child_process");
const { search, initDepts } = require("./es6.xtype");
async function fix(path) {
new Promise(res => {
@ -11,7 +12,65 @@ async function fix(path) {
});
}
// 加载模块
const loader = {
G: { "@/core": { shortcut: true } },
async load(srcName, module) {
const G = loader.G;
const target = [
"isNull",
"toPix",
"isKey",
"isObject",
"map",
"extend",
"isFunction",
"isEmptyArray",
"isArray",
"Controller",
"createWidget",
"Events",
"emptyFn",
"nextTick",
"bind",
"i18nText",
"isNotNull",
"isString",
"isNumber",
"isEmpty",
"isEmptyString",
"any",
"deepContains",
"isNotEmptyString",
"each",
"contains",
"remove",
"createItems",
"makeArrayByArray",
];
console.log(module);
if (target.indexOf(module) >= 0) {
G["@/core"][module] = true;
return true;
}
const key = search(srcName, module);
if (key) {
if (!G[key]) {
G[key] = {};
}
G[key][module] = true;
}
return !!key;
},
};
async function handleFile(srcName) {
const G = loader.G;
const sourceCode = fs.readFileSync(srcName).toString();
const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode);
@ -24,6 +83,8 @@ async function handleFile(srcName) {
const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1];
// const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1];
const collection = {
@ -31,6 +92,7 @@ async function handleFile(srcName) {
attr: {},
};
// eslint-disable-next-line no-unused-vars
const BI = {
inherit(_, options) {
collection.methods = Object.keys(options)
@ -49,6 +111,7 @@ async function handleFile(srcName) {
},
};
// eslint-disable-next-line no-eval
eval(sourceCode);
let M = "";
@ -56,13 +119,7 @@ async function handleFile(srcName) {
let I = "";
let A = "";
const coreImport = {
shortcut: true,
};
if (superName === "Widget") {
coreImport.Widget = true;
}
loader.load(srcName, superName);
Object.keys(collection.attr).forEach(key => {
A = `${key} = ${JSON.stringify(collection.attr[key])};`;
@ -81,67 +138,52 @@ async function handleFile(srcName) {
f = f.replace(`BI.${clzName}.superclass`, "super");
// 换 super._defaultConfig
f = f.replace(
`super\._defaultConfig\.apply\(this\,\sarguments\)`,
/super\._defaultConfig\.apply\(this,\sarguments\)/g,
"super._defaultConfig(arguments)"
);
// 换 super.xxx.apply
f = f.replace(/super\.(.*?)\.apply\(this\,\sarguments\)/, a => {
const f = /super\.(.*?)\.apply\(this\,\sarguments\)/.exec(a);
f = f.replace(/super\.(.*?)\.apply\(this,\sarguments\)/, a => {
const f = /super\.(.*?)\.apply\(this,\sarguments\)/.exec(a);
return `super.${f[1]}(...arguments)`;
});
const target = [
"isNull",
"toPix",
"isKey",
"isObject",
"map",
"extend",
"isFunction",
"isEmptyArray",
"isArray",
"Controller",
clzName,
"createWidget",
"Events",
"emptyFn",
"nextTick",
"bind",
"i18nText",
"isNotNull",
"isString",
"isNumber",
"isEmpty",
"isEmptyString",
"any",
"deepContains",
"isNotEmptyString",
"each",
"contains",
"remove",
"createItems",
"makeArrayByArray",
];
// 尝试换掉所有的 BI.xxx. BI.xxx( BI.xxx[空白]
f = f.replace(/BI\.(.*?)(\.|\(|\s|,)/g, matchedSentence => {
const match = /BI\.(.*?)(\.|\(|\s|,)/.exec(matchedSentence);
const target = match[1];
const end = match[2];
// 尝试加载 target
const loadSuccess = loader.load(srcName, target);
if (loadSuccess) {
return target + end;
} else {
console.log(`加载 ${target}失败`);
target.forEach(t => {
const arr = f.split(`BI.${t}`);
// nodejs 低版本没有 replaceAll
if (arr.length > 1) {
if (t !== clzName) coreImport[t] = true;
f = arr.join(t);
return matchedSentence;
}
});
M += `${f}\n`;
});
Object.keys(coreImport).forEach(el => {
I += `${el},`;
Object.keys(G).forEach(moduleKey => {
if (moduleKey === path.basename(srcName).replace(/.js$/g, "")) {
return;
}
let i = "";
Object.keys(G[moduleKey]).forEach(key => {
i += `${key}, `;
});
I += `import {${i}} from '${moduleKey}'\n`;
});
const outputCode = `
import {${I}} from "@/core"
${I}
@shortcut()
export class ${clzName} extends ${superName} {
@ -187,4 +229,9 @@ async function traverse(srcName) {
}
const srcName = process.argv[2];
initDepts().then(() => {
traverse(srcName);
});

128
es6.xtype.js

@ -0,0 +1,128 @@
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];
}
}
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, clzName) {
if (!depts[clzName]) {
return "";
}
const dstName = path.basename(depts[clzName]).replace(/.js$/g, "");
const dstPath = path.normalize(depts[clzName]).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++;
}
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);
// 还有好多没有匹配完
// while (i < srcPath) {
// // exists(srcPath.slice(0, i).join())
// result += srcPath[i];
// 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;
Loading…
Cancel
Save