signal-desktop/autoupdate.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

const autoUpdater = require('electron-updater').autoUpdater
const { dialog } = require('electron');
const hour = 60 * 60;
const autoUpdaterInterval = hour * 1000;
const RESTART_BUTTON = 0;
const LATER_BUTTON = 1;
function autoUpdateDisabled(config) {
return process.mas || config.get('disableAutoUpdate');
}
function checkForUpdates() {
autoUpdater.checkForUpdates();
}
function showUpdateDialog(localeMessages) {
return function() {
const options = {
type: 'info',
buttons: [
localeMessages.autoUpdateRestartButtonLabel.message,
localeMessages.autoUpdateLaterButtonLabel.message
],
title: localeMessages.autoUpdateNewVersionTitle.message,
message: localeMessages.autoUpdateNewVersionMessage.message,
detail: localeMessages.autoUpdateNewVersionInstructions.message,
defaultId: RESTART_BUTTON,
cancelId: LATER_BUTTON
}
dialog.showMessageBox(options, function(response) {
if (response == RESTART_BUTTON) {
autoUpdater.quitAndInstall();
}
});
}
}
function onError(error) {
console.log("Got an error while updating: ", error.stack);
}
function initializeAutoUpdater(config, localeMessages) {
if (autoUpdateDisabled(config)) {
return;
}
const onUpdateDownloaded = showUpdateDialog(localeMessages);
autoUpdater.addListener('update-downloaded', onUpdateDownloaded);
autoUpdater.addListener('error', onError);
checkForUpdates();
setInterval(checkForUpdates, autoUpdaterInterval);
}
module.exports = {
initializeAutoUpdater
};