|
|
@ -1,121 +1,137 @@ |
|
|
|
const fs = require("fs"); |
|
|
|
const fs = require("fs"); |
|
|
|
|
|
|
|
const path = require("path"); |
|
|
|
const srcName = process.argv[2]; |
|
|
|
const prettier = require("prettier"); |
|
|
|
|
|
|
|
const { exec } = require("child_process"); |
|
|
|
const sourceCode = fs.readFileSync(srcName).toString(); |
|
|
|
|
|
|
|
|
|
|
|
async function fix(path) { |
|
|
|
const clzName = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode)[1]; |
|
|
|
new Promise(res => { |
|
|
|
|
|
|
|
exec(`yarn eslint --fix ${path}`, () => { |
|
|
|
const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1]; |
|
|
|
res(); |
|
|
|
|
|
|
|
}); |
|
|
|
// const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1];
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const collection = { |
|
|
|
|
|
|
|
"static": {}, |
|
|
|
|
|
|
|
attr: {}, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const BI = { |
|
|
|
|
|
|
|
inherit(_, options) { |
|
|
|
|
|
|
|
collection.methods = Object.keys(options) |
|
|
|
|
|
|
|
.filter(key => typeof options[key] === "function") |
|
|
|
|
|
|
|
.map(key => options[key]); |
|
|
|
|
|
|
|
Object.keys(options) |
|
|
|
|
|
|
|
.filter(key => typeof options[key] !== "function") |
|
|
|
|
|
|
|
.forEach(key => { |
|
|
|
|
|
|
|
collection.attr[key] = options[key]; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return collection.static; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
shortcut(xtype) { |
|
|
|
|
|
|
|
collection.xtype = xtype; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eval(sourceCode); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let M = ""; |
|
|
|
|
|
|
|
let E = ""; |
|
|
|
|
|
|
|
let I = ""; |
|
|
|
|
|
|
|
let A = ""; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const coreImport = { |
|
|
|
|
|
|
|
shortcut: true, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (superName === "Widget") { |
|
|
|
|
|
|
|
coreImport.Widget = true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Object.keys(collection.attr).forEach(key => { |
|
|
|
async function handleFile(srcName) { |
|
|
|
A = `${key} = ${JSON.stringify(collection.attr[key])};`; |
|
|
|
const sourceCode = fs.readFileSync(srcName).toString(); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode); |
|
|
|
// 静态方法
|
|
|
|
if (!result) { |
|
|
|
Object.keys(collection.static).forEach(key => { |
|
|
|
console.log(`可能是已经es6过了 ${srcName}`); |
|
|
|
E += `\tstatic ${key} = "${collection.static[key]}"\n`; |
|
|
|
|
|
|
|
}); |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
// 对函数进行替换
|
|
|
|
const clzName = result[1]; |
|
|
|
collection.methods.forEach(el => { |
|
|
|
|
|
|
|
let f = `${el.toString().replace(/^function/, el.name)}\n`; |
|
|
|
const superName = /inherit\(BI\.(.*?),/.exec(sourceCode)[1]; |
|
|
|
|
|
|
|
|
|
|
|
// 换 BI.Button.superclass
|
|
|
|
// const xtype = /BI.shortcut\(\"(.*?)\"/.exec(sourceCode)[1];
|
|
|
|
f = f.replace(`BI.${clzName}.superclass`, "super"); |
|
|
|
|
|
|
|
// 换 super._defaultConfig
|
|
|
|
const collection = { |
|
|
|
f = f.replace( |
|
|
|
"static": {}, |
|
|
|
`super\._defaultConfig\.apply\(this\,\sarguments\)`, |
|
|
|
attr: {}, |
|
|
|
"super._defaultConfig(arguments)" |
|
|
|
}; |
|
|
|
); |
|
|
|
|
|
|
|
// 换 super.xxx.apply
|
|
|
|
const BI = { |
|
|
|
f = f.replace(/super\.(.*?)\.apply\(this\,\sarguments\)/, a => { |
|
|
|
inherit(_, options) { |
|
|
|
const f = /super\.(.*?)\.apply\(this\,\sarguments\)/.exec(a); |
|
|
|
collection.methods = Object.keys(options) |
|
|
|
|
|
|
|
.filter(key => typeof options[key] === "function") |
|
|
|
return `super.${f[1]}(...arguments)`; |
|
|
|
.map(key => options[key]); |
|
|
|
|
|
|
|
Object.keys(options) |
|
|
|
|
|
|
|
.filter(key => typeof options[key] !== "function") |
|
|
|
|
|
|
|
.forEach(key => { |
|
|
|
|
|
|
|
collection.attr[key] = options[key]; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return collection.static; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
shortcut(xtype) { |
|
|
|
|
|
|
|
collection.xtype = xtype; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eval(sourceCode); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let M = ""; |
|
|
|
|
|
|
|
let E = ""; |
|
|
|
|
|
|
|
let I = ""; |
|
|
|
|
|
|
|
let A = ""; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const coreImport = { |
|
|
|
|
|
|
|
shortcut: true, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (superName === "Widget") { |
|
|
|
|
|
|
|
coreImport.Widget = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.keys(collection.attr).forEach(key => { |
|
|
|
|
|
|
|
A = `${key} = ${JSON.stringify(collection.attr[key])};`; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const target = [ |
|
|
|
// 静态方法
|
|
|
|
"isNull", |
|
|
|
Object.keys(collection.static).forEach(key => { |
|
|
|
"toPix", |
|
|
|
E += `\tstatic ${key} = "${collection.static[key]}"\n`; |
|
|
|
"isKey", |
|
|
|
|
|
|
|
"isObject", |
|
|
|
|
|
|
|
"map", |
|
|
|
|
|
|
|
"extend", |
|
|
|
|
|
|
|
"isFunction", |
|
|
|
|
|
|
|
"isEmptyArray", |
|
|
|
|
|
|
|
"isArray", |
|
|
|
|
|
|
|
"Controller", |
|
|
|
|
|
|
|
clzName, |
|
|
|
|
|
|
|
"createWidget", |
|
|
|
|
|
|
|
"Events", |
|
|
|
|
|
|
|
"emptyFn", |
|
|
|
|
|
|
|
"nextTick", |
|
|
|
|
|
|
|
"bind", |
|
|
|
|
|
|
|
"i18nText", |
|
|
|
|
|
|
|
"isNotNull", |
|
|
|
|
|
|
|
"isString", |
|
|
|
|
|
|
|
"isNumber", |
|
|
|
|
|
|
|
"isEmpty", |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
M += `${f}\n`; |
|
|
|
// 对函数进行替换
|
|
|
|
}); |
|
|
|
collection.methods.forEach(el => { |
|
|
|
|
|
|
|
let f = `${el.toString().replace(/^function/, el.name)}\n`; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 换 BI.Button.superclass
|
|
|
|
|
|
|
|
f = f.replace(`BI.${clzName}.superclass`, "super"); |
|
|
|
|
|
|
|
// 换 super._defaultConfig
|
|
|
|
|
|
|
|
f = f.replace( |
|
|
|
|
|
|
|
`super\._defaultConfig\.apply\(this\,\sarguments\)`, |
|
|
|
|
|
|
|
"super._defaultConfig(arguments)" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
// 换 super.xxx.apply
|
|
|
|
|
|
|
|
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", |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M += `${f}\n`; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Object.keys(coreImport).forEach(el => { |
|
|
|
Object.keys(coreImport).forEach(el => { |
|
|
|
I += `${el},`; |
|
|
|
I += `${el},`; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const outputCode = ` |
|
|
|
const outputCode = ` |
|
|
|
import {${I}} from "@/core" |
|
|
|
import {${I}} from "@/core" |
|
|
|
|
|
|
|
|
|
|
|
@shortcut() |
|
|
|
@shortcut() |
|
|
@ -130,5 +146,36 @@ ${M} |
|
|
|
} |
|
|
|
} |
|
|
|
`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
|
|
// fs.writeFileSync(`${srcName}.js.raw`, sourceCode);
|
|
|
|
const prettierCode = prettier.format(outputCode); |
|
|
|
fs.writeFileSync(srcName, outputCode); |
|
|
|
fs.writeFileSync(srcName, prettierCode); |
|
|
|
|
|
|
|
await fix(srcName); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return clzName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function traverse(srcName) { |
|
|
|
|
|
|
|
if (srcName.indexOf("__test__") >= 0) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (srcName.endsWith(".js")) { |
|
|
|
|
|
|
|
return await handleFile(srcName); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
const stat = fs.statSync(srcName); |
|
|
|
|
|
|
|
const flag = stat.isDirectory(); |
|
|
|
|
|
|
|
if (flag) { |
|
|
|
|
|
|
|
const files = fs.readdirSync(srcName); |
|
|
|
|
|
|
|
// let indexContent = "";
|
|
|
|
|
|
|
|
for (let i = 0; i < files.length; i++) { |
|
|
|
|
|
|
|
const file = files[i]; |
|
|
|
|
|
|
|
await traverse(path.resolve(srcName, file)); |
|
|
|
|
|
|
|
// const clzName = await traverse(path.resolve(srcName, file));
|
|
|
|
|
|
|
|
// const moduleName = path.basename(srcName).replace(/.js$/, "");
|
|
|
|
|
|
|
|
// if (clzName) {
|
|
|
|
|
|
|
|
// indexContent += `export { ${clzName} } from '${moduleName}'\n`;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const srcName = process.argv[2]; |
|
|
|
|
|
|
|
traverse(srcName); |
|
|
|