|
|
@ -3,6 +3,7 @@ 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, depts } = require("./es6.xtype"); |
|
|
|
const { search, initDepts, depts } = require("./es6.xtype"); |
|
|
|
|
|
|
|
const _ = require("lodash"); |
|
|
|
|
|
|
|
|
|
|
|
// const XTYPE_ONLY = false;
|
|
|
|
// const XTYPE_ONLY = false;
|
|
|
|
// const THIS_REPLACE = 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 => { |
|
|
|
new Promise(res => { |
|
|
|
exec(`yarn eslint --fix ${path}`, () => { |
|
|
|
exec(`yarn eslint --fix ${path}`, () => { |
|
|
|
res(); |
|
|
|
res(); |
|
|
@ -104,18 +130,16 @@ const loader = { |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
async function handleFile(srcName) { |
|
|
|
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 sourceCode = fs.readFileSync(srcName).toString(); |
|
|
|
|
|
|
|
|
|
|
|
const result = /BI\.(.*?)\s=\sBI\.inherit\(/.exec(sourceCode); |
|
|
|
const result = /BI\.(.*?)\s=\sBI\.inherit\(/.exec(sourceCode); |
|
|
|
if (!result) { |
|
|
|
if (!result) { |
|
|
|
console.log(`已经es6过,替换 xtype => ${srcName}`); |
|
|
|
console.log(`已经es6过,替换 xtype => ${srcName}`); |
|
|
|
|
|
|
|
|
|
|
|
// 处理 xtype
|
|
|
|
// 处理 xtype
|
|
|
|
|
|
|
|
|
|
|
|
// 尝试对 xtype 进行替换
|
|
|
|
|
|
|
|
const noXtypeCode = sourceCode.replace(/type:\s?"bi\.(.*?)"/g, v => { |
|
|
|
const noXtypeCode = sourceCode.replace(/type:\s?"bi\.(.*?)"/g, v => { |
|
|
|
const matchedSentence = v.replace(/type:\s?/, ""); |
|
|
|
const matchedSentence = v.replace(/type:\s?/, ""); |
|
|
|
const loadSuccess = loader.load(srcName, matchedSentence); |
|
|
|
const loadSuccess = loader.load(srcName, matchedSentence); |
|
|
@ -126,16 +150,85 @@ async function handleFile(srcName) { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
console.log(`xtype 替换失败 ${matchedSentence} `); |
|
|
|
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; |
|
|
|
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]; |
|
|
|
const clzName = result[1]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!clzName) { |
|
|
|
|
|
|
|
console.log(`${srcName} 不需要 es6 化`); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
@ -296,13 +389,7 @@ ${E} |
|
|
|
${M} |
|
|
|
${M} |
|
|
|
} |
|
|
|
} |
|
|
|
`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
await saveAndFixCode(srcName, outputCode); |
|
|
|
const prettierCode = prettier.format(outputCode, { |
|
|
|
|
|
|
|
tabWidth: 4, |
|
|
|
|
|
|
|
parser: 'babel', |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
fs.writeFileSync(srcName, prettierCode); |
|
|
|
|
|
|
|
await fix(srcName); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return clzName; |
|
|
|
return clzName; |
|
|
|
} |
|
|
|
} |
|
|
@ -314,7 +401,8 @@ async function traverse(srcName) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
return await handleFile(srcName); |
|
|
|
return await handleFile(srcName); |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
console.log(`文件处理失败 ${srcName} \n${error}`); |
|
|
|
console.log(`文件处理失败 ${srcName} \n`); |
|
|
|
|
|
|
|
console.error(error); |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|