对比新文件 |
| | |
| | | const { glob } = require('glob') |
| | | const { readFileSync, writeFileSync, ensureDirSync } = require('fs-extra') |
| | | const { resolve } = require('path') |
| | | const regExp = /export default [\s\S]*?;/ |
| | | const regExpObj = /\{[\s\S]*?;/ |
| | | const babel = require('@babel/core') |
| | | const pkg = require('../package.json') |
| | | const isWin = process.platform === 'win32' |
| | | |
| | | /** |
| | | * 鏍规嵁widgets涓嬬殑缁勪欢锛岃嚜鍔ㄧ敓鎴愯彍鍗曟暟鎹紝鐢ㄤ簬瀵瑰寮曠敤 |
| | | */ |
| | | async function start() { |
| | | const tsFiles = await glob(resolve(process.cwd(), 'src/widgets/*/index.ts'), { |
| | | ignore: 'node_modules/**', |
| | | windowsPathsNoEscape: true, |
| | | }) |
| | | |
| | | const menu = [] |
| | | const menuMap = {} |
| | | const errorKey = ' is not defined' |
| | | |
| | | tsFiles.forEach((filePath) => { |
| | | const spl = !isWin ? filePath.split('/') : filePath.split('\\') |
| | | |
| | | const patchName = spl[spl.length - 2] |
| | | const file = readFileSync(filePath, { encoding: 'utf8' }) |
| | | const { code } = babel.transformSync(file) |
| | | const exportDefaultRegion = code.match(regExp) |
| | | const exportDefaultContent = exportDefaultRegion[0] |
| | | if (exportDefaultContent) { |
| | | const v = exportDefaultContent.match(regExpObj) |
| | | const canvasView = exportDefaultContent.match(/canvasView: ([^,]+),/) |
| | | let canvasViewValue = canvasView ? canvasView[0] : '' |
| | | canvasViewValue = !canvasViewValue.includes(')') |
| | | ? canvasViewValue.replace(',', '),') |
| | | : canvasViewValue |
| | | |
| | | const c = v[0].replace(canvasViewValue, '') |
| | | let setViewMatch = c.match(/settingsView:\s*(.*?)(?=\s*[,}])/) |
| | | let newCode = '' |
| | | if (setViewMatch[0]) { |
| | | newCode = c.replace(setViewMatch[0], '').replace(';', '') |
| | | } |
| | | if (newCode.includes('canvasView')) { |
| | | newCode = newCode.replace( |
| | | /canvasView\s*:\s*.*?(\{.*?\}|\(.*?\)|[^\s,]+)\s*,?\s*(?=\n|$)/gs, |
| | | '' |
| | | ) |
| | | } |
| | | |
| | | const codeRun = (code) => { |
| | | const fn = new Function(`return ${code}`) |
| | | const widgetInfo = fn() |
| | | const row = { |
| | | name: widgetInfo.name, |
| | | path: `/${pkg.name}/` + patchName, |
| | | patchName: patchName, |
| | | |
| | | icon: widgetInfo.icon, |
| | | notPage: !!widgetInfo.notPage, |
| | | } |
| | | menu.push(row) |
| | | menuMap[patchName] = row |
| | | } |
| | | try { |
| | | codeRun(newCode) |
| | | } catch (error) { |
| | | if (error.message.includes(errorKey)) { |
| | | const iconKey = error.message.split(errorKey) |
| | | if (iconKey.length > 1) { |
| | | const iconName = iconKey[0] |
| | | const code = newCode.replaceAll(iconName, `"${iconName}"`) |
| | | codeRun(code) |
| | | } |
| | | } else { |
| | | console.error(error.message) |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | const data = `export const menu: Record<string,any>[] = ${JSON.stringify( |
| | | menu, |
| | | null, |
| | | 2 |
| | | )};\nexport const menuMap: Record<string,any> = ${JSON.stringify( |
| | | menuMap, |
| | | null, |
| | | 2 |
| | | )};` |
| | | // 鐢熸垚menu JSON 鍒癰uild.prod |
| | | // const buildInfo = readFileSync(resolve(process.cwd(), '.build'), { |
| | | // encoding: 'utf-8', |
| | | // }) |
| | | // const recoveryWidget = buildInfo.split('\n') |
| | | ensureDirSync(resolve(process.cwd(), './src/config/')) |
| | | writeFileSync(resolve(process.cwd(), './src/config/menu.ts'), data, { |
| | | encoding: 'utf-8', |
| | | }) |
| | | |
| | | // const widgets = menu |
| | | // .map((item) => item.patchName) |
| | | // .filter((name) => !recoveryWidget.includes(name)) |
| | | // writeFileSync(resolve(process.cwd(), '.build.prod'), widgets.join('\n'), { |
| | | // encoding: 'utf-8', |
| | | // }) |
| | | } |
| | | const startTime = performance.now() |
| | | |
| | | start() |
| | | |
| | | console.log('鎵ц鏃堕棿: ', Math.ceil(performance.now() - startTime), 'ms') |