diff --git a/es6.js b/es6.js index 40633a425..82231feec 100644 --- a/es6.js +++ b/es6.js @@ -64,6 +64,35 @@ async function saveAndFixCode(path, code) { }); } +function divideFile(srcName) { + const targetPath = srcName.match(/.*\//g); + const sourceCode = fs.readFileSync(srcName).toString(); + const splitSourceCode = sourceCode.match(/[\.\s]{1}[^\s\.]+?\s=\sBI\.inherit\([^]*?BI\.shortcut.*\;/g); + const newFileNames = []; + for (let i = 0; i < splitSourceCode.length; i++) { + // 去除开头的 空格 或者 . + const newCode = splitSourceCode[i].slice(1); + // 匹配第一个等号之前的组件名 + const componentName = /BI\.shortcut\("(.*)"/.exec(newCode)[1]; + // 文件名转化 ButtonIcon => demo.button.icon.js + const demoFileName = componentName + '.js'; + newFileNames.push(demoFileName); + // 代码 componentName 前面加上 BI. + const fileCode = 'BI.' + newCode; + + // 规范最后一行的组件为 BI.Button + const targetComponet = /(BI\..*)\s=/.exec(fileCode)[1]; + // 最后一行的内容 + const replaceContext = /BI\.shortcut.*;/.exec(fileCode)[0]; + // 替换 + const finalCode = fileCode.replace(replaceContext,`BI.shortcut("${componentName}", ${targetComponet});`) + + // 创建新文件 + fs.writeFileSync(targetPath + demoFileName, finalCode); + } + return newFileNames; +} + // const target = []; // 加载模块 @@ -107,6 +136,12 @@ async function handleFile(srcName) { let G = (loader.G = {}); const sourceCode = fs.readFileSync(srcName).toString(); + if (sourceCode.match(/shortcut/g).length > 1) { + console.log('该文件存在多处BI.shorcut, 需要拆分...'); + const newTargets = divideFile(srcName); + newTargets.forEach(name => console.log(name)); + return; + } const result = /BI\.(.*?)\s=\sBI\.inherit\(/.exec(sourceCode);