()
| 54 | } |
| 55 | |
| 56 | private setupIPC(): void { |
| 57 | // 获取动态 features |
| 58 | ipcMain.on('get-features', (event, codes?: string[]) => { |
| 59 | try { |
| 60 | const pluginRuntimeContext = this.getPluginRuntimeContext(event) |
| 61 | if (!pluginRuntimeContext) { |
| 62 | event.returnValue = [] |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | const features = this.loadDynamicFeatures(pluginRuntimeContext.pluginName) |
| 67 | |
| 68 | // 如果指定了 codes,只返回匹配的 features |
| 69 | if (codes && Array.isArray(codes)) { |
| 70 | const filtered = features.filter((f) => codes.includes(f.code)) |
| 71 | event.returnValue = filtered |
| 72 | } else { |
| 73 | event.returnValue = features |
| 74 | } |
| 75 | } catch (error) { |
| 76 | console.error('[PluginFeature] get-features error:', error) |
| 77 | event.returnValue = [] |
| 78 | } |
| 79 | }) |
| 80 | |
| 81 | // 设置动态 feature |
| 82 | ipcMain.on('set-feature', (event, feature: DynamicFeature) => { |
| 83 | try { |
| 84 | console.log('[PluginFeature] set-feature', feature) |
| 85 | const pluginRuntimeContext = this.getPluginRuntimeContext(event) |
| 86 | if (!pluginRuntimeContext) { |
| 87 | event.returnValue = { success: false, error: 'Plugin not found' } |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | // 验证 feature 结构 |
| 92 | if (!feature.code || !feature.cmds || !Array.isArray(feature.cmds)) { |
| 93 | event.returnValue = { success: false, error: 'Invalid feature structure' } |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | // 加载现有的动态 features |
| 98 | const features = this.loadDynamicFeatures(pluginRuntimeContext.pluginName) |
| 99 | |
| 100 | // 查找是否已存在该 code |
| 101 | const existingIndex = features.findIndex((f) => f.code === feature.code) |
| 102 | |
| 103 | if (existingIndex >= 0) { |
| 104 | // 更新现有 feature |
| 105 | features[existingIndex] = feature |
| 106 | } else { |
| 107 | // 添加新 feature |
| 108 | features.push(feature) |
| 109 | } |
| 110 | |
| 111 | // 保存到数据库 |
| 112 | this.saveDynamicFeatures(pluginRuntimeContext.pluginName, features) |
| 113 |
no test coverage detected