* 下载并解压更新包
(updateInfo: any)
| 205 | * 下载并解压更新包 |
| 206 | */ |
| 207 | private async downloadAndExtractUpdate(updateInfo: any): Promise<any> { |
| 208 | try { |
| 209 | // 1. 获取下载 URL(已经在 checkUpdate 中构建好了) |
| 210 | const downloadUrl = this.selectDownloadUrl(updateInfo) |
| 211 | |
| 212 | console.log('[Updater] 下载更新包:', downloadUrl) |
| 213 | |
| 214 | // 2. 下载更新包 |
| 215 | const tempDir = path.join(app.getPath('userData'), 'ztools-update-pkg') |
| 216 | await fs.mkdir(tempDir, { recursive: true }) |
| 217 | const tempZipPath = path.join(tempDir, `update-${Date.now()}.zip`) |
| 218 | const extractPath = path.join(tempDir, `extracted-${Date.now()}`) |
| 219 | |
| 220 | await downloadFile(downloadUrl, tempZipPath) |
| 221 | |
| 222 | // 3. 解压 |
| 223 | console.log('[Updater] 解压更新包...') |
| 224 | await fs.mkdir(extractPath, { recursive: true }) |
| 225 | |
| 226 | const zip = new AdmZip(tempZipPath) |
| 227 | await new Promise<void>((resolve, reject) => { |
| 228 | zip.extractAllToAsync(extractPath, true, false, (error?: Error) => { |
| 229 | if (error) { |
| 230 | reject(error) |
| 231 | } else { |
| 232 | resolve() |
| 233 | } |
| 234 | }) |
| 235 | }) |
| 236 | |
| 237 | // 4. 重命名 app.asar.tmp -> app.asar(如果存在) |
| 238 | const appAsarTmp = path.join(extractPath, 'app.asar.tmp') |
| 239 | const appAsar = path.join(extractPath, 'app.asar') |
| 240 | try { |
| 241 | await fs.access(appAsarTmp) |
| 242 | await fs.rename(appAsarTmp, appAsar) |
| 243 | console.log('[Updater] 成功重命名: app.asar.tmp -> app.asar') |
| 244 | } catch { |
| 245 | console.log('[Updater] 未找到 app.asar.tmp,可能直接是 app.asar') |
| 246 | } |
| 247 | |
| 248 | // 5. 删除 zip 文件节省空间 |
| 249 | try { |
| 250 | await fs.unlink(tempZipPath) |
| 251 | } catch (e) { |
| 252 | console.error('[Updater] 删除 zip 文件失败:', e) |
| 253 | } |
| 254 | |
| 255 | return { success: true, extractPath } |
| 256 | } catch (error: unknown) { |
| 257 | console.error('[Updater] 下载更新失败:', error) |
| 258 | return { success: false, error: error instanceof Error ? error.message : '未知错误' } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * 获取更新路径配置 |
no test coverage detected