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.
 
 
 
 
 
 

139 lines
3.6 KiB

const { readFileSync, writeFileSync } = require('fs')
const path = require('path')
/**
* 数据清理器
* 清理direct-route-mappings.js中的数据结构
*/
class DataCleaner {
constructor() {
this.mappingsPath = path.join(__dirname, '../../src/renderer/modules/route-sync/direct-route-mappings.js')
}
/**
* 清理数据结构
*/
cleanData() {
try {
// 读取direct-route-mappings.js
const content = readFileSync(this.mappingsPath, 'utf-8')
// 解析mappings内容
const mappings = this.parseMappings(content)
// 清理数据
const cleanedMappings = this.cleanMappings(mappings)
// 生成新的文件内容
const newContent = this.generateNewContent(cleanedMappings)
// 写回文件
writeFileSync(this.mappingsPath, newContent, 'utf-8')
// 返回清理后的数据,供后续处理
return cleanedMappings
} catch (error) {
return null
}
}
/**
* 解析mappings内容
*/
parseMappings(content) {
// 提取export default部分,文件可能没有以分号结尾
const mappingsMatch = content.match(/export default ([\s\S]*?)(?:;|$)/)
if (!mappingsMatch) {
throw new Error('无法解析direct-route-mappings.js')
}
// 使用eval解析(在生产环境中应该使用更安全的方式)
const mappings = eval(`(${mappingsMatch[1]})`)
return mappings
}
/**
* 清理mappings数据
*/
cleanMappings(mappings) {
// 保持原有的结构,只清理apiMappings部分
const cleanedMappings = {
routes: mappings.routes || [],
apiMappings: []
}
if (!mappings.apiMappings || mappings.apiMappings.length === 0) {
return cleanedMappings
}
// 新的数据结构:apiMappings直接是API映射数组
mappings.apiMappings.forEach(api => {
// 仅删除triggerSources列表为空的apiMapping
// 保留所有有triggerSources的apiMapping,不管triggerType是什么
if (api.triggerSources && api.triggerSources.length > 0) {
cleanedMappings.apiMappings.push(api)
}
})
return cleanedMappings
}
/**
* 生成新的文件内容
*/
generateNewContent(cleanedMappings) {
const header = `// 路由映射数据 - 构建时生成
export default ${JSON.stringify(cleanedMappings, null, 2)};`
return header
}
/**
* 统计清理结果
*/
getCleanupStats(originalMappings, cleanedMappings) {
const stats = {
original: {
modules: originalMappings.length,
apis: 0,
triggerSources: 0,
buttonTriggerSources: 0
},
cleaned: {
modules: cleanedMappings.length,
apis: 0,
triggerSources: 0
}
}
// 统计原始数据
originalMappings.forEach(module => {
if (module.apiMappings) {
stats.original.apis += module.apiMappings.length
module.apiMappings.forEach(api => {
if (api.triggerSources) {
stats.original.triggerSources += api.triggerSources.length
stats.original.buttonTriggerSources += api.triggerSources.filter(t => t.triggerType === 'button').length
}
})
}
})
// 统计清理后数据
cleanedMappings.forEach(module => {
if (module.apiMappings) {
stats.cleaned.apis += module.apiMappings.length
module.apiMappings.forEach(api => {
if (api.triggerSources) {
stats.cleaned.triggerSources += api.triggerSources.length
}
})
}
})
return stats
}
}
module.exports = DataCleaner