Created
May 24, 2019 04:19
-
-
Save sarfarazansari/c2ad6cb832c6e75e7e19a89c1bb736ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { autoUpdater } from 'electron-updater'; | |
import { dialog, webContents, app, ipcMain, BrowserWindow } from 'electron'; | |
import { ProgressInfo } from 'builder-util-runtime'; | |
import * as log from 'electron-log'; | |
export default class AppUpdater { | |
constructor() { | |
log.transports.file.level = 'debug'; | |
autoUpdater.logger = log; | |
if (process.platform === 'darwin' || process.platform === 'win32') { | |
autoUpdater.checkForUpdatesAndNotify(); | |
log.info('initiated app autoUpdater'); | |
} | |
} | |
} | |
export function checkForUpdates(menuItem, focusedWindow, event) { | |
if (process.platform === 'darwin' || process.platform === 'win32') { | |
autoUpdater.checkForUpdatesAndNotify(); | |
} | |
} | |
function sendStatusToWindow(type: string, e?: ProgressInfo) { | |
let windows = BrowserWindow.getAllWindows(); | |
if (windows) { | |
let window = windows[0]; | |
if (window) { | |
window.webContents.send(type, (e) ? e : { msg: 'am good' }); | |
} | |
} | |
} | |
app.once('ready', () => { | |
setTimeout(() => { | |
log.info(`current app version is: ${app.getVersion()} from AppUpdater.`); | |
}, 20000); | |
// initiate again for download | |
ipcMain.on('download-update-triggger', event => { | |
log.info('bingo we are ready!'); | |
if (process.platform === 'darwin' || process.platform === 'win32') { | |
autoUpdater.checkForUpdatesAndNotify(); | |
} | |
}); | |
// initiate restart | |
ipcMain.on('quit-install-triggger', event => { | |
try { | |
autoUpdater.quitAndInstall(); | |
} catch (e) { | |
log.error(e); | |
} | |
}); | |
// error case | |
autoUpdater.on('error', (e: any) => { | |
log.info('err while updating:', e); | |
}); | |
// progress case | |
autoUpdater.on('checking-for-update', (e: any) => { | |
log.info('checking-for-update', e); | |
}); | |
autoUpdater.on('update-available', (e: any) => { | |
log.info('update-available', e); | |
if (process.platform === 'darwin' || process.platform === 'win32') { | |
autoUpdater.downloadUpdate(); | |
} | |
}); | |
autoUpdater.on('update-not-available', (e: any) => { | |
log.info('update not available', e); | |
}); | |
autoUpdater.on('update-downloaded', (event: any) => { | |
log.info('update-downloaded:', event); | |
if (event) { | |
const latest: string = (event && event.version) ? event.version : 'latest'; | |
dialog.showMessageBox({ | |
title: 'Install Updates', | |
type: 'info', | |
buttons: ['Restart', 'Later'], | |
message: `Restart your app to get it\'s ${latest} version.` | |
}, (response: number) => { | |
log.info('showMessageBox:', response); | |
if (response === 0) { | |
autoUpdater.quitAndInstall(); | |
} else if (response === 1) { | |
sendStatusToWindow('WILL_RESTART_LATER'); | |
} | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment