2018-01-26 14:53:51 -05:00
import { app , dialog , BrowserWindow } from 'electron' ;
import { autoUpdater , UpdateInfo } from 'electron-updater' ;
2018-01-22 18:38:06 -05:00
import TEST_RELEASE from './testrelease.json' ;
autoUpdater . autoDownload = false ;
2018-01-26 14:53:51 -05:00
// Set to 'true' if you want to test update behavior. Requires a recompile.
const shouldMockUpdate = false && process . env . NODE_ENV !== 'production' ;
const shouldMockUpdateError = false && process . env . NODE_ENV !== 'production' ;
let hasRunUpdater = false ;
let hasStartedUpdating = false ;
2018-01-22 18:38:06 -05:00
enum AutoUpdaterEvents {
CHECKING_FOR_UPDATE = 'checking-for-update' ,
UPDATE_AVAILABLE = 'update-available' ,
DOWNLOAD_PROGRESS = 'download-progress' ,
UPDATE_DOWNLOADED = 'update-downloaded' ,
ERROR = 'error'
}
2018-01-26 14:53:51 -05:00
export default function ( mainWindow : BrowserWindow ) {
if ( hasRunUpdater ) {
return ;
}
hasRunUpdater = true ;
autoUpdater . on ( AutoUpdaterEvents . UPDATE_AVAILABLE , ( info : UpdateInfo ) = > {
dialog . showMessageBox (
{
type : 'question' ,
buttons : [ 'Yes, start downloading' , 'Maybe later' ] ,
title : ` An Update is Available (v ${ info . version } ) ` ,
message : ` An Update is Available (v ${ info . version } ) ` ,
detail :
'A new version has been released. Would you like to start downloading the update? You will be notified when the download is finished.'
} ,
response = > {
if ( response === 0 ) {
if ( shouldMockUpdate ) {
mockDownload ( ) ;
} else {
autoUpdater . downloadUpdate ( ) ;
}
}
}
) ;
hasStartedUpdating = true ;
2018-01-22 18:38:06 -05:00
} ) ;
autoUpdater . on ( AutoUpdaterEvents . DOWNLOAD_PROGRESS , progress = > {
2018-01-26 14:53:51 -05:00
mainWindow . setTitle ( ` MyEtherWallet (Downloading update... ${ Math . round ( progress . percent ) } %) ` ) ;
mainWindow . setProgressBar ( progress . percent / 100 ) ;
2018-01-22 18:38:06 -05:00
} ) ;
autoUpdater . on ( AutoUpdaterEvents . UPDATE_DOWNLOADED , ( ) = > {
2018-01-26 14:53:51 -05:00
resetWindowFromUpdates ( mainWindow ) ;
dialog . showMessageBox (
{
type : 'question' ,
buttons : [ 'Yes, restart now' , 'Maybe later' ] ,
title : 'Update Has Been Downloaded' ,
message : 'Download complete!' ,
detail :
'The new version of MyEtherWallet has finished downloading. Would you like to restart to complete the installation?'
} ,
response = > {
if ( response === 0 ) {
if ( shouldMockUpdate ) {
app . quit ( ) ;
} else {
autoUpdater . quitAndInstall ( ) ;
}
}
}
) ;
2018-01-22 18:38:06 -05:00
} ) ;
2018-01-26 14:53:51 -05:00
autoUpdater . on ( AutoUpdaterEvents . ERROR , ( err : Error ) = > {
2018-01-22 18:38:06 -05:00
console . error ( 'Update failed with an error' ) ;
console . error ( err ) ;
2018-01-26 14:53:51 -05:00
// If they haven't started updating yet, just fail silently
if ( ! hasStartedUpdating ) {
return ;
2018-01-22 18:38:06 -05:00
}
2018-01-26 14:53:51 -05:00
resetWindowFromUpdates ( mainWindow ) ;
dialog . showErrorBox (
'Downloading Update has Failed' ,
` The update could not be downloaded. Restart the app and try again later, or manually install the new update at https://github.com/MyEtherWallet/MyEtherWallet/releases \ n \ n( ${
err . name
} : $ { err . message } ) `
) ;
2018-01-22 18:38:06 -05:00
} ) ;
2018-01-26 14:53:51 -05:00
// Kick off the check
autoUpdater . checkForUpdatesAndNotify ( ) ;
2018-01-22 18:38:06 -05:00
// Simulate a test release
if ( shouldMockUpdate ) {
2018-01-26 14:53:51 -05:00
mockUpdateCheck ( ) ;
2018-01-22 18:38:06 -05:00
}
2018-01-26 14:53:51 -05:00
}
function resetWindowFromUpdates ( window : BrowserWindow ) {
window . setTitle ( 'MyEtherWallet' ) ;
window . setProgressBar ( - 1 ) ; // Clears progress bar
}
2018-01-22 18:38:06 -05:00
// Mock functions for dev testing
2018-01-26 14:53:51 -05:00
function mockUpdateCheck() {
autoUpdater . emit ( AutoUpdaterEvents . CHECKING_FOR_UPDATE ) ;
2018-01-22 18:38:06 -05:00
setTimeout ( ( ) = > {
2018-01-26 14:53:51 -05:00
autoUpdater . emit ( AutoUpdaterEvents . UPDATE_AVAILABLE , TEST_RELEASE ) ;
2018-01-22 18:38:06 -05:00
} , 3000 ) ;
}
2018-01-26 14:53:51 -05:00
function mockDownload() {
for ( let i = 0 ; i < 11 ; i ++ ) {
2018-01-22 18:38:06 -05:00
setTimeout ( ( ) = > {
2018-01-26 14:53:51 -05:00
if ( i >= 5 && shouldMockUpdateError ) {
if ( i === 5 ) {
autoUpdater . emit (
AutoUpdaterEvents . ERROR ,
new Error ( 'Test error, nothing actually failed' )
) ;
}
return ;
}
2018-01-22 18:38:06 -05:00
const total = 150000000 ;
2018-01-26 14:53:51 -05:00
autoUpdater . emit ( AutoUpdaterEvents . DOWNLOAD_PROGRESS , {
bytesPerSecond : Math.round ( Math . random ( ) * 100000000 ) ,
percent : i * 10 ,
2018-01-22 18:38:06 -05:00
transferred : total / i ,
total
} ) ;
2018-01-26 14:53:51 -05:00
if ( i === 10 ) {
autoUpdater . emit ( AutoUpdaterEvents . UPDATE_DOWNLOADED ) ;
2018-01-22 18:38:06 -05:00
}
2018-01-26 14:53:51 -05:00
} , 500 * i ) ;
2018-01-22 18:38:06 -05:00
}
}