Browse Source

KERNEL-14076 feat:递归xtype合并 import

es6
Treecat 2 years ago
parent
commit
27c6956b1d
  1. 118
      es6.js

118
es6.js

@ -3,6 +3,7 @@ const path = require("path");
const prettier = require("prettier");
const { exec } = require("child_process");
const { search, initDepts, depts } = require("./es6.xtype");
const _ = require("lodash");
// const XTYPE_ONLY = false;
// const THIS_REPLACE = false;
@ -20,7 +21,32 @@ function objHaveFunction(obj) {
});
}
async function fix(path) {
function parserImport(code) {
const reg = /import {([\s\S]*?)} from "(.*?)";/g;
const importMap = {};
let regResult = reg.exec(code);
while (regResult) {
importMap[regResult[2]] = regResult[1]
.split(",")
.map(el => el.trim()).filter(el => el);
regResult = reg.exec(code);
}
return importMap;
}
async function saveAndFixCode(path, code) {
let _code = code;
if (!code) {
_code = fs.readFileSync(path).toString();
}
const prettierCode = prettier.format(_code, {
tabWidth: 4,
parser: 'babel',
});
fs.writeFileSync(path, prettierCode);
new Promise(res => {
exec(`yarn eslint --fix ${path}`, () => {
res();
@ -104,18 +130,16 @@ const loader = {
};
async function handleFile(srcName) {
await saveAndFixCode(srcName);
// 全局状态回归
const G = loader.G = { "@/core": { shortcut: true } };
let G = loader.G = { };
const sourceCode = fs.readFileSync(srcName).toString();
const result = /BI\.(.*?)\s=\sBI\.inherit\(/.exec(sourceCode);
if (!result) {
console.log(`已经es6过,替换 xtype => ${srcName}`);
// 处理 xtype
// 尝试对 xtype 进行替换
const noXtypeCode = sourceCode.replace(/type:\s?"bi\.(.*?)"/g, v => {
const matchedSentence = v.replace(/type:\s?/, "");
const loadSuccess = loader.load(srcName, matchedSentence);
@ -126,16 +150,85 @@ async function handleFile(srcName) {
} else {
console.log(`xtype 替换失败 ${matchedSentence} `);
return matchedSentence;
return v;
}
});
// 识别 import
const importMap = parserImport(noXtypeCode);
// 合并原来的 import 到 G
_.forEach(importMap, (depts, module) => {
depts.forEach(dept => {
if (!G[module]) {
G[module] = {};
}
G[module][dept] = true;
});
});
fs.writeFileSync(srcName, noXtypeCode);
// 合并 core
const crossPackages = fs.readdirSync("src");
_.forEach(G, (depts, module) => {
crossPackages.forEach(crosspackage => {
if (module.indexOf(crosspackage) >= 0) {
if (!G[`@/${crosspackage}`]) {
G[`@/${crosspackage}`] = {};
}
Object.assign(G[`@/${crosspackage}`], depts);
}
});
});
const tmpG = {};
_.forEach(G, (depts, module)=> {
const flag = _.some(crossPackages, crosspackage => module.indexOf(crosspackage) >= 0 && !module.startsWith("@"))
if (!flag) {
tmpG[module] = depts;
}
});
G = tmpG;
const noImportCode = noXtypeCode.replace(/import {([\s\S]*?)} from "(.*?)";/g, "");
let I = "";
Object.keys(G).forEach(key => {
let moduleKey = key;
if (moduleKey === path.basename(srcName).replace(/.js$/g, "")) {
return;
}
let i = "";
Object.keys(G[moduleKey]).forEach(key => {
i += `${key}, `;
});
// 必须以 . 开头
const moduleInValid = /^[^@.]/.test(moduleKey);
if (moduleInValid) {
moduleKey = `./${moduleKey}`;
}
I += `import {${i}} from '${moduleKey}'\n`;
});
const code = `${I}\n${noImportCode}`;
await saveAndFixCode(srcName, code);
return;
}
G["@/core"] = { shortcut: true };
const clzName = result[1];
if (!clzName) {
console.log(`${srcName} 不需要 es6 化`);
return;
}
const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1];
// const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1];
@ -296,13 +389,7 @@ ${E}
${M}
}
`;
const prettierCode = prettier.format(outputCode, {
tabWidth: 4,
parser: 'babel',
});
fs.writeFileSync(srcName, prettierCode);
await fix(srcName);
await saveAndFixCode(srcName, outputCode);
return clzName;
}
@ -314,7 +401,8 @@ async function traverse(srcName) {
try {
return await handleFile(srcName);
} catch (error) {
console.log(`文件处理失败 ${srcName} \n${error}`);
console.log(`文件处理失败 ${srcName} \n`);
console.error(error);
return;
}

Loading…
Cancel
Save