* 扫描应用并缓存到数据库
()
| 218 | * 扫描应用并缓存到数据库 |
| 219 | */ |
| 220 | private async scanAndCacheApps(): Promise<any[]> { |
| 221 | const apps = await scanApplications() |
| 222 | console.log(`扫描到 ${apps.length} 个应用`) |
| 223 | |
| 224 | // Windows 平台:获取 UWP 应用并合并 |
| 225 | if (process.platform === 'win32') { |
| 226 | try { |
| 227 | const uwpApps = UwpManager.getUwpApps() |
| 228 | console.log(`获取到 ${uwpApps.length} 个 UWP 应用`) |
| 229 | |
| 230 | // 将 UWP 应用转换为 Command 格式,使用 uwp: 前缀标识 |
| 231 | for (const uwpApp of uwpApps) { |
| 232 | const uwpPath = `uwp:${uwpApp.appId}` |
| 233 | // 按 name|path 组合去重,与 windowsScanner.deduplicateCommands 策略一致 |
| 234 | const dedupeKey = `${uwpApp.name.toLowerCase()}|${uwpPath.toLowerCase()}` |
| 235 | const isDuplicate = apps.some( |
| 236 | (a) => `${a.name.toLowerCase()}|${a.path.toLowerCase()}` === dedupeKey |
| 237 | ) |
| 238 | if (isDuplicate) continue |
| 239 | |
| 240 | apps.push({ |
| 241 | name: uwpApp.name, |
| 242 | path: uwpPath, |
| 243 | icon: uwpApp.icon || '' |
| 244 | }) |
| 245 | } |
| 246 | console.log(`合并 UWP 后共 ${apps.length} 个应用`) |
| 247 | } catch (error) { |
| 248 | console.error('[Commands] 获取 UWP 应用失败:', error) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // 注意:windowsScanner 已经在扫描时生成了 ztools-icon:// 协议 URL |
| 253 | // 不需要再进行图标提取或文件转换,直接使用扫描结果即可 |
| 254 | |
| 255 | // 保存到数据库缓存 |
| 256 | try { |
| 257 | databaseAPI.dbPut('cached-commands', apps) |
| 258 | databaseAPI.dbPut(AppsAPI.APP_CACHE_VERSION_KEY, AppsAPI.APP_CACHE_VERSION) |
| 259 | console.log('[Commands] 应用列表已缓存到数据库') |
| 260 | } catch (error) { |
| 261 | console.error('[Commands] 缓存应用列表失败:', error) |
| 262 | } |
| 263 | |
| 264 | return apps |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * 刷新应用缓存(当检测到应用文件夹变化时调用) |
no test coverage detected