Commit 6b9a4d07 authored by 前端-许佳敏's avatar 前端-许佳敏

chore: 优化获取paas菜单功能

parent cd3db032
......@@ -9,6 +9,7 @@ const Axios = require('axios').default;
const deepClone = require('clone')
const fs = require('fs')
const path = require('path')
const { validateRouterJson } = require('./utils')
const BACK_GATEWAY = process.env.BACK_GATEWAY
const source = 1
......@@ -57,6 +58,7 @@ function code2component(components, codes) {
})
}
// 批量组装接口
async function batchAxiosHttps() {
const asyncHttpQueue = deepClone(serviceConfig)
......@@ -105,9 +107,14 @@ async function batchAxiosHttps() {
// if (err) throw err
// })
// code2component(asyncHttpQueue.router.routerList, asyncHttpQueue.router.componentList)
if (!validateRouterJson(asyncHttpQueue.router.routerList)) {
return false
}
fs.writeFile(path.resolve(__dirname, '../config/router.config.json'), `${JSON.stringify(asyncHttpQueue.router.routerList)}`, function(err) {
if (err) throw err
})
}
module.exports = batchAxiosHttps
const path = require('path')
const fs = require('fs')
function validateRouterJson(json, testLocalJson) {
const excludes = ['.DS_Store', '.umi', '.umi-production']
let dispatchs = []
let hashMapReSource = {}
const resourcePath = path.resolve(process.cwd(), 'src')
if (testLocalJson) {
// 测试
hashMapReSource = Array.isArray(testLocalJson) ? transformJson(testLocalJson) : testLocalJson
} else {
dispatchs = dispatchDir(path.resolve(resourcePath, 'pages'), excludes)
// 本地的json
while(dispatchs.length) {
const item = dispatchs.shift()
hashMapReSource[item] = true
dispatchs.push(...dispatchDir(item, excludes))
}
}
// 拉下来的json文件
function transformJson(json) {
return Array.isArray(json) ? json.reduce((target, maps) => {
if (maps.component) {
target[maps.component.replace('@', resourcePath).replace('./', resourcePath + '/pages/')] = true
}
if (maps.routes) {
target = Object.assign(target, transformJson(maps.routes))
}
return target
}, {}) : json
}
// 远程拉下来的json文件
const tempHashMap = transformJson(json)
const warnPaths = compareJson(tempHashMap, hashMapReSource)
if (warnPaths.length > 0) {
console.warn('---------- warn ----------')
console.warn('出现路由文件不匹配')
console.warn('---------- warn ----------')
console.log(warnPaths)
return false
}
return true
}
function dispatchDir(rpath, excludes) {
if (fs.statSync(rpath).isFile()) {
return []
}
return fs.readdirSync(rpath).reduce((dispatchs, dir) => {
if (excludes.includes(dir)) {
return dispatchs
}
const nearDir = path.resolve(rpath, dir)
if (fs.statSync(nearDir).isDirectory()) {
dispatchs.push(nearDir)
} else {
if (path.extname(nearDir) === '.tsx') {
dispatchs.push(nearDir)
}
}
return dispatchs
}, [])
}
function compareJson(json1, json2) {
const results = []
for (const item in json1) {
if (!json2[item] && !json2[item + '.tsx']) {
results.push(item)
}
}
return results
}
/**
* 对router文件做校验,若本地目录不存在该路径,则给出警告,而不覆盖错误的路由
*/
exports.validateRouterJson = validateRouterJson
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment