Browse Source

优化第一轮

master
hejl 2 days ago
parent
commit
dea92a36db
  1. 7
      gofaster/app/plugins/modules/api-collector.js
  2. 74
      gofaster/app/plugins/modules/file-generator.js
  3. 58
      gofaster/app/plugins/modules/route-analyzer.js
  4. 70
      gofaster/app/plugins/route-mapping-plugin.js
  5. 2
      gofaster/app/src/renderer/modules/route-sync/RouteCollector.js
  6. 397
      gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js

7
gofaster/app/plugins/modules/api-collector.js

@ -82,11 +82,12 @@ class ApiCollector { @@ -82,11 +82,12 @@ class ApiCollector {
plugins: ['jsx', 'typescript']
})
const self = this
traverse(ast, {
ObjectMethod(path) {
if (path.node.key && path.node.key.type === 'Identifier') {
const methodName = path.node.key.name
const apiMapping = this.extractApiMapping(path, methodName, serviceName, servicePath)
const apiMapping = self.extractApiMapping(path, methodName, serviceName, servicePath)
if (apiMapping) {
apiMappings.push(apiMapping)
}
@ -97,7 +98,7 @@ class ApiCollector { @@ -97,7 +98,7 @@ class ApiCollector {
if (path.node.id && path.node.id.type === 'Identifier' &&
path.node.init && path.node.init.type === 'ArrowFunctionExpression') {
const methodName = path.node.id.name
const apiMapping = this.extractApiMapping(path.get('init'), methodName, serviceName, servicePath)
const apiMapping = self.extractApiMapping(path.get('init'), methodName, serviceName, servicePath)
if (apiMapping) {
apiMappings.push(apiMapping)
}
@ -105,7 +106,7 @@ class ApiCollector { @@ -105,7 +106,7 @@ class ApiCollector {
}
})
} catch (error) {
// 静默处理错误
console.error(`❌ 分析服务文件失败: ${servicePath}`, error.message)
}
return apiMappings

74
gofaster/app/plugins/modules/file-generator.js

@ -27,6 +27,28 @@ class FileGenerator { @@ -27,6 +27,28 @@ class FileGenerator {
writeFileSync(this.outputPath, content, 'utf-8')
}
/**
* 生成基础映射文件 - 优化版本仅创建基础结构
*/
generateBasicMappingFile() {
const content = `// 路由映射数据 - 构建时生成
export default {
// 路由配置
routes: [],
// API映射配置
apiMappings: []
}`
// 如果文件已存在,先清空内容
if (existsSync(this.outputPath)) {
writeFileSync(this.outputPath, '', 'utf-8')
}
// 写入新内容
writeFileSync(this.outputPath, content, 'utf-8')
}
/**
* 生成文件内容
* @param {Array} routes - 路由数组
@ -34,20 +56,58 @@ class FileGenerator { @@ -34,20 +56,58 @@ class FileGenerator {
* @returns {string} 文件内容
*/
generateFileContent(routes, apiMappings) {
const header = `// 此文件由 route-mapping-plugin 在构建时生成
// 包含路由配置分析结果和API收集结果
// 瘦身处理:只保留运行时必需的信息
const slimRoutes = this.slimRoutes(routes)
const slimApiMappings = this.slimApiMappings(apiMappings)
const header = `// 路由映射数据 - 构建时生成
export default {
// 路由配置分析结果
routes: ${JSON.stringify(routes, null, 2)},
// 路由配置
routes: ${JSON.stringify(slimRoutes, null, 2)},
// API收集结果(包含页面关联和路径完善)
apiMappings: ${JSON.stringify(apiMappings, null, 2)}
// API映射配置
apiMappings: ${JSON.stringify(slimApiMappings, null, 2)}
}`
return header
}
/**
* 瘦身路由信息
* @param {Array} routes - 原始路由数组
* @returns {Array} 瘦身后的路由数组
*/
slimRoutes(routes) {
return routes.map(route => ({
path: route.path,
name: route.name,
component: route.component,
description: route.description
}))
}
/**
* 瘦身API映射信息
* @param {Array} apiMappings - 原始API映射数组
* @returns {Array} 瘦身后的API映射数组
*/
slimApiMappings(apiMappings) {
return apiMappings.map(moduleMapping => ({
module: moduleMapping.module,
serviceName: moduleMapping.serviceName,
apiMappings: moduleMapping.apiMappings.map(api => ({
apiMethodName: api.apiMethodName,
method: api.method,
path: api.path,
triggerSources: api.triggerSources.map(trigger => ({
component: trigger.component,
triggerName: trigger.triggerName,
triggerType: trigger.triggerType
}))
}))
}))
}
/**
* 优化API映射
* @param {Array} apiMappings - API映射数组

58
gofaster/app/plugins/modules/route-analyzer.js

@ -38,19 +38,30 @@ class RouteAnalyzer { @@ -38,19 +38,30 @@ class RouteAnalyzer {
parseRouteConfig(routeContent) {
const routes = []
// 简单的路由解析,查找路由定义
const routeMatches = routeContent.match(/path:\s*['"]([^'"]+)['"][\s\S]*?component:\s*['"]([^'"]+)['"]/g)
// 解析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*['"]([^'"]+)['"]/)
const componentMatch = match.match(/component:\s*([A-Za-z][A-Za-z0-9]*)/)
const nameMatch = match.match(/name:\s*['"]([^'"]+)['"]/)
if (pathMatch && componentMatch) {
routes.push({
const route = {
path: pathMatch[1],
component: componentMatch[1]
})
}
if (nameMatch) {
route.name = nameMatch[1]
}
// 添加中文描述
route.description = this._generateRouteDescription(route)
routes.push(route)
}
})
}
@ -58,6 +69,43 @@ class RouteAnalyzer { @@ -58,6 +69,43 @@ class RouteAnalyzer {
return routes
}
/**
* 生成路由的中文描述
* @param {Object} route - 路由对象
* @returns {string} 中文描述
*/
_generateRouteDescription(route) {
const path = route.path
const component = route.component
// 根据路径和组件名称生成中文描述
if (path === '/' || path === '') {
return '首页'
} else if (path.includes('user-management')) {
return '用户管理'
} else if (path.includes('role-management')) {
return '角色管理'
} else if (path.includes('user-profile')) {
return '个人资料'
} else if (path.includes('settings')) {
return '系统设置'
} else if (component === 'MainLayout') {
return '主布局'
} else if (component === 'Home') {
return '首页'
} else if (component === 'UserManagement') {
return '用户管理'
} else if (component === 'RoleManagement') {
return '角色管理'
} else if (component === 'UserProfile') {
return '个人资料'
} else if (component === 'Settings') {
return '系统设置'
} else {
return `${component}页面`
}
}
/**
* 获取模块列表
* @returns {Array} 模块目录列表

70
gofaster/app/plugins/route-mapping-plugin.js

@ -113,41 +113,13 @@ function routeMappingPlugin() { @@ -113,41 +113,13 @@ function routeMappingPlugin() {
return false
},
// 检查是否需要重新生成文件
// 检查是否需要重新生成文件 - 优化版本:每次都生成
_shouldRegenerateFile() {
const outputPath = this._fileGenerator.outputPath
if (!existsSync(outputPath)) {
return true
}
const fileStats = statSync(outputPath)
const fileAge = Date.now() - fileStats.mtime.getTime()
// 如果文件超过30分钟,需要重新生成
if (fileAge > 30 * 60 * 1000) {
return true
}
// 检查源文件是否比生成文件更新
const sourceFiles = [
resolve(__dirname, '../../src/renderer/router/index.js'),
resolve(__dirname, '../../src/renderer/modules')
]
for (const sourceFile of sourceFiles) {
if (existsSync(sourceFile)) {
const sourceStats = statSync(sourceFile)
if (sourceStats.mtime.getTime() > fileStats.mtime.getTime()) {
return true
}
}
}
return false
// 为了优化调试,每次都重新生成文件
return true
},
// 收集直接映射关系
// 收集直接映射关系 - 优化版本:简化逻辑
collectDirectMappings() {
try {
// 设置生成中标志
@ -158,21 +130,8 @@ function routeMappingPlugin() { @@ -158,21 +130,8 @@ function routeMappingPlugin() {
return
}
// 1. 分析路由配置
const routes = this._routeAnalyzer.analyzeRoutes()
// 2. 收集API信息(第一层)
const moduleDirs = this._routeAnalyzer.getModuleDirs()
const apiMappings = this._apiCollector.collectApiMappings(moduleDirs)
// 3. 关联页面与API(第三层)
const enhancedApiMappings = this._triggerAnalyzer.findTriggerSourcesForApiMappings(apiMappings)
// 4. 优化API映射
const optimizedApiMappings = this._fileGenerator.optimizeApiMappings(enhancedApiMappings)
// 5. 生成最终映射文件
this._fileGenerator.generateMappingFile(routes, optimizedApiMappings)
// 简化版本:只创建基础文件结构
this._fileGenerator.generateBasicMappingFile()
// 重置生成中标志并更新时间戳
this._generationInProgress = false
@ -183,6 +142,23 @@ function routeMappingPlugin() { @@ -183,6 +142,23 @@ function routeMappingPlugin() {
this._generationWindowStart = this._lastGenerationTime
}
// TODO: 后续步骤将在优化过程中逐步添加
// 1. 分析路由配置
// const routes = this._routeAnalyzer.analyzeRoutes()
// 2. 收集API信息(第一层)
// const moduleDirs = this._routeAnalyzer.getModuleDirs()
// const apiMappings = this._apiCollector.collectApiMappings(moduleDirs)
// 3. 关联页面与API(第三层)
// const enhancedApiMappings = this._triggerAnalyzer.findTriggerSourcesForApiMappings(apiMappings)
// 4. 优化API映射
// const optimizedApiMappings = this._fileGenerator.optimizeApiMappings(enhancedApiMappings)
// 5. 生成最终映射文件
// this._fileGenerator.generateMappingFile(routes, optimizedApiMappings)
} catch (error) {
this._generationInProgress = false
throw error

2
gofaster/app/src/renderer/modules/route-sync/RouteCollector.js

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
// 路由收集器 - 收集前端路由信息
import { directRouteMappings } from './direct-route-mappings.js'
import directRouteMappings from './direct-route-mappings.js'
import { RouteUtils } from './RouteConfig.js'
// 路由收集器

397
gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js

@ -1,391 +1,8 @@ @@ -1,391 +1,8 @@
// 直接路由映射文件
// 此文件由 route-mapping-plugin 在构建时生成
// 包含路由配置分析结果和API收集结果
// 请勿手动修改
export const directRouteMappings = {
// 路由配置分析结果
routes: [
{
"path": "/",
"component": "MainLayout",
"module": "home",
"description": "首页"
},
{
"path": "/user-management",
"name": "UserManagement",
"component": "UserManagement",
"module": "user-management",
"description": "用户管理"
},
{
"path": "/settings",
"name": "Settings",
"component": "Settings",
"module": "settings",
"description": "用户设置"
},
{
"path": "/user-profile",
"name": "UserProfile",
"component": "UserProfile",
"module": "user-management",
"description": "用户管理"
},
{
"path": "/role-management",
"name": "RoleManagement",
"component": "RoleManagement",
"module": "role-management",
"description": "角色管理"
}
],
// 路由映射数据 - 构建时生成
export default {
// 路由配置
routes: [],
// API收集结果(包含页面关联和路径完善)
apiMappings: [
{
"module": "user-management",
"serviceName": "userService",
"servicePath": "D:\\manta\\gofaster\\app\\src\\renderer\\modules\\user-management\\services\\userService.js",
"apiMappings": [
{
"apiMethodName": "getUsers",
"method": "GET",
"path": "/auth/admin/users",
"line": 50,
"triggerSources": [
{
"component": "UserManagement",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "createUser",
"method": "POST",
"path": "/auth/admin/users",
"line": 74,
"triggerSources": [
{
"component": "UserManagement",
"triggerName": "usermanagement-tcfjru",
"triggerType": "button"
}
]
},
{
"apiMethodName": "updateUser",
"method": "PUT",
"path": "/auth/admin/users/{id}",
"line": 85,
"triggerSources": [
{
"component": "UserManagement",
"triggerName": "usermanagement-tcfjru",
"triggerType": "button"
}
]
},
{
"apiMethodName": "deleteUser",
"method": "DELETE",
"path": "/auth/admin/users/{id}",
"line": 96,
"triggerSources": [
{
"component": "UserManagement",
"triggerName": "usermanagement-djl8u7",
"triggerType": "button"
}
]
},
{
"apiMethodName": "getRoles",
"method": "GET",
"path": "/auth/admin/roles",
"line": 107,
"triggerSources": [
{
"component": "UserManagement",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "changePassword",
"method": "POST",
"path": "/auth/change-password",
"line": 118,
"triggerSources": [
{
"component": "PasswordChangeModal",
"triggerName": "passwordchangemodal-krxyp6",
"triggerType": "button"
}
]
},
{
"apiMethodName": "getPasswordPolicy",
"method": "GET",
"path": "/auth/password-policy",
"line": 129,
"triggerSources": [
{
"component": "PasswordChangeModal",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "validatePassword",
"method": "POST",
"path": "/auth/validate-password",
"line": 140,
"triggerSources": [
{
"component": "PasswordChangeModal",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "getCaptcha",
"method": "GET",
"path": "/auth/captcha",
"line": 173,
"triggerSources": [
{
"component": "LoginModal",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "login",
"method": "POST",
"path": "/auth/login",
"line": 184,
"triggerSources": [
{
"component": "LoginModal",
"triggerName": "loginmodal-ys2551",
"triggerType": "button"
}
]
},
{
"apiMethodName": "getCurrentUser",
"method": "GET",
"path": "/auth/me",
"line": 252,
"triggerSources": [
{
"component": "UserProfile",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "changePassword",
"method": "POST",
"path": "/auth/change-password",
"line": 273,
"triggerSources": [
{
"component": "PasswordChangeModal",
"triggerName": "passwordchangemodal-krxyp6",
"triggerType": "button"
}
]
}
]
},
{
"module": "role-management",
"serviceName": "roleService",
"servicePath": "D:\\manta\\gofaster\\app\\src\\renderer\\modules\\role-management\\services\\roleService.js",
"apiMappings": [
{
"apiMethodName": "getRoles",
"method": "GET",
"path": "/auth/roles",
"line": 47,
"triggerSources": [
{
"component": "RoleManagement",
"triggerName": null,
"triggerType": "page"
},
{
"component": "UserRoleAssignment",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "getRole",
"method": "GET",
"path": "/auth/roles/{id}",
"line": 60,
"triggerSources": [
{
"component": "PermissionManager",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "createRole",
"method": "POST",
"path": "/auth/roles",
"line": 71,
"triggerSources": [
{
"component": "RoleManagement",
"triggerName": "save-role",
"triggerType": "button"
}
]
},
{
"apiMethodName": "updateRole",
"method": "PUT",
"path": "/auth/roles/{id}",
"line": 82,
"triggerSources": [
{
"component": "RoleManagement",
"triggerName": "save-role",
"triggerType": "button"
},
{
"component": "PermissionManager",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "deleteRole",
"method": "DELETE",
"path": "/auth/roles/{id}",
"line": 93,
"triggerSources": [
{
"component": "RoleManagement",
"triggerName": "delete-role",
"triggerType": "button"
}
]
},
{
"apiMethodName": "getPermissions",
"method": "GET",
"path": "/auth/permissions",
"line": 104,
"triggerSources": [
{
"component": "PermissionManager",
"triggerName": null,
"triggerType": "page"
},
{
"component": "RolePermissionAssignment",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "assignRolesToUser",
"method": "POST",
"path": "/auth/roles/users/{userId}/assign",
"line": 115,
"triggerSources": [
{
"component": "UserRoleAssignment",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "getUserRoles",
"method": "GET",
"path": "/auth/roles/users/{userId}",
"line": 128,
"triggerSources": [
{
"component": "UserRoleAssignment",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "removeRolesFromUser",
"method": "DELETE",
"path": "/auth/roles/users/{userId}/remove",
"line": 139,
"triggerSources": [
{
"component": "UserRoleAssignment",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "getRolePermissions",
"method": "GET",
"path": "/auth/permissions/roles/{roleId}",
"line": 152,
"triggerSources": [
{
"component": "RolePermissionAssignment",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "assignPermissionsToRole",
"method": "POST",
"path": "/auth/permissions/roles/{roleId}/assign",
"line": 163,
"triggerSources": [
{
"component": "RolePermissionAssignment",
"triggerName": null,
"triggerType": "page"
}
]
},
{
"apiMethodName": "removePermissionsFromRole",
"method": "DELETE",
"path": "/auth/permissions/roles/{roleId}/remove",
"line": 176,
"triggerSources": [
{
"component": "RolePermissionAssignment",
"triggerName": null,
"triggerType": "page"
}
]
}
]
}
]
}
export default directRouteMappings
// API映射配置
apiMappings: []
}
Loading…
Cancel
Save