From dd9db4d1fbf93b042e9129c3d28c4554aee7afa8 Mon Sep 17 00:00:00 2001 From: Treecat Date: Wed, 11 Jan 2023 16:16:21 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-14076=20feat:=E8=84=9A=E6=9C=AC=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=87=AA=E5=8A=A8=E5=A4=84=E7=90=86=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es6.js | 153 +++++++++++++++++++++++++++++++++------------------ es6.xtype.js | 128 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 228 insertions(+), 53 deletions(-) create mode 100644 es6.xtype.js diff --git a/es6.js b/es6.js index 7b7734593..2f92decf7 100644 --- a/es6.js +++ b/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", - ]; - - 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); + // 尝试换掉所有的 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}失败`); + + 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]; -traverse(srcName); + +initDepts().then(() => { + traverse(srcName); +}); + + diff --git a/es6.xtype.js b/es6.xtype.js new file mode 100644 index 000000000..70b4d3db7 --- /dev/null +++ b/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; + + + + +