77
88'use strict' ;
99
10+ const logger = require ( './console-logger' ) ;
1011const { autoUpdater } = require ( 'electron-updater' ) ;
1112const I18NUtils = require ( './i18n-utils' ) ;
13+ autoUpdater . autoDownload = false ;
14+ autoUpdater . allowDowngrade = true ;
1215const { dialog } = require ( 'electron' ) ;
1316
17+ let failed ;
1418let _updateInfo ;
1519let _window ;
20+ let downloadingDialogAbortController ;
21+
22+ autoUpdater . on ( 'error' , error => {
23+ failed = true ;
24+ if ( downloadingDialogAbortController ) {
25+ downloadingDialogAbortController . abort ( ) ;
26+ // This is a little odd. We have to delay this slightly to allow the
27+ // abort to close the "downloading" dialog before showing the "quit"
28+ // dialog. electron doesn't allow the abort to work after the second
29+ // dialog is started.
30+ setTimeout ( ( ) => showError ( error ) , 100 ) ;
31+ }
32+ else
33+ showError ( error ) ;
34+ } ) ;
1635
1736( ( ) => {
18- autoUpdater . autoDownload = false ;
19- autoUpdater . allowDowngrade = true ;
2037 autoUpdater . on ( 'update-downloaded' , ( ) => {
21- dialog . showMessageBox (
22- _window ,
23- {
24- title : `${ I18NUtils . get ( 'wrc-electron.dialog.help.checkForUpdates.restartNow.message' , getVersion ( ) ) } ` ,
25- message : `${ I18NUtils . get ( 'wrc-electron.dialog.help.checkForUpdates.updateDownloaded.message' ) } ` ,
26- buttons : [ `${ I18NUtils . get ( 'wrc-common.buttons.restart.label' ) } ` , `${ I18NUtils . get ( 'wrc-common.buttons.cancel.label' ) } ` ] ,
27- type : 'info'
38+ if ( ! failed ) {
39+ if ( downloadingDialogAbortController ) {
40+ downloadingDialogAbortController . abort ( ) ;
41+ // This is a little odd. We have to delay this slightly to allow the
42+ // abort to close the "downloading" dialog before showing the "quit"
43+ // dialog. electron doesn't allow the abort to work after the second
44+ // dialog is started.
45+ setTimeout ( ( ) => showQuitDialog ( ) , 100 ) ;
46+ }
47+ else {
48+ showQuitDialog ( ) ;
2849 }
29- ) . then ( ( choice ) => {
30- if ( choice . response === 0 )
31- autoUpdater . quitAndInstall ( ) ;
32- } ) ;
50+ }
3351 } ) ;
3452} ) ( ) ;
3553
54+ const log = require ( 'electron-log' ) ;
55+
56+ // We don't need the logging in a second file. We already have "console.log"
57+ // going to a file.
58+ log . transports . file . level = false ;
59+
60+ // Make it match our logger (mostly)
61+ log . transports . console . format = '{y}.{m}.{d} {h}:{i}:{s}.{ms} [{level}] {text}' ;
62+
63+ // Perhaps we'll use the same level for the auto updater as the rest, as below,
64+ // but for now, let's leave logging level at the default (which is "silly") for
65+ // now. A little extra logging couldn't hurt in this tricky area of the system.
66+ // log.transports.console.level = logger.getLoggingLevel();
67+
68+ autoUpdater . logger = log ;
69+
70+ function showError ( error ) {
71+ dialog . showMessageBox (
72+ _window ,
73+ {
74+ title : `${ I18NUtils . get ( 'wrc-electron.menus.updates.downloadFailed.title' ) } ` ,
75+ buttons : [ `${ I18NUtils . get ( 'wrc-common.buttons.ok.label' ) } ` ] ,
76+ type : 'info' ,
77+ message : `${ I18NUtils . get ( 'wrc-electron.menus.updates.downloadFailed.message' , error ) } `
78+ }
79+ ) ;
80+ }
81+
82+ function showQuitDialog ( ) {
83+ dialog . showMessageBox (
84+ _window ,
85+ {
86+ title : `${ I18NUtils . get ( 'wrc-electron.dialog.help.checkForUpdates.restartNow.message' , getVersion ( ) ) } ` ,
87+ message : `${ I18NUtils . get ( 'wrc-electron.dialog.help.checkForUpdates.updateDownloaded.message' ) } ` ,
88+ buttons : [ `${ I18NUtils . get ( 'wrc-common.buttons.restart.label' ) } ` , `${ I18NUtils . get ( 'wrc-common.buttons.cancel.label' ) } ` ] ,
89+ type : 'info'
90+ }
91+ ) . then ( ( choice ) => {
92+ if ( choice . response === 0 ) {
93+ // There is an issue on the Mac with quitAndInstall inside the "on"
94+ setTimeout ( ( ) => autoUpdater . quitAndInstall ( ) , 100 ) ;
95+ }
96+ } ) ;
97+ }
98+
3699autoUpdater . on ( 'update-available' , ( info ) => {
37100 _updateInfo = info ;
38101} ) ;
@@ -48,18 +111,22 @@ function getVersion() {
48111
49112async function doUpdate ( window ) {
50113 _window = window ;
114+ downloadingDialogAbortController = new AbortController ( ) ;
115+ dialog . showMessageBox (
116+ _window ,
117+ {
118+ title : `${ I18NUtils . get ( 'wrc-electron.menus.updates.downloadStarted.title' ) } ` ,
119+ buttons : [ `${ I18NUtils . get ( 'wrc-common.buttons.ok.label' ) } ` ] ,
120+ type : 'info' ,
121+ signal : downloadingDialogAbortController . signal ,
122+ message : `${ I18NUtils . get ( 'wrc-electron.menus.updates.downloadStarted.message' ) } `
123+ } ,
124+ ) ;
125+ setTimeout ( ( ) => { downloadingDialogAbortController . abort ( ) ; downloadingDialogAbortController = null ; } , 3000 ) ;
51126 try {
52127 await autoUpdater . downloadUpdate ( ) ;
53128 } catch ( error ) {
54- dialog . showMessageBox (
55- _window ,
56- {
57- title : `${ I18NUtils . get ( 'wrc-electron.menus.updates.downloadFailed.title' ) } ` ,
58- buttons : [ `${ I18NUtils . get ( 'wrc-common.buttons.ok.label' ) } ` ] ,
59- type : 'info' ,
60- message : `${ I18NUtils . get ( 'wrc-electron.menus.updates.downloadFailed.message' , error ) } `
61- }
62- ) ;
129+ showError ( error ) ;
63130 }
64131}
65132
0 commit comments