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.
169 lines
4.4 KiB
169 lines
4.4 KiB
2 days ago
|
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) {
|
||
|
console.error('❌ 清理数据结构失败:', error.message)
|
||
|
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
|
||
|
}
|
||
|
|
||
|
mappings.apiMappings.forEach(module => {
|
||
|
if (!module.apiMappings || module.apiMappings.length === 0) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
const cleanedApiMappings = []
|
||
|
|
||
|
module.apiMappings.forEach(api => {
|
||
|
if (!api.triggerSources || api.triggerSources.length === 0) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 1. 只保留triggerType为button的triggerSources
|
||
|
const buttonTriggerSources = api.triggerSources.filter(trigger => {
|
||
|
return trigger.triggerType === 'button'
|
||
|
})
|
||
|
|
||
|
// 2. 如果还有button类型的triggerSources,保留这个API
|
||
|
if (buttonTriggerSources.length > 0) {
|
||
|
const cleanedApi = {
|
||
|
...api,
|
||
|
triggerSources: buttonTriggerSources
|
||
|
}
|
||
|
cleanedApiMappings.push(cleanedApi)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
// 3. 如果还有apiMappings,保留这个模块
|
||
|
if (cleanedApiMappings.length > 0) {
|
||
|
const cleanedModule = {
|
||
|
...module,
|
||
|
apiMappings: cleanedApiMappings
|
||
|
}
|
||
|
cleanedMappings.apiMappings.push(cleanedModule)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
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
|