Browse Source

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

Merge in VISUAL/fineui from ~TREECAT/fineui:es6 to es6

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