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.

203 lines
5.2 KiB

2 days ago
const { writeFileSync, existsSync } = require('fs')
const { resolve } = require('path')
/**
* 文件生成模块
* 负责生成最终的映射文件
*/
class FileGenerator {
constructor() {
this.outputPath = resolve(__dirname, '../../src/renderer/modules/route-sync/direct-route-mappings.js')
}
/**
* 生成映射文件
* @param {Array} routes - 路由数组
* @param {Array} apiMappings - API映射数组
*/
generateMappingFile(routes, apiMappings) {
const content = this.generateFileContent(routes, apiMappings)
// 如果文件已存在,先清空内容
if (existsSync(this.outputPath)) {
writeFileSync(this.outputPath, '', 'utf-8')
}
// 写入新内容
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 - 路由数组
*/
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')
}
/**
* 生成路由和API映射文件 - 第三步第一小步包含API信息
* @param {Array} routes - 路由数组
* @param {Array} apiMappings - API映射数组
*/
generateRouteApiMappingFile(routes, apiMappings) {
const content = `// 路由映射数据 - 构建时生成
export default {
// 路由配置
routes: ${JSON.stringify(routes, null, 2)},
// API映射配置
apiMappings: ${JSON.stringify(apiMappings, null, 2)}
}`
// 如果文件已存在,先清空内容
if (existsSync(this.outputPath)) {
writeFileSync(this.outputPath, '', 'utf-8')
}
// 写入新内容
writeFileSync(this.outputPath, content, 'utf-8')
}
2 days ago
/**
* 生成文件内容
* @param {Array} routes - 路由数组
* @param {Array} apiMappings - API映射数组
* @returns {string} 文件内容
*/
generateFileContent(routes, apiMappings) {
// 瘦身处理:只保留运行时必需的信息
const slimRoutes = this.slimRoutes(routes)
const slimApiMappings = this.slimApiMappings(apiMappings)
const header = `// 路由映射数据 - 构建时生成
2 days ago
export default {
// 路由配置
routes: ${JSON.stringify(slimRoutes, null, 2)},
2 days ago
// API映射配置
apiMappings: ${JSON.stringify(slimApiMappings, null, 2)}
2 days ago
}`
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(api => ({
apiMethodName: api.apiMethodName,
method: api.method,
path: api.path,
serviceName: api.serviceName,
module: api.module,
triggerSources: api.triggerSources.map(trigger => ({
component: trigger.component,
triggerName: trigger.triggerName,
triggerType: trigger.triggerType
}))
}))
}
2 days ago
/**
* 优化API映射
* @param {Array} apiMappings - API映射数组
* @returns {Array} 优化后的API映射数组
*/
optimizeApiMappings(apiMappings) {
const optimizedApiMappings = []
let totalRemoved = 0
apiMappings.forEach(apiMapping => {
// 删除无触发器的API映射
if (!apiMapping.triggerSources || apiMapping.triggerSources.length === 0) {
totalRemoved++
return
}
2 days ago
// 去重触发器源
const uniqueTriggerSources = this.deduplicateTriggerSources(apiMapping.triggerSources)
2 days ago
optimizedApiMappings.push({
...apiMapping,
triggerSources: uniqueTriggerSources
})
2 days ago
})
return optimizedApiMappings
}
/**
* 去重触发器源
* @param {Array} triggerSources - 触发器源数组
* @returns {Array} 去重后的触发器源数组
*/
deduplicateTriggerSources(triggerSources) {
const seen = new Set()
return triggerSources.filter(trigger => {
const key = `${trigger.component}-${trigger.triggerName}-${trigger.triggerType}`
if (seen.has(key)) {
return false
}
seen.add(key)
return true
})
}
}
module.exports = FileGenerator