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.
109 lines
2.9 KiB
109 lines
2.9 KiB
3 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')
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 生成文件内容
|
||
|
* @param {Array} routes - 路由数组
|
||
|
* @param {Array} apiMappings - API映射数组
|
||
|
* @returns {string} 文件内容
|
||
|
*/
|
||
|
generateFileContent(routes, apiMappings) {
|
||
|
const header = `// 此文件由 route-mapping-plugin 在构建时生成
|
||
|
// 包含路由配置分析结果和API收集结果
|
||
|
|
||
|
export default {
|
||
|
// 路由配置分析结果
|
||
|
routes: ${JSON.stringify(routes, null, 2)},
|
||
|
|
||
|
// API收集结果(包含页面关联和路径完善)
|
||
|
apiMappings: ${JSON.stringify(apiMappings, null, 2)}
|
||
|
}`
|
||
|
|
||
|
return header
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 优化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
|