|
|
@ -1,21 +1,37 @@ |
|
|
|
const fs = require("fs"); |
|
|
|
const fs = require("fs"); |
|
|
|
|
|
|
|
const path = require("path"); |
|
|
|
|
|
|
|
const prettier = require("prettier"); |
|
|
|
|
|
|
|
const { exec } = require("child_process"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function fix(path) { |
|
|
|
|
|
|
|
new Promise(res => { |
|
|
|
|
|
|
|
exec(`yarn eslint --fix ${path}`, () => { |
|
|
|
|
|
|
|
res(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const srcName = process.argv[2]; |
|
|
|
async function handleFile(srcName) { |
|
|
|
|
|
|
|
const sourceCode = fs.readFileSync(srcName).toString(); |
|
|
|
|
|
|
|
|
|
|
|
const sourceCode = fs.readFileSync(srcName).toString(); |
|
|
|
const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode); |
|
|
|
|
|
|
|
if (!result) { |
|
|
|
|
|
|
|
console.log(`可能是已经es6过了 ${srcName}`); |
|
|
|
|
|
|
|
|
|
|
|
const clzName = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode)[1]; |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
const clzName = result[1]; |
|
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
|
|
const collection = { |
|
|
|
const collection = { |
|
|
|
"static": {}, |
|
|
|
"static": {}, |
|
|
|
attr: {}, |
|
|
|
attr: {}, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const BI = { |
|
|
|
const BI = { |
|
|
|
inherit(_, options) { |
|
|
|
inherit(_, options) { |
|
|
|
collection.methods = Object.keys(options) |
|
|
|
collection.methods = Object.keys(options) |
|
|
|
.filter(key => typeof options[key] === "function") |
|
|
|
.filter(key => typeof options[key] === "function") |
|
|
@ -31,34 +47,34 @@ const BI = { |
|
|
|
shortcut(xtype) { |
|
|
|
shortcut(xtype) { |
|
|
|
collection.xtype = xtype; |
|
|
|
collection.xtype = xtype; |
|
|
|
}, |
|
|
|
}, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
eval(sourceCode); |
|
|
|
eval(sourceCode); |
|
|
|
|
|
|
|
|
|
|
|
let M = ""; |
|
|
|
let M = ""; |
|
|
|
let E = ""; |
|
|
|
let E = ""; |
|
|
|
let I = ""; |
|
|
|
let I = ""; |
|
|
|
let A = ""; |
|
|
|
let A = ""; |
|
|
|
|
|
|
|
|
|
|
|
const coreImport = { |
|
|
|
const coreImport = { |
|
|
|
shortcut: true, |
|
|
|
shortcut: true, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
if (superName === "Widget") { |
|
|
|
if (superName === "Widget") { |
|
|
|
coreImport.Widget = true; |
|
|
|
coreImport.Widget = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Object.keys(collection.attr).forEach(key => { |
|
|
|
Object.keys(collection.attr).forEach(key => { |
|
|
|
A = `${key} = ${JSON.stringify(collection.attr[key])};`; |
|
|
|
A = `${key} = ${JSON.stringify(collection.attr[key])};`; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 静态方法
|
|
|
|
// 静态方法
|
|
|
|
Object.keys(collection.static).forEach(key => { |
|
|
|
Object.keys(collection.static).forEach(key => { |
|
|
|
E += `\tstatic ${key} = "${collection.static[key]}"\n`; |
|
|
|
E += `\tstatic ${key} = "${collection.static[key]}"\n`; |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 对函数进行替换
|
|
|
|
// 对函数进行替换
|
|
|
|
collection.methods.forEach(el => { |
|
|
|
collection.methods.forEach(el => { |
|
|
|
let f = `${el.toString().replace(/^function/, el.name)}\n`; |
|
|
|
let f = `${el.toString().replace(/^function/, el.name)}\n`; |
|
|
|
|
|
|
|
|
|
|
|
// 换 BI.Button.superclass
|
|
|
|
// 换 BI.Button.superclass
|
|
|
@ -118,13 +134,13 @@ collection.methods.forEach(el => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
M += `${f}\n`; |
|
|
|
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() |
|
|
@ -139,5 +155,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); |
|
|
|