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.
215 lines
5.6 KiB
215 lines
5.6 KiB
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') |
|
} |
|
|
|
/** |
|
* 生成文件内容 |
|
* @param {Array} routes - 路由数组 |
|
* @param {Array} apiMappings - API映射数组 |
|
* @returns {string} 文件内容 |
|
*/ |
|
generateFileContent(routes, apiMappings) { |
|
// 瘦身处理:只保留运行时必需的信息 |
|
const slimRoutes = this.slimRoutes(routes) |
|
const slimApiMappings = this.slimApiMappings(apiMappings) |
|
|
|
const header = `// 路由映射数据 - 构建时生成 |
|
export default { |
|
// 路由配置 |
|
routes: ${JSON.stringify(slimRoutes, 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映射数组 |
|
* @returns {Array} 优化后的API映射数组 |
|
*/ |
|
optimizeApiMappings(apiMappings) { |
|
const optimizedApiMappings = [] |
|
let totalRemoved = 0 |
|
|
|
apiMappings.forEach(moduleMapping => { |
|
const deduplicatedApiMappings = [] |
|
|
|
moduleMapping.apiMappings.forEach(apiMapping => { |
|
// 删除无触发器的API映射 |
|
if (!apiMapping.triggerSources || apiMapping.triggerSources.length === 0) { |
|
totalRemoved++ |
|
return |
|
} |
|
|
|
// 去重触发器源 |
|
const uniqueTriggerSources = this.deduplicateTriggerSources(apiMapping.triggerSources) |
|
|
|
deduplicatedApiMappings.push({ |
|
...apiMapping, |
|
triggerSources: uniqueTriggerSources |
|
}) |
|
}) |
|
|
|
if (deduplicatedApiMappings.length > 0) { |
|
optimizedApiMappings.push({ |
|
...moduleMapping, |
|
apiMappings: deduplicatedApiMappings |
|
}) |
|
} |
|
}) |
|
|
|
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
|
|
|