Browse Source

KERNEL-14076 feat:es6脚本自动修复xtype

es6
Treecat 2 years ago
parent
commit
065e44cf0c
  1. 67
      es6.js
  2. 37
      es6.xtype.js

67
es6.js

@ -2,7 +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");
const { search, initDepts, depts } = require("./es6.xtype");
async function fix(path) {
new Promise(res => {
@ -42,28 +42,42 @@ const target = [
"remove",
"createItems",
"makeArrayByArray",
"VerticalAlign",
"transformItems",
];
// 加载模块
const loader = {
G: { "@/core": { shortcut: true } },
async load(srcName, module) {
load(srcName, module) {
const G = loader.G;
if (target.indexOf(module) >= 0) {
G["@/core"][module] = true;
return true;
}
if (module.startsWith('"bi.')) {
const key = search(srcName, module);
if (key) {
if (!G[key]) {
G[key] = {};
}
const clzName = depts[module].clzName;
G[key][clzName] = true;
}
const key = search(srcName, module);
if (key) {
if (!G[key]) {
G[key] = {};
return !!key;
} else {
const key = search(srcName, module);
if (key) {
if (!G[key]) {
G[key] = {};
}
G[key][module] = true;
}
G[key][module] = true;
}
return !!key;
return !!key;
}
},
};
@ -74,7 +88,24 @@ async function handleFile(srcName) {
const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode);
if (!result) {
console.log(`可能是已经es6过了 ${srcName}`);
console.log(`可能是已经es6过了 ${srcName}, 尝试替换 xtype`);
// 处理 xtype
// 尝试对 xtype 进行替换
const noXtypeCode = sourceCode.replace(/"bi\.(.*?)"/g, matchedSentence => {
const loadSuccess = loader.load(srcName, matchedSentence);
if (loadSuccess) {
const clzName = depts[matchedSentence].clzName;
return `${clzName}.xtype`;
} else {
console.log(`加载 ${matchedSentence}失败`);
return matchedSentence;
}
});
fs.writeFileSync(srcName, noXtypeCode);
return;
}
@ -157,7 +188,21 @@ async function handleFile(srcName) {
if (loadSuccess) {
return target + end;
} else {
console.log(`加载 ${target}失败`);
console.log(`BI.xxx 加载 ${target}失败`);
return matchedSentence;
}
});
// 尝试对 xtype 进行替换
f = f.replace(/"bi\.(.*?)"/g, matchedSentence => {
const loadSuccess = loader.load(srcName, matchedSentence);
if (loadSuccess) {
const clzName = depts[matchedSentence].clzName;
return `${clzName}.xtype`;
} else {
console.log(`加载 ${matchedSentence}失败`);
return matchedSentence;
}

37
es6.xtype.js

@ -10,10 +10,24 @@ async function handle(filename) {
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;
@ -43,13 +57,21 @@ async function initDepts() {
}
function search(src, clzName) {
function search(src, module) {
let clzName = module;
let clzPath = depts[module];
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("/");
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);
@ -60,8 +82,6 @@ function search(src, clzName) {
srcPath.shift();
srcPath.pop();
const findDstIndexPath = (dstArr, startIndex) => {
let i = startIndex;
@ -100,12 +120,6 @@ function search(src, clzName) {
// 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}`;
}
@ -124,6 +138,7 @@ function search(src, clzName) {
exports.initDepts = initDepts;
exports.search = search;
exports.depts = depts;

Loading…
Cancel
Save