import { app , dialog } from 'electron' ; import { autoUpdater } from 'electron-updater' ; import { applicationMenu } from '@main/menu' ; export class Updater { protected startup = true ; protected downloading = false ; protected downloaded = false ; constructor ( ) { autoUpdater . autoDownload = false ; autoUpdater . logger = log ; autoUpdater . checkForUpdates ( ) . catch ( error => { log . error ( error ) ; } ) ; autoUpdater . on ( 'checking-for-update' , ( ) => { applicationMenu . setOptions ( { isCheckingForUpdate : true } ) ; } ) ; autoUpdater . on ( 'update-available' , ( info ) => { applicationMenu . setOptions ( { isCheckingForUpdate : false } ) ; if ( this . startup ) { this . startup = false ; this . download ( ) ; return ; } dialog . showMessageBox ( { type : 'info' , message : 'A new version of Lode is available' , detail : `Lode ${ info . version } is now available — you have ${ app . getVersion ( ) }. Would you like to download it now?` , buttons : [ 'Download Update' , 'Cancel' ] } ) . then ( ( { response } ) => { if ( response === 0 ) { this . download ( ) ; } } ) ; } ) ; autoUpdater . on ( 'update-not-available' , ( info ) => { applicationMenu . setOptions ( { isCheckingForUpdate : false } ) ; if ( this . startup ) { this . startup = false ; return ; } dialog . showMessageBox ( { type : 'info' , message : 'You’re up-to-date!' , detail : `Lode ${ app . getVersion ( ) } is currently the newest version available.` , buttons : [ 'OK' ] } ) ; } ) ; autoUpdater . on ( 'error' , ( _ ) => { applicationMenu . setOptions ( { isCheckingForUpdate : false } ) ; if ( this . startup ) { this . startup = false ; return ; } dialog . showErrorBox ( 'Update Failed' , 'An error occurred while attempting to update Lode, please try again. You may be required to update manually by downloading Lode again.' ) ; } ) ; autoUpdater . on ( 'update-downloaded' , ( info ) => { this . downloading = false ; this . downloaded = true ; applicationMenu . setOptions ( { isDownloadingUpdate : false , hasDownloadedUpdate : true } ) ; dialog . showMessageBox ( { type : 'info' , message : 'Ready to Install' , detail : `Lode ${ info . version } has been download and is ready to install. You are required to restart the app.` , buttons : [ 'Install and Relaunch' , 'Cancel' ] } ) . then ( ( { response } ) => { if ( response === 0 ) { autoUpdater . quitAndInstall ( ) ; } } ) ; } ) ; } protected download ( ) { this . downloading = true ; applicationMenu . setOptions ( { isDownloadingUpdate : true } ) ; autoUpdater . downloadUpdate ( ) ; } }