Browse Source

第一步收集完成。

master
hejl 3 days ago
parent
commit
a7256bac54
  1. 23
      gofaster/app/plugins/modules/file-generator.js
  2. 49
      gofaster/app/plugins/modules/route-analyzer.js
  3. 10
      gofaster/app/plugins/route-mapping-plugin.js
  4. 2
      gofaster/app/src/renderer/modules/route-sync/RouteCollector.js
  5. 29
      gofaster/app/src/renderer/modules/route-sync/direct-route-mappings.js
  6. 15
      gofaster/app/src/renderer/router/index.js
  7. 11
      gofaster/app/test-optimized-collection.js

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

@ -49,6 +49,29 @@ export default {
writeFileSync(this.outputPath, content, 'utf-8') writeFileSync(this.outputPath, content, 'utf-8')
} }
/**
* 生成路由映射文件 - 第二步包含路由信息
* @param {Array} routes - 路由数组
*/
generateRouteMappingFile(routes) {
const content = `// 路由映射数据 - 构建时生成
export default {
// 路由配置
routes: ${JSON.stringify(routes, null, 2)},
// API映射配置
apiMappings: []
}`
// 如果文件已存在,先清空内容
if (existsSync(this.outputPath)) {
writeFileSync(this.outputPath, '', 'utf-8')
}
// 写入新内容
writeFileSync(this.outputPath, content, 'utf-8')
}
/** /**
* 生成文件内容 * 生成文件内容
* @param {Array} routes - 路由数组 * @param {Array} routes - 路由数组

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

@ -31,7 +31,7 @@ class RouteAnalyzer {
} }
/** /**
* 解析路由配置 * 解析路由配置 - 优化版本直接从路由配置中收集信息
* @param {string} routeContent - 路由配置内容 * @param {string} routeContent - 路由配置内容
* @returns {Array} 路由数组 * @returns {Array} 路由数组
*/ */
@ -47,6 +47,7 @@ class RouteAnalyzer {
const pathMatch = match.match(/path:\s*['"]([^'"]+)['"]/) const pathMatch = match.match(/path:\s*['"]([^'"]+)['"]/)
const componentMatch = match.match(/component:\s*([A-Za-z][A-Za-z0-9]*)/) const componentMatch = match.match(/component:\s*([A-Za-z][A-Za-z0-9]*)/)
const nameMatch = match.match(/name:\s*['"]([^'"]+)['"]/) const nameMatch = match.match(/name:\s*['"]([^'"]+)['"]/)
const descriptionMatch = match.match(/description:\s*['"]([^'"]+)['"]/)
if (pathMatch && componentMatch) { if (pathMatch && componentMatch) {
const route = { const route = {
@ -54,12 +55,15 @@ class RouteAnalyzer {
component: componentMatch[1] component: componentMatch[1]
} }
if (nameMatch) { // 只有当name与component不同时才保留name字段
if (nameMatch && nameMatch[1] !== componentMatch[1]) {
route.name = nameMatch[1] route.name = nameMatch[1]
} }
// 添加中文描述 // 优先使用路由配置中的description,如果没有则生成
route.description = this._generateRouteDescription(route) if (descriptionMatch) {
route.description = descriptionMatch[1]
}
routes.push(route) routes.push(route)
} }
@ -69,43 +73,6 @@ class RouteAnalyzer {
return routes 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} 模块目录列表 * @returns {Array} 模块目录列表

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

@ -130,8 +130,12 @@ function routeMappingPlugin() {
return return
} }
// 简化版本:只创建基础文件结构 // 第二步:启用路由分析功能
this._fileGenerator.generateBasicMappingFile() // 1. 分析路由配置
const routes = this._routeAnalyzer.analyzeRoutes()
// 生成包含路由信息的文件
this._fileGenerator.generateRouteMappingFile(routes)
// 重置生成中标志并更新时间戳 // 重置生成中标志并更新时间戳
this._generationInProgress = false this._generationInProgress = false
@ -143,8 +147,6 @@ function routeMappingPlugin() {
} }
// TODO: 后续步骤将在优化过程中逐步添加 // TODO: 后续步骤将在优化过程中逐步添加
// 1. 分析路由配置
// const routes = this._routeAnalyzer.analyzeRoutes()
// 2. 收集API信息(第一层) // 2. 收集API信息(第一层)
// const moduleDirs = this._routeAnalyzer.getModuleDirs() // const moduleDirs = this._routeAnalyzer.getModuleDirs()

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

@ -37,7 +37,7 @@ export class RouteCollector {
return { return {
path: route.path, path: route.path,
name: route.name, name: route.name || route.component, // 如果有name就取name,没有就取component
module: route.module, module: route.module,
description: route.description || `${route.name || route.component}页面`, description: route.description || `${route.name || route.component}页面`,
type: 'list', type: 'list',

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

@ -1,7 +1,34 @@
// 路由映射数据 - 构建时生成 // 路由映射数据 - 构建时生成
export default { export default {
// 路由配置 // 路由配置
routes: [], routes: [
{
"path": "/",
"component": "MainLayout",
"name": "Home",
"description": "首页"
},
{
"path": "/user-management",
"component": "UserManagement",
"description": "用户管理"
},
{
"path": "/settings",
"component": "Settings",
"description": "系统设置"
},
{
"path": "/user-profile",
"component": "UserProfile",
"description": "个人资料"
},
{
"path": "/role-management",
"component": "RoleManagement",
"description": "角色管理"
}
],
// API映射配置 // API映射配置
apiMappings: [] apiMappings: []

15
gofaster/app/src/renderer/router/index.js

@ -14,27 +14,32 @@ const routes = [
{ {
path: '', path: '',
name: 'Home', name: 'Home',
component: Home component: Home,
description: '首页'
}, },
{ {
path: '/user-management', path: '/user-management',
name: 'UserManagement', name: 'UserManagement',
component: UserManagement component: UserManagement,
description: '用户管理'
}, },
{ {
path: '/settings', path: '/settings',
name: 'Settings', name: 'Settings',
component: Settings component: Settings,
description: '系统设置'
}, },
{ {
path: '/user-profile', path: '/user-profile',
name: 'UserProfile', name: 'UserProfile',
component: UserProfile component: UserProfile,
description: '个人资料'
}, },
{ {
path: '/role-management', path: '/role-management',
name: 'RoleManagement', name: 'RoleManagement',
component: RoleManagement component: RoleManagement,
description: '角色管理'
} }
] ]
} }

11
gofaster/app/test-optimized-collection.js

@ -0,0 +1,11 @@
import { RouteCollector } from './src/renderer/modules/route-sync/RouteCollector.js';
const collector = new RouteCollector();
const routes = collector.collectRoutes();
console.log('优化后的路由收集结果:');
console.log('路由数量:', routes.length);
console.log('路由详情:');
routes.forEach((route, index) => {
console.log(`${index + 1}. ${route.path} - name: ${route.name} - component: ${route.component} - description: ${route.description}`);
});
Loading…
Cancel
Save