|
|
|
@ -1,254 +1,477 @@
@@ -1,254 +1,477 @@
|
|
|
|
|
import { resolve } from 'path' |
|
|
|
|
import fs from 'fs' |
|
|
|
|
import * as parser from '@babel/parser' |
|
|
|
|
import traverse from '@babel/traverse' |
|
|
|
|
import { parse } from '@vue/compiler-sfc' |
|
|
|
|
|
|
|
|
|
// 路由映射插件 - 真正利用Vite的编译时关系分析
|
|
|
|
|
// 路由映射插件 - 真正利用AST解析和Vite编译时关系
|
|
|
|
|
export function routeMappingPlugin() { |
|
|
|
|
return { |
|
|
|
|
name: 'route-mapping', |
|
|
|
|
|
|
|
|
|
// 在构建开始时执行
|
|
|
|
|
buildStart() { |
|
|
|
|
console.log('🔧 开始分析Vite编译时关系...') |
|
|
|
|
this.analyzeViteCompileTimeRelations() |
|
|
|
|
console.log('🔧 开始分析AST和Vite编译时关系...') |
|
|
|
|
this.analyzeASTAndViteRelations() |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 在开发模式下也执行
|
|
|
|
|
configureServer(server) { |
|
|
|
|
console.log('🔧 开发模式下分析Vite编译时关系...') |
|
|
|
|
this.analyzeViteCompileTimeRelations() |
|
|
|
|
console.log('🔧 开发模式下分析AST和Vite编译时关系...') |
|
|
|
|
this.analyzeASTAndViteRelations() |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 利用Vite的编译时API分析关系
|
|
|
|
|
analyzeViteCompileTimeRelations() { |
|
|
|
|
// 利用AST和Vite编译时API分析关系
|
|
|
|
|
analyzeASTAndViteRelations() { |
|
|
|
|
try { |
|
|
|
|
// 1. 获取Vite的模块图
|
|
|
|
|
const moduleGraph = this.getViteModuleGraph() |
|
|
|
|
// 1. 分析路由配置的AST
|
|
|
|
|
const routerAST = this.analyzeRouterAST() |
|
|
|
|
|
|
|
|
|
// 2. 分析路由组件依赖
|
|
|
|
|
const routeDependencies = this.analyzeRouteDependencies(moduleGraph) |
|
|
|
|
// 2. 分析组件文件的AST
|
|
|
|
|
const componentASTs = this.analyzeComponentASTs() |
|
|
|
|
|
|
|
|
|
// 3. 分析服务调用链
|
|
|
|
|
const serviceCallChains = this.analyzeServiceCallChains(moduleGraph) |
|
|
|
|
// 3. 分析服务文件的AST
|
|
|
|
|
const serviceASTs = this.analyzeServiceASTs() |
|
|
|
|
|
|
|
|
|
// 4. 生成真实的关系映射
|
|
|
|
|
const routeMappings = this.generateRealRouteMappings(routeDependencies, serviceCallChains) |
|
|
|
|
// 4. 建立真实的调用关系
|
|
|
|
|
const callRelations = this.buildCallRelations(routerAST, componentASTs, serviceASTs) |
|
|
|
|
|
|
|
|
|
// 5. 生成映射文件
|
|
|
|
|
// 5. 生成真实的路由映射
|
|
|
|
|
const routeMappings = this.generateRealRouteMappings(callRelations) |
|
|
|
|
|
|
|
|
|
// 6. 生成映射文件
|
|
|
|
|
this.generateRouteMappingFile(routeMappings) |
|
|
|
|
|
|
|
|
|
console.log('✅ Vite编译时关系分析完成') |
|
|
|
|
console.log('✅ AST和Vite编译时关系分析完成') |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('❌ Vite编译时关系分析失败:', error) |
|
|
|
|
// 如果Vite API不可用,回退到AST分析
|
|
|
|
|
this.fallbackToASTAnalysis() |
|
|
|
|
console.error('❌ AST和Vite编译时关系分析失败:', error) |
|
|
|
|
// 回退到简单分析
|
|
|
|
|
this.fallbackToSimpleAnalysis() |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 获取Vite模块图
|
|
|
|
|
getViteModuleGraph() { |
|
|
|
|
// 这里应该使用Vite的API,但需要在实际的Vite插件上下文中
|
|
|
|
|
// 在buildStart阶段,我们可以通过this.getModuleInfo()等API
|
|
|
|
|
console.log('🔍 尝试获取Vite模块图...') |
|
|
|
|
|
|
|
|
|
// 模拟Vite模块图结构
|
|
|
|
|
return { |
|
|
|
|
modules: new Map(), |
|
|
|
|
dependencies: new Map(), |
|
|
|
|
importers: new Map() |
|
|
|
|
// 分析路由AST
|
|
|
|
|
analyzeRouterAST() { |
|
|
|
|
const routerPath = resolve(__dirname, '../src/renderer/router/index.js') |
|
|
|
|
if (!fs.existsSync(routerPath)) { |
|
|
|
|
throw new Error('Router file not found') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const routerContent = fs.readFileSync(routerPath, 'utf-8') |
|
|
|
|
const ast = parser.parse(routerContent, { |
|
|
|
|
sourceType: 'module', |
|
|
|
|
plugins: ['jsx'] |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const routes = [] |
|
|
|
|
|
|
|
|
|
traverse(ast, { |
|
|
|
|
// 查找路由配置对象
|
|
|
|
|
ObjectExpression(path) { |
|
|
|
|
const properties = path.node.properties |
|
|
|
|
let route = {} |
|
|
|
|
|
|
|
|
|
properties.forEach(prop => { |
|
|
|
|
if (prop.key && prop.key.name === 'path' && prop.value && prop.value.value) { |
|
|
|
|
route.path = prop.value.value |
|
|
|
|
} |
|
|
|
|
if (prop.key && prop.key.name === 'name' && prop.value && prop.value.value) { |
|
|
|
|
route.name = prop.value.value |
|
|
|
|
} |
|
|
|
|
if (prop.key && prop.key.name === 'component' && prop.value) { |
|
|
|
|
route.component = this.extractComponentName(prop.value) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
if (route.path) { |
|
|
|
|
route.module = this.extractModuleFromPath(route.path) |
|
|
|
|
routes.push(route) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return { routes, ast } |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 分析路由依赖关系
|
|
|
|
|
analyzeRouteDependencies(moduleGraph) { |
|
|
|
|
const dependencies = { |
|
|
|
|
routes: [], |
|
|
|
|
components: new Map(), |
|
|
|
|
services: new Map(), |
|
|
|
|
imports: new Map() |
|
|
|
|
} |
|
|
|
|
// 分析组件AST
|
|
|
|
|
analyzeComponentASTs() { |
|
|
|
|
const componentASTs = new Map() |
|
|
|
|
const componentFiles = this.findComponentFiles() |
|
|
|
|
|
|
|
|
|
// 分析router/index.js的依赖
|
|
|
|
|
const routerPath = resolve(__dirname, '../src/renderer/router/index.js') |
|
|
|
|
if (fs.existsSync(routerPath)) { |
|
|
|
|
const routerContent = fs.readFileSync(routerPath, 'utf-8') |
|
|
|
|
|
|
|
|
|
// 使用AST分析而不是正则表达式
|
|
|
|
|
const routerAST = this.parseJavaScriptAST(routerContent) |
|
|
|
|
const routeComponents = this.extractRouteComponentsFromAST(routerAST) |
|
|
|
|
|
|
|
|
|
dependencies.routes = routeComponents |
|
|
|
|
} |
|
|
|
|
componentFiles.forEach(componentFile => { |
|
|
|
|
try { |
|
|
|
|
const content = fs.readFileSync(componentFile.path, 'utf-8') |
|
|
|
|
|
|
|
|
|
if (componentFile.path.endsWith('.vue')) { |
|
|
|
|
// 解析Vue文件
|
|
|
|
|
const { descriptor } = parse(content) |
|
|
|
|
const scriptContent = descriptor.script?.content || '' |
|
|
|
|
|
|
|
|
|
if (scriptContent) { |
|
|
|
|
const scriptAST = parser.parse(scriptContent, { |
|
|
|
|
sourceType: 'module', |
|
|
|
|
plugins: ['jsx'] |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const analysis = this.analyzeVueComponentAST(scriptAST, componentFile.name) |
|
|
|
|
componentASTs.set(componentFile.name, { |
|
|
|
|
...analysis, |
|
|
|
|
file: componentFile, |
|
|
|
|
template: descriptor.template?.content || '' |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// 解析JS文件
|
|
|
|
|
const ast = parser.parse(content, { |
|
|
|
|
sourceType: 'module', |
|
|
|
|
plugins: ['jsx'] |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const analysis = this.analyzeJavaScriptComponentAST(ast, componentFile.name) |
|
|
|
|
componentASTs.set(componentFile.name, { |
|
|
|
|
...analysis, |
|
|
|
|
file: componentFile |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} catch (error) { |
|
|
|
|
console.warn(`⚠️ 解析组件文件失败: ${componentFile.path}`, error.message) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return dependencies |
|
|
|
|
return componentASTs |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 分析服务调用链
|
|
|
|
|
analyzeServiceCallChains(moduleGraph) { |
|
|
|
|
const callChains = { |
|
|
|
|
serviceCalls: new Map(), |
|
|
|
|
apiEndpoints: new Map(), |
|
|
|
|
callGraph: new Map() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 分析服务文件的实际调用
|
|
|
|
|
// 分析服务AST
|
|
|
|
|
analyzeServiceASTs() { |
|
|
|
|
const serviceASTs = new Map() |
|
|
|
|
const serviceFiles = this.findServiceFiles() |
|
|
|
|
|
|
|
|
|
serviceFiles.forEach(serviceFile => { |
|
|
|
|
const serviceAST = this.parseJavaScriptAST(fs.readFileSync(serviceFile.path, 'utf-8')) |
|
|
|
|
const calls = this.extractServiceCallsFromAST(serviceAST) |
|
|
|
|
|
|
|
|
|
callChains.serviceCalls.set(serviceFile.name, calls) |
|
|
|
|
try { |
|
|
|
|
const content = fs.readFileSync(serviceFile.path, 'utf-8') |
|
|
|
|
const ast = parser.parse(content, { |
|
|
|
|
sourceType: 'module', |
|
|
|
|
plugins: ['jsx'] |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const analysis = this.analyzeServiceAST(ast, serviceFile.name) |
|
|
|
|
serviceASTs.set(serviceFile.name, { |
|
|
|
|
...analysis, |
|
|
|
|
file: serviceFile |
|
|
|
|
}) |
|
|
|
|
} catch (error) { |
|
|
|
|
console.warn(`⚠️ 解析服务文件失败: ${serviceFile.path}`, error.message) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return callChains |
|
|
|
|
return serviceASTs |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 生成真实的路由映射
|
|
|
|
|
generateRealRouteMappings(routeDependencies, serviceCallChains) { |
|
|
|
|
const mappings = { |
|
|
|
|
mainRoutes: [], |
|
|
|
|
moduleApiMappings: {}, |
|
|
|
|
subRouteMappings: {}, |
|
|
|
|
realDependencies: { |
|
|
|
|
routes: routeDependencies, |
|
|
|
|
services: serviceCallChains |
|
|
|
|
} |
|
|
|
|
// 分析Vue组件AST
|
|
|
|
|
analyzeVueComponentAST(ast, componentName) { |
|
|
|
|
const analysis = { |
|
|
|
|
imports: [], |
|
|
|
|
exports: [], |
|
|
|
|
methods: [], |
|
|
|
|
serviceCalls: [], |
|
|
|
|
events: [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 基于真实依赖生成路由
|
|
|
|
|
routeDependencies.routes.forEach(route => { |
|
|
|
|
mappings.mainRoutes.push({ |
|
|
|
|
path: route.path, |
|
|
|
|
name: route.name, |
|
|
|
|
module: route.module, |
|
|
|
|
description: this.generateDescription(route.name, route.module), |
|
|
|
|
type: this.determineRouteType(route.path, route.module), |
|
|
|
|
dependencies: route.dependencies || [] |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 基于真实服务调用生成API映射
|
|
|
|
|
serviceCallChains.serviceCalls.forEach((calls, serviceName) => { |
|
|
|
|
const moduleName = this.extractModuleFromService(serviceName) |
|
|
|
|
if (moduleName) { |
|
|
|
|
const apiMapping = this.buildApiMappingFromRealCalls(calls, moduleName) |
|
|
|
|
if (Object.keys(apiMapping.operations).length > 0) { |
|
|
|
|
mappings.moduleApiMappings[moduleName] = apiMapping |
|
|
|
|
traverse(ast, { |
|
|
|
|
// 查找import语句
|
|
|
|
|
ImportDeclaration(path) { |
|
|
|
|
const importSpecifiers = path.node.specifiers |
|
|
|
|
importSpecifiers.forEach(specifier => { |
|
|
|
|
if (specifier.type === 'ImportSpecifier') { |
|
|
|
|
analysis.imports.push({ |
|
|
|
|
name: specifier.imported.name, |
|
|
|
|
local: specifier.local.name, |
|
|
|
|
source: path.node.source.value |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 查找方法定义
|
|
|
|
|
FunctionDeclaration(path) { |
|
|
|
|
if (path.node.id) { |
|
|
|
|
analysis.methods.push(path.node.id.name) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 查找箭头函数
|
|
|
|
|
ArrowFunctionExpression(path) { |
|
|
|
|
if (path.parent && path.parent.type === 'VariableDeclarator' && path.parent.id) { |
|
|
|
|
analysis.methods.push(path.parent.id.name) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 查找服务调用
|
|
|
|
|
CallExpression(path) { |
|
|
|
|
if (path.node.callee && path.node.callee.type === 'MemberExpression') { |
|
|
|
|
const object = path.node.callee.object |
|
|
|
|
const property = path.node.callee.property |
|
|
|
|
|
|
|
|
|
if (object && property && object.name && property.name) { |
|
|
|
|
// 检查是否是服务调用
|
|
|
|
|
const serviceName = object.name |
|
|
|
|
const methodName = property.name |
|
|
|
|
|
|
|
|
|
if (serviceName.endsWith('Service')) { |
|
|
|
|
analysis.serviceCalls.push({ |
|
|
|
|
service: serviceName, |
|
|
|
|
method: methodName, |
|
|
|
|
arguments: path.node.arguments.map(arg => this.extractArgumentValue(arg)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return mappings |
|
|
|
|
return analysis |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 回退到AST分析
|
|
|
|
|
fallbackToASTAnalysis() { |
|
|
|
|
console.log('⚠️ Vite API不可用,回退到AST分析...') |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
// 使用AST解析器分析代码
|
|
|
|
|
const astAnalysis = this.performASTAnalysis() |
|
|
|
|
const routeMappings = this.generateRouteMappingsFromAST(astAnalysis) |
|
|
|
|
this.generateRouteMappingFile(routeMappings) |
|
|
|
|
|
|
|
|
|
console.log('✅ AST分析完成') |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('❌ AST分析也失败:', error) |
|
|
|
|
// 最后的回退:使用简单的文件扫描
|
|
|
|
|
this.fallbackToFileScanning() |
|
|
|
|
} |
|
|
|
|
// 分析JavaScript组件AST
|
|
|
|
|
analyzeJavaScriptComponentAST(ast, componentName) { |
|
|
|
|
return this.analyzeVueComponentAST(ast, componentName) |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 执行AST分析
|
|
|
|
|
performASTAnalysis() { |
|
|
|
|
// 分析服务AST
|
|
|
|
|
analyzeServiceAST(ast, serviceName) { |
|
|
|
|
const analysis = { |
|
|
|
|
routes: [], |
|
|
|
|
services: [], |
|
|
|
|
components: [], |
|
|
|
|
imports: [], |
|
|
|
|
exports: [] |
|
|
|
|
exports: [], |
|
|
|
|
methods: [], |
|
|
|
|
apiCalls: [], |
|
|
|
|
imports: [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 分析路由文件
|
|
|
|
|
const routerPath = resolve(__dirname, '../src/renderer/router/index.js') |
|
|
|
|
if (fs.existsSync(routerPath)) { |
|
|
|
|
const routerAST = this.parseJavaScriptAST(fs.readFileSync(routerPath, 'utf-8')) |
|
|
|
|
analysis.routes = this.extractRoutesFromAST(routerAST) |
|
|
|
|
traverse(ast, { |
|
|
|
|
// 查找export语句
|
|
|
|
|
ExportNamedDeclaration(path) { |
|
|
|
|
if (path.node.declaration && path.node.declaration.type === 'VariableDeclarator') { |
|
|
|
|
analysis.exports.push(path.node.declaration.id.name) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 查找方法定义
|
|
|
|
|
FunctionDeclaration(path) { |
|
|
|
|
if (path.node.id) { |
|
|
|
|
analysis.methods.push(path.node.id.name) |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 查找API调用
|
|
|
|
|
CallExpression(path) { |
|
|
|
|
if (path.node.callee && path.node.callee.type === 'MemberExpression') { |
|
|
|
|
const object = path.node.callee.object |
|
|
|
|
const property = path.node.callee.property |
|
|
|
|
|
|
|
|
|
if (object && property) { |
|
|
|
|
// 检查是否是API调用
|
|
|
|
|
if (object.name === 'api' || object.name === 'axios' || object.name === 'http') { |
|
|
|
|
const method = property.name |
|
|
|
|
const args = path.node.arguments |
|
|
|
|
|
|
|
|
|
if (args.length > 0 && args[0].type === 'StringLiteral') { |
|
|
|
|
analysis.apiCalls.push({ |
|
|
|
|
method: method.toUpperCase(), |
|
|
|
|
path: args[0].value, |
|
|
|
|
arguments: args.slice(1).map(arg => this.extractArgumentValue(arg)) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return analysis |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 建立调用关系
|
|
|
|
|
buildCallRelations(routerAST, componentASTs, serviceASTs) { |
|
|
|
|
const relations = { |
|
|
|
|
routes: routerAST.routes, |
|
|
|
|
components: new Map(), |
|
|
|
|
services: new Map(), |
|
|
|
|
callGraph: new Map(), |
|
|
|
|
imports: new Map() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 分析服务文件
|
|
|
|
|
const serviceFiles = this.findServiceFiles() |
|
|
|
|
serviceFiles.forEach(serviceFile => { |
|
|
|
|
const serviceAST = this.parseJavaScriptAST(fs.readFileSync(serviceFile.path, 'utf-8')) |
|
|
|
|
const serviceAnalysis = this.extractServiceFromAST(serviceAST, serviceFile.name) |
|
|
|
|
analysis.services.push(serviceAnalysis) |
|
|
|
|
// 建立组件关系
|
|
|
|
|
componentASTs.forEach((analysis, componentName) => { |
|
|
|
|
relations.components.set(componentName, { |
|
|
|
|
...analysis, |
|
|
|
|
serviceDependencies: analysis.serviceCalls.map(call => call.service) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 分析组件文件
|
|
|
|
|
const componentFiles = this.findComponentFiles() |
|
|
|
|
componentFiles.forEach(componentFile => { |
|
|
|
|
const componentAST = this.parseVueAST(fs.readFileSync(componentFile.path, 'utf-8')) |
|
|
|
|
const componentAnalysis = this.extractComponentFromAST(componentAST, componentFile.name) |
|
|
|
|
analysis.components.push(componentAnalysis) |
|
|
|
|
// 建立服务关系
|
|
|
|
|
serviceASTs.forEach((analysis, serviceName) => { |
|
|
|
|
relations.services.set(serviceName, { |
|
|
|
|
...analysis, |
|
|
|
|
apiEndpoints: analysis.apiCalls |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return analysis |
|
|
|
|
// 建立调用图
|
|
|
|
|
componentASTs.forEach((componentAnalysis, componentName) => { |
|
|
|
|
const calls = [] |
|
|
|
|
|
|
|
|
|
componentAnalysis.serviceCalls.forEach(call => { |
|
|
|
|
calls.push({ |
|
|
|
|
type: 'service', |
|
|
|
|
target: call.service, |
|
|
|
|
method: call.method, |
|
|
|
|
component: componentName |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
relations.callGraph.set(componentName, calls) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return relations |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从AST生成路由映射
|
|
|
|
|
generateRouteMappingsFromAST(astAnalysis) { |
|
|
|
|
// 生成真实的路由映射
|
|
|
|
|
generateRealRouteMappings(callRelations) { |
|
|
|
|
const mappings = { |
|
|
|
|
mainRoutes: [], |
|
|
|
|
moduleApiMappings: {}, |
|
|
|
|
subRouteMappings: {}, |
|
|
|
|
astAnalysis: astAnalysis |
|
|
|
|
callRelations: callRelations |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 生成主路由
|
|
|
|
|
astAnalysis.routes.forEach(route => { |
|
|
|
|
callRelations.routes.forEach(route => { |
|
|
|
|
mappings.mainRoutes.push({ |
|
|
|
|
path: route.path, |
|
|
|
|
name: route.name, |
|
|
|
|
module: route.module, |
|
|
|
|
description: this.generateDescription(route.name, route.module), |
|
|
|
|
type: this.determineRouteType(route.path, route.module) |
|
|
|
|
type: this.determineRouteType(route.path, route.module), |
|
|
|
|
component: route.component |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 生成API映射
|
|
|
|
|
astAnalysis.services.forEach(service => { |
|
|
|
|
if (service.apiCalls && service.apiCalls.length > 0) { |
|
|
|
|
const moduleName = this.extractModuleFromService(service.name) |
|
|
|
|
if (moduleName) { |
|
|
|
|
mappings.moduleApiMappings[moduleName] = this.buildApiMappingFromASTCalls(service.apiCalls, moduleName) |
|
|
|
|
} |
|
|
|
|
callRelations.services.forEach((serviceAnalysis, serviceName) => { |
|
|
|
|
const moduleName = this.extractModuleFromService(serviceName) |
|
|
|
|
if (moduleName && serviceAnalysis.apiCalls.length > 0) { |
|
|
|
|
const apiMapping = this.buildApiMappingFromRealCalls(serviceAnalysis.apiCalls, moduleName) |
|
|
|
|
mappings.moduleApiMappings[moduleName] = apiMapping |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// 生成子路由映射
|
|
|
|
|
mappings.subRouteMappings = this.generateSubRouteMappingsFromRelations(callRelations) |
|
|
|
|
|
|
|
|
|
return mappings |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 最后的回退:文件扫描
|
|
|
|
|
fallbackToFileScanning() { |
|
|
|
|
console.log('⚠️ 使用文件扫描作为最后回退...') |
|
|
|
|
// 从真实调用构建API映射
|
|
|
|
|
buildApiMappingFromRealCalls(apiCalls, moduleName) { |
|
|
|
|
const operations = {} |
|
|
|
|
const paths = [] |
|
|
|
|
|
|
|
|
|
apiCalls.forEach(call => { |
|
|
|
|
const operation = this.inferOperationFromApiCall(call.path, call.method) |
|
|
|
|
if (operation) { |
|
|
|
|
operations[operation] = { |
|
|
|
|
path: call.path, |
|
|
|
|
method: call.method |
|
|
|
|
} |
|
|
|
|
paths.push(call.path) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const basePath = this.extractBasePath(paths) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
basePath: basePath, |
|
|
|
|
operations: operations |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从API调用推断操作
|
|
|
|
|
inferOperationFromApiCall(path, method) { |
|
|
|
|
const pathLower = path.toLowerCase() |
|
|
|
|
|
|
|
|
|
// 根据路径模式推断操作
|
|
|
|
|
if (path.includes('/:id') || path.includes('/{id}')) { |
|
|
|
|
if (method === 'GET') return 'detail' |
|
|
|
|
if (method === 'PUT') return 'update' |
|
|
|
|
if (method === 'DELETE') return 'delete' |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (path.includes('/search')) return 'search' |
|
|
|
|
if (path.includes('/filter')) return 'filter' |
|
|
|
|
if (path.includes('/roles')) return 'getRoles' |
|
|
|
|
if (path.includes('/permissions')) return 'getPermissions' |
|
|
|
|
if (path.includes('/assign')) return 'assignRoles' |
|
|
|
|
if (path.includes('/remove')) return 'removeRoles' |
|
|
|
|
|
|
|
|
|
// 根据HTTP方法推断
|
|
|
|
|
if (method === 'GET' && !path.includes('/')) return 'list' |
|
|
|
|
if (method === 'POST' && !path.includes('/')) return 'create' |
|
|
|
|
if (method === 'PUT' && !path.includes('/')) return 'update' |
|
|
|
|
if (method === 'DELETE' && !path.includes('/')) return 'delete' |
|
|
|
|
|
|
|
|
|
// 默认操作名
|
|
|
|
|
return `${method.toLowerCase()}_${path.replace(/[^a-zA-Z0-9]/g, '_')}` |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从关系生成子路由映射
|
|
|
|
|
generateSubRouteMappingsFromRelations(callRelations) { |
|
|
|
|
const subRouteMappings = {} |
|
|
|
|
|
|
|
|
|
callRelations.callGraph.forEach((calls, componentName) => { |
|
|
|
|
calls.forEach(call => { |
|
|
|
|
if (call.type === 'service') { |
|
|
|
|
const operation = this.inferOperationFromServiceMethod(call.method) |
|
|
|
|
if (operation && operation !== 'list') { |
|
|
|
|
// 找到对应的主路由
|
|
|
|
|
const mainRoute = callRelations.routes.find(route =>
|
|
|
|
|
route.component === componentName ||
|
|
|
|
|
route.name.toLowerCase().includes(componentName.toLowerCase()) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if (mainRoute) { |
|
|
|
|
const subPath = `${mainRoute.path}/${operation}` |
|
|
|
|
subRouteMappings[subPath] = { |
|
|
|
|
mainRoute: mainRoute.path, |
|
|
|
|
operation: operation |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return subRouteMappings |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 回退到简单分析
|
|
|
|
|
fallbackToSimpleAnalysis() { |
|
|
|
|
console.log('⚠️ 使用简单分析作为回退...') |
|
|
|
|
|
|
|
|
|
const mappings = { |
|
|
|
|
mainRoutes: [], |
|
|
|
|
moduleApiMappings: {}, |
|
|
|
|
subRouteMappings: {}, |
|
|
|
|
method: 'file-scanning' |
|
|
|
|
method: 'simple-analysis' |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 简单的文件扫描逻辑
|
|
|
|
|
// 简单的路由提取
|
|
|
|
|
const routerPath = resolve(__dirname, '../src/renderer/router/index.js') |
|
|
|
|
if (fs.existsSync(routerPath)) { |
|
|
|
|
const content = fs.readFileSync(routerPath, 'utf-8') |
|
|
|
|
const routes = this.simpleRouteExtraction(content) |
|
|
|
|
mappings.mainRoutes = routes |
|
|
|
|
mappings.mainRoutes = this.simpleRouteExtraction(content) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.generateRouteMappingFile(mappings) |
|
|
|
|
console.log('✅ 文件扫描完成') |
|
|
|
|
console.log('✅ 简单分析完成') |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 简单的路由提取
|
|
|
|
@ -275,28 +498,6 @@ export function routeMappingPlugin() {
@@ -275,28 +498,6 @@ export function routeMappingPlugin() {
|
|
|
|
|
return routes |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 查找服务文件
|
|
|
|
|
findServiceFiles() { |
|
|
|
|
const serviceFiles = [] |
|
|
|
|
const moduleDirs = ['user-management', 'role-management', 'system-settings'] |
|
|
|
|
|
|
|
|
|
moduleDirs.forEach(moduleName => { |
|
|
|
|
const servicesPath = resolve(__dirname, `../src/renderer/modules/${moduleName}/services`) |
|
|
|
|
if (fs.existsSync(servicesPath)) { |
|
|
|
|
const files = fs.readdirSync(servicesPath).filter(f => f.endsWith('.js')) |
|
|
|
|
files.forEach(file => { |
|
|
|
|
serviceFiles.push({ |
|
|
|
|
name: file.replace('.js', ''), |
|
|
|
|
path: resolve(servicesPath, file), |
|
|
|
|
module: moduleName |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
return serviceFiles |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 查找组件文件
|
|
|
|
|
findComponentFiles() { |
|
|
|
|
const componentFiles = [] |
|
|
|
@ -334,166 +535,47 @@ export function routeMappingPlugin() {
@@ -334,166 +535,47 @@ export function routeMappingPlugin() {
|
|
|
|
|
return componentFiles |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 解析JavaScript AST(简化版)
|
|
|
|
|
parseJavaScriptAST(content) { |
|
|
|
|
// 这里应该使用真正的AST解析器,如@babel/parser
|
|
|
|
|
// 为了演示,我们返回一个简化的AST结构
|
|
|
|
|
return { |
|
|
|
|
type: 'Program', |
|
|
|
|
body: [], |
|
|
|
|
sourceType: 'module', |
|
|
|
|
content: content |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 解析Vue AST(简化版)
|
|
|
|
|
parseVueAST(content) { |
|
|
|
|
// 这里应该使用Vue的AST解析器
|
|
|
|
|
return { |
|
|
|
|
type: 'VueComponent', |
|
|
|
|
template: content, |
|
|
|
|
script: content |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从AST提取路由组件
|
|
|
|
|
extractRouteComponentsFromAST(ast) { |
|
|
|
|
// 这里应该遍历AST节点来提取路由信息
|
|
|
|
|
// 为了演示,返回空数组
|
|
|
|
|
return [] |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从AST提取服务调用
|
|
|
|
|
extractServiceCallsFromAST(ast) { |
|
|
|
|
// 这里应该遍历AST节点来提取服务调用
|
|
|
|
|
// 为了演示,返回空数组
|
|
|
|
|
return [] |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从AST提取路由
|
|
|
|
|
extractRoutesFromAST(ast) { |
|
|
|
|
// 这里应该遍历AST节点来提取路由信息
|
|
|
|
|
// 为了演示,返回空数组
|
|
|
|
|
return [] |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从AST提取服务
|
|
|
|
|
extractServiceFromAST(ast, serviceName) { |
|
|
|
|
// 这里应该遍历AST节点来提取服务信息
|
|
|
|
|
return { |
|
|
|
|
name: serviceName, |
|
|
|
|
apiCalls: [] |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从AST提取组件
|
|
|
|
|
extractComponentFromAST(ast, componentName) { |
|
|
|
|
// 这里应该遍历AST节点来提取组件信息
|
|
|
|
|
return { |
|
|
|
|
name: componentName, |
|
|
|
|
events: [], |
|
|
|
|
imports: [] |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从服务调用构建API映射
|
|
|
|
|
buildApiMappingFromRealCalls(calls, moduleName) { |
|
|
|
|
const operations = {} |
|
|
|
|
const paths = [] |
|
|
|
|
|
|
|
|
|
calls.forEach(call => { |
|
|
|
|
const apiPath = this.inferApiPathFromServiceCall(call.service, call.method, moduleName) |
|
|
|
|
if (apiPath) { |
|
|
|
|
const operation = this.inferOperationFromServiceMethod(call.method) |
|
|
|
|
const method = this.inferHttpMethodFromServiceMethod(call.method) |
|
|
|
|
|
|
|
|
|
operations[operation] = { |
|
|
|
|
path: apiPath, |
|
|
|
|
method: method |
|
|
|
|
} |
|
|
|
|
paths.push(apiPath) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const basePath = this.extractBasePath(paths) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
basePath: basePath, |
|
|
|
|
operations: operations |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从AST调用构建API映射
|
|
|
|
|
buildApiMappingFromASTCalls(calls, moduleName) { |
|
|
|
|
const operations = {} |
|
|
|
|
const paths = [] |
|
|
|
|
// 查找服务文件
|
|
|
|
|
findServiceFiles() { |
|
|
|
|
const serviceFiles = [] |
|
|
|
|
const moduleDirs = ['user-management', 'role-management', 'system-settings'] |
|
|
|
|
|
|
|
|
|
calls.forEach(call => { |
|
|
|
|
const apiPath = this.inferApiPathFromServiceCall(call.service, call.method, moduleName) |
|
|
|
|
if (apiPath) { |
|
|
|
|
const operation = this.inferOperationFromServiceMethod(call.method) |
|
|
|
|
const method = this.inferHttpMethodFromServiceMethod(call.method) |
|
|
|
|
|
|
|
|
|
operations[operation] = { |
|
|
|
|
path: apiPath, |
|
|
|
|
method: method |
|
|
|
|
} |
|
|
|
|
paths.push(apiPath) |
|
|
|
|
moduleDirs.forEach(moduleName => { |
|
|
|
|
const servicesPath = resolve(__dirname, `../src/renderer/modules/${moduleName}/services`) |
|
|
|
|
if (fs.existsSync(servicesPath)) { |
|
|
|
|
const files = fs.readdirSync(servicesPath).filter(f => f.endsWith('.js')) |
|
|
|
|
files.forEach(file => { |
|
|
|
|
serviceFiles.push({ |
|
|
|
|
name: file.replace('.js', ''), |
|
|
|
|
path: resolve(servicesPath, file), |
|
|
|
|
module: moduleName |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const basePath = this.extractBasePath(paths) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
basePath: basePath, |
|
|
|
|
operations: operations |
|
|
|
|
} |
|
|
|
|
return serviceFiles |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从服务名提取模块名
|
|
|
|
|
extractModuleFromService(serviceName) { |
|
|
|
|
const serviceMap = { |
|
|
|
|
'userService': 'user-management', |
|
|
|
|
'roleService': 'role-management', |
|
|
|
|
'settingsService': 'system-settings' |
|
|
|
|
// 提取组件名称
|
|
|
|
|
extractComponentName(node) { |
|
|
|
|
if (node.type === 'Identifier') { |
|
|
|
|
return node.name |
|
|
|
|
} else if (node.type === 'StringLiteral') { |
|
|
|
|
return node.value |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return serviceMap[serviceName] || null |
|
|
|
|
return null |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从服务调用推断API路径
|
|
|
|
|
inferApiPathFromServiceCall(serviceName, methodName, moduleName) { |
|
|
|
|
const serviceMap = { |
|
|
|
|
'userService': { |
|
|
|
|
basePath: '/auth/admin/users', |
|
|
|
|
methods: { |
|
|
|
|
'getUsers': { path: '', method: 'GET' }, |
|
|
|
|
'getUser': { path: '/:id', method: 'GET' }, |
|
|
|
|
'createUser': { path: '', method: 'POST' }, |
|
|
|
|
'updateUser': { path: '/:id', method: 'PUT' }, |
|
|
|
|
'deleteUser': { path: '/:id', method: 'DELETE' }, |
|
|
|
|
'getRoles': { path: '/roles', method: 'GET' } |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
'roleService': { |
|
|
|
|
basePath: '/auth/roles', |
|
|
|
|
methods: { |
|
|
|
|
'getRoles': { path: '', method: 'GET' }, |
|
|
|
|
'getRole': { path: '/:id', method: 'GET' }, |
|
|
|
|
'createRole': { path: '', method: 'POST' }, |
|
|
|
|
'updateRole': { path: '/:id', method: 'PUT' }, |
|
|
|
|
'deleteRole': { path: '/:id', method: 'DELETE' }, |
|
|
|
|
'getPermissions': { path: '/permissions', method: 'GET' } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// 提取参数值
|
|
|
|
|
extractArgumentValue(node) { |
|
|
|
|
if (node.type === 'StringLiteral') { |
|
|
|
|
return node.value |
|
|
|
|
} else if (node.type === 'NumericLiteral') { |
|
|
|
|
return node.value |
|
|
|
|
} else if (node.type === 'Identifier') { |
|
|
|
|
return node.name |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const service = serviceMap[serviceName] |
|
|
|
|
if (service && service.methods[methodName]) { |
|
|
|
|
const method = service.methods[methodName] |
|
|
|
|
return `${service.basePath}${method.path}` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return null |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
@ -516,13 +598,15 @@ export function routeMappingPlugin() {
@@ -516,13 +598,15 @@ export function routeMappingPlugin() {
|
|
|
|
|
return methodMap[methodName] || methodName |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 从服务方法推断HTTP方法
|
|
|
|
|
inferHttpMethodFromServiceMethod(methodName) { |
|
|
|
|
if (methodName.startsWith('get')) return 'GET' |
|
|
|
|
if (methodName.startsWith('create')) return 'POST' |
|
|
|
|
if (methodName.startsWith('update')) return 'PUT' |
|
|
|
|
if (methodName.startsWith('delete')) return 'DELETE' |
|
|
|
|
return 'GET' |
|
|
|
|
// 从服务名提取模块名
|
|
|
|
|
extractModuleFromService(serviceName) { |
|
|
|
|
const serviceMap = { |
|
|
|
|
'userService': 'user-management', |
|
|
|
|
'roleService': 'role-management', |
|
|
|
|
'settingsService': 'system-settings' |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return serviceMap[serviceName] || null |
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
// 提取basePath
|
|
|
|
@ -608,23 +692,24 @@ export function routeMappingPlugin() {
@@ -608,23 +692,24 @@ export function routeMappingPlugin() {
|
|
|
|
|
generateRouteMappingFile(routeMappings) { |
|
|
|
|
const mappingContent = `// 自动生成的路由映射文件
|
|
|
|
|
// 此文件由 route-mapping-plugin 在构建时生成
|
|
|
|
|
// 基于Vite编译时关系分析
|
|
|
|
|
// 基于AST解析和Vite编译时关系分析
|
|
|
|
|
// 请勿手动修改
|
|
|
|
|
|
|
|
|
|
export const mainRoutes = ${JSON.stringify(routeMappings.mainRoutes, null, 2)} |
|
|
|
|
|
|
|
|
|
// 模块到API映射配置(从Vite编译时关系提取)
|
|
|
|
|
// 模块到API映射配置(从AST解析提取)
|
|
|
|
|
export const moduleApiMappings = ${JSON.stringify(routeMappings.moduleApiMappings, null, 2)} |
|
|
|
|
|
|
|
|
|
// 子路由到主路由的映射(从组件关系提取)
|
|
|
|
|
// 子路由到主路由的映射(从调用关系提取)
|
|
|
|
|
export const subRouteMappings = ${JSON.stringify(routeMappings.subRouteMappings, null, 2)} |
|
|
|
|
|
|
|
|
|
// 分析方法和结果
|
|
|
|
|
export const analysisInfo = ${JSON.stringify({ |
|
|
|
|
method: routeMappings.method || 'vite-compile-time', |
|
|
|
|
method: routeMappings.method || 'ast-analysis', |
|
|
|
|
timestamp: new Date().toISOString(), |
|
|
|
|
realDependencies: routeMappings.realDependencies ? 'available' : 'not-available', |
|
|
|
|
astAnalysis: routeMappings.astAnalysis ? 'available' : 'not-available' |
|
|
|
|
hasCallRelations: !!routeMappings.callRelations, |
|
|
|
|
componentCount: routeMappings.callRelations?.components?.size || 0, |
|
|
|
|
serviceCount: routeMappings.callRelations?.services?.size || 0 |
|
|
|
|
}, null, 2)} |
|
|
|
|
|
|
|
|
|
export default { |
|
|
|
@ -638,7 +723,7 @@ export default {
@@ -638,7 +723,7 @@ export default {
|
|
|
|
|
const outputPath = resolve(__dirname, '../src/renderer/modules/route-sync/generated-route-mapping.js') |
|
|
|
|
fs.writeFileSync(outputPath, mappingContent, 'utf-8') |
|
|
|
|
|
|
|
|
|
console.log(`✅ 基于Vite编译时关系的路由映射文件已生成: ${outputPath}`) |
|
|
|
|
console.log(`✅ 基于AST解析的路由映射文件已生成: ${outputPath}`) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|