11import { app , BrowserWindow } from 'electron'
22import { autoUpdater } from 'electron-updater'
3+ import { existsSync , rmSync , writeFileSync , readFileSync } from 'fs'
4+ import { join } from 'path'
35import { logger } from './logger'
46
57type WindowGetter = ( ) => BrowserWindow | null
68
9+ function pendingFilePath ( ) : string {
10+ return join ( app . getPath ( 'userData' ) , '.last-update-installer' )
11+ }
12+
13+ function cleanupLastInstaller ( ) : void {
14+ const marker = pendingFilePath ( )
15+ if ( ! existsSync ( marker ) ) return
16+ try {
17+ const installerPath = readFileSync ( marker , 'utf-8' ) . trim ( )
18+ if ( existsSync ( installerPath ) ) {
19+ rmSync ( installerPath )
20+ logger . info ( `[updater] Cleaned up installer: ${ installerPath } ` )
21+ }
22+ rmSync ( marker )
23+ } catch { }
24+ }
25+
726export function initAutoUpdater ( getWindow : WindowGetter ) : void {
27+ cleanupLastInstaller ( )
28+
829 // eslint-disable-next-line @typescript-eslint/no-explicit-any
930 autoUpdater . logger = logger as any
1031 autoUpdater . autoDownload = false
@@ -30,6 +51,9 @@ export function initAutoUpdater(getWindow: WindowGetter): void {
3051
3152 autoUpdater . on ( 'update-downloaded' , ( info ) => {
3253 logger . info ( `[updater] Patch update ${ info . version } downloaded — showing badge` )
54+ try {
55+ writeFileSync ( pendingFilePath ( ) , info . downloadedFile , 'utf-8' )
56+ } catch { }
3357 getWindow ( ) ?. webContents . send ( 'updater:patch-ready' , { version : info . version } )
3458 } )
3559
0 commit comments