1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Electron: Improved auto-update process to avoid random crashes

This commit is contained in:
Laurent Cozic 2018-01-30 22:35:50 +00:00
parent 9b8f520b9f
commit 9a41b9e192
4 changed files with 83 additions and 1 deletions

View File

@ -43,6 +43,7 @@ class Application extends BaseApplication {
constructor() { constructor() {
super(); super();
this.lastMenuScreen_ = null; this.lastMenuScreen_ = null;
this.checkForUpdateLoggerPath_ = Setting.value('profileDir') + '/log-autoupdater.txt';
} }
hasGui() { hasGui() {
@ -294,6 +295,11 @@ class Application extends BaseApplication {
label: _('Website and documentation'), label: _('Website and documentation'),
accelerator: 'F1', accelerator: 'F1',
click () { bridge().openExternal('http://joplin.cozic.net') } click () { bridge().openExternal('http://joplin.cozic.net') }
}, {
label: _('Check for updates...'),
click: () => {
bridge().checkForUpdates(false, this.checkForUpdateLoggerPath_);
}
}, { }, {
label: _('About Joplin'), label: _('About Joplin'),
click: () => { click: () => {
@ -387,7 +393,7 @@ class Application extends BaseApplication {
if (shim.isWindows() || shim.isMac()) { if (shim.isWindows() || shim.isMac()) {
const runAutoUpdateCheck = function() { const runAutoUpdateCheck = function() {
if (Setting.value('autoUpdateEnabled')) { if (Setting.value('autoUpdateEnabled')) {
bridge().checkForUpdatesAndNotify(Setting.value('profileDir') + '/log-autoupdater.txt'); bridge().checkForUpdates(true, this.checkForUpdateLoggerPath_);
} }
} }

View File

@ -125,6 +125,11 @@ class Bridge {
} }
} }
checkForUpdates(inBackground, logFilePath) {
const { checkForUpdates } = require('./checkForUpdates.js');
checkForUpdates(inBackground, logFilePath);
}
} }
let bridge_ = null; let bridge_ = null;

View File

@ -0,0 +1,67 @@
const { dialog } = require('electron')
const { autoUpdater } = require('electron-updater')
const { Logger } = require('lib/logger.js');
const { _ } = require('lib/locale.js');
let autoUpdateLogger_ = new Logger();
let checkInBackground_ = false;
autoUpdater.autoDownload = false;
autoUpdater.on('error', (error) => {
autoUpdateLogger_.error(error);
if (checkInBackground_) return;
dialog.showErrorBox(_('Error'), error == null ? "unknown" : (error.stack || error).toString())
})
autoUpdater.on('update-available', () => {
dialog.showMessageBox({
type: 'info',
message: _('An update is available, do you want to update now?'),
buttons: ['Sure', 'No']
}, (buttonIndex) => {
if (buttonIndex === 0) {
try {
autoUpdater.downloadUpdate()
} catch (error) {
autoUpdateLogger_.error(error);
dialog.showErrorBox(_('Error'), _('Could not download the update: %s', error.message));
}
}
})
})
autoUpdater.on('update-not-available', () => {
if (checkInBackground_) return;
dialog.showMessageBox({ message: _('Current version is up-to-date.') })
})
autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({ message: _('New version downloaded - application will quit now and update...') }, () => {
setTimeout(() => {
try {
autoUpdater.quitAndInstall();
} catch (error) {
autoUpdateLogger_.error(error);
dialog.showErrorBox(_('Error'), _('Could not install the update: %s', error.message));
}
}, 100);
})
})
function checkForUpdates(inBackground, logFilePath) {
if (logFilePath && !autoUpdateLogger_.targets().length) {
autoUpdateLogger_ = new Logger();
autoUpdateLogger_.addTarget('file', { path: logFilePath });
autoUpdateLogger_.setLevel(Logger.LEVEL_DEBUG);
autoUpdateLogger_.info('checkForUpdates: Initializing...');
autoUpdater.logger = autoUpdateLogger_;
}
checkInBackground_ = inBackground;
autoUpdater.checkForUpdates()
}
module.exports.checkForUpdates = checkForUpdates

View File

@ -25,6 +25,10 @@ class Logger {
return this.level_; return this.level_;
} }
targets() {
return this.targets_;
}
clearTargets() { clearTargets() {
this.targets_.clear(); this.targets_.clear();
} }