1- import { ipcMain , app } from 'electron'
1+ import { ipcMain , app , autoUpdater as nativeAutoUpdater } from 'electron'
22import type { AppUpdater } from 'electron-updater'
33import electronUpdater from 'electron-updater'
44import log from 'electron-log'
55
6+ let updaterLifecycleLoggingRegistered = false
7+
68export function getAutoUpdater ( ) : AppUpdater {
79 // Using destructuring to access autoUpdater due to the CommonJS module of 'electron-updater'.
810 // It is a workaround for ESM compatibility issues, see https://github.com/electron-userland/electron-builder/issues/7976.
@@ -13,37 +15,81 @@ export function getAutoUpdater(): AppUpdater {
1315}
1416
1517export function initializeAutoUpdater ( ) {
16- getAutoUpdater ( ) . autoDownload = true
17- getAutoUpdater ( ) . autoInstallOnAppQuit = false
18- getAutoUpdater ( ) . allowDowngrade = true
18+ registerUpdaterLifecycleLogging ( )
19+
20+ const updater = getAutoUpdater ( )
21+ updater . autoDownload = true
22+ updater . autoInstallOnAppQuit = false
23+ updater . allowDowngrade = true
24+
25+ log . info (
26+ `[updater] initialized (appVersion=${ app . getVersion ( ) } , isPackaged=${ app . isPackaged } , platform=${ process . platform } )`
27+ )
1928}
2029
2130export function registerAutoUpdateListeners ( mainWindow : Electron . BrowserWindow ) {
31+ const updater = getAutoUpdater ( )
32+
2233 ipcMain . on ( 'updateAutoUpdater' , ( ) => {
23- // force dev update config
24- getAutoUpdater ( ) . checkForUpdatesAndNotify ( )
34+ updater . checkForUpdatesAndNotify ( ) . catch ( ( error ) => {
35+ const message = error instanceof Error ? error . message : String ( error )
36+ log . error ( `[updater] checkForUpdatesAndNotify rejected: ${ message } ` )
37+ } )
2538 } )
2639
27- getAutoUpdater ( ) . addListener ( 'update-available' , ( ) => {
40+ updater . addListener ( 'update-available' , ( info ) => {
41+ log . info ( `[updater] update-available (version=${ info . version } )` )
2842 mainWindow . webContents . send ( 'updateAvailable' )
2943 } )
3044
31- getAutoUpdater ( ) . addListener ( 'update-not-available' , ( ) => {
45+ updater . addListener ( 'update-not-available' , ( ) => {
3246 mainWindow . webContents . send ( 'updateNotAvailable' )
3347 } )
3448
35- getAutoUpdater ( ) . addListener ( 'update-downloaded' , ( ) => {
49+ updater . addListener ( 'update-downloaded' , ( info ) => {
50+ log . info ( `[updater] update-downloaded (version=${ info . version } )` )
3651 mainWindow . webContents . send ( 'updateDownloaded' )
3752 } )
3853
39- getAutoUpdater ( ) . addListener ( 'error' , ( error ) => {
54+ updater . addListener ( 'error' , ( error ) => {
55+ log . error ( `[updater] error: ${ error . message } ` )
4056 mainWindow . webContents . send ( 'updateError' , error . message )
4157 } )
4258
4359 ipcMain . on ( 'installUpdate' , ( ) => {
44- app . emit ( 'before-quit' )
45- setTimeout ( ( ) => {
46- getAutoUpdater ( ) . quitAndInstall ( )
47- } , 500 )
60+ log . info ( `[updater] installUpdate IPC received, calling quitAndInstall()` )
61+ try {
62+ updater . quitAndInstall ( )
63+ } catch ( error ) {
64+ const message = error instanceof Error ? error . message : String ( error )
65+ log . error ( `[updater] quitAndInstall() threw: ${ message } ` )
66+ mainWindow . webContents . send ( 'updateError' , message )
67+ }
68+ } )
69+ }
70+
71+ function registerUpdaterLifecycleLogging ( ) {
72+ if ( updaterLifecycleLoggingRegistered ) {
73+ return
74+ }
75+
76+ updaterLifecycleLoggingRegistered = true
77+
78+ // These four events are the ones that tell us whether Squirrel.Mac is actually
79+ // being reached during an install. Keep them.
80+ nativeAutoUpdater . on ( 'before-quit-for-update' , ( ) => {
81+ log . info ( `[updater] native autoUpdater emitted before-quit-for-update` )
82+ } )
83+
84+ app . on ( 'before-quit' , ( ) => {
85+ log . info ( `[updater] app emitted before-quit` )
86+ } )
87+
88+ app . on ( 'will-quit' , ( ) => {
89+ log . info ( `[updater] app emitted will-quit` )
90+ } )
91+
92+ app . on ( 'quit' , ( _event , exitCode ) => {
93+ log . info ( `[updater] app emitted quit (exitCode=${ exitCode } )` )
4894 } )
4995}
0 commit comments