You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.7 KiB
101 lines
2.7 KiB
const { readFileSync, existsSync } = require('fs') |
|
const { resolve } = require('path') |
|
|
|
/** |
|
* 路由分析模块 |
|
* 负责分析路由配置 |
|
*/ |
|
class RouteAnalyzer { |
|
constructor() { |
|
this.routes = [] |
|
} |
|
|
|
/** |
|
* 分析路由配置 |
|
* @returns {Array} 路由数组 |
|
*/ |
|
analyzeRoutes() { |
|
this.routes = [] |
|
|
|
try { |
|
const routeConfigPath = resolve(__dirname, '../../src/renderer/router/index.js') |
|
if (existsSync(routeConfigPath)) { |
|
const routeContent = readFileSync(routeConfigPath, 'utf-8') |
|
this.routes = this.parseRouteConfig(routeContent) |
|
} |
|
} catch (error) { |
|
// 静默处理错误 |
|
} |
|
|
|
return this.routes |
|
} |
|
|
|
/** |
|
* 解析路由配置 - 优化版本:直接从路由配置中收集信息 |
|
* @param {string} routeContent - 路由配置内容 |
|
* @returns {Array} 路由数组 |
|
*/ |
|
parseRouteConfig(routeContent) { |
|
const routes = [] |
|
|
|
// 解析Vue Router格式的路由配置 |
|
// 查找所有路由定义,包括嵌套路由 |
|
const routeMatches = routeContent.match(/\{[^}]*path:\s*['"]([^'"]+)['"][^}]*\}/g) |
|
|
|
if (routeMatches) { |
|
routeMatches.forEach(match => { |
|
const pathMatch = match.match(/path:\s*['"]([^'"]+)['"]/) |
|
const componentMatch = match.match(/component:\s*([A-Za-z][A-Za-z0-9]*)/) |
|
const nameMatch = match.match(/name:\s*['"]([^'"]+)['"]/) |
|
const descriptionMatch = match.match(/description:\s*['"]([^'"]+)['"]/) |
|
|
|
if (pathMatch && componentMatch) { |
|
const route = { |
|
path: pathMatch[1], |
|
component: componentMatch[1] |
|
} |
|
|
|
// 只有当name与component不同时才保留name字段 |
|
if (nameMatch && nameMatch[1] !== componentMatch[1]) { |
|
route.name = nameMatch[1] |
|
} |
|
|
|
// 优先使用路由配置中的description,如果没有则生成 |
|
if (descriptionMatch) { |
|
route.description = descriptionMatch[1] |
|
} |
|
|
|
routes.push(route) |
|
} |
|
}) |
|
} |
|
|
|
return routes |
|
} |
|
|
|
/** |
|
* 获取模块列表 |
|
* @returns {Array} 模块目录列表 |
|
*/ |
|
getModuleDirs() { |
|
const moduleDirs = [] |
|
|
|
try { |
|
const modulesPath = resolve(__dirname, '../../src/renderer/modules') |
|
if (existsSync(modulesPath)) { |
|
const dirs = require('fs').readdirSync(modulesPath, { withFileTypes: true }) |
|
dirs.forEach(dirent => { |
|
if (dirent.isDirectory()) { |
|
moduleDirs.push(dirent.name) |
|
} |
|
}) |
|
} |
|
} catch (error) { |
|
// 静默处理错误 |
|
} |
|
|
|
return moduleDirs |
|
} |
|
} |
|
|
|
module.exports = RouteAnalyzer
|
|
|