Tweak auto-updater flow

This commit is contained in:
Yuya Ochiai
2017-09-07 23:23:46 +09:00
parent 9d31066136
commit fb47c287d9
3 changed files with 59 additions and 29 deletions

View File

@@ -652,14 +652,22 @@ app.on('ready', () => {
session.defaultSession.setPermissionRequestHandler(permissionRequestHandler(mainWindow, permissionManager)); session.defaultSession.setPermissionRequestHandler(permissionRequestHandler(mainWindow, permissionManager));
autoUpdater.initialize(appState, mainWindow); autoUpdater.initialize(appState, mainWindow);
ipcMain.on('check-for-updates', () => { ipcMain.on('check-for-updates', (isManual) => {
if (global.isDev) { if (global.isDev) {
console.log('Development mode: Skip checking for updates'); console.log('Development mode: Skip checking for updates');
} else { } else {
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates(isManual);
}
});
mainWindow.once('show', () => {
if (autoUpdater.shouldCheckForUpdatesOnStart(appState.updateCheckedDate)) {
ipcMain.emit('check-for-updates');
} else {
setTimeout(() => {
ipcMain.emit('check-for-updates');
}, autoUpdater.INTERVAL_48_HOURS_IN_MS);
} }
}); });
ipcMain.emit('check-for-updates');
// Open the DevTools. // Open the DevTools.
// mainWindow.openDevTools(); // mainWindow.openDevTools();

View File

@@ -3,7 +3,7 @@ const path = require('path');
const {autoUpdater} = require('electron-updater'); const {autoUpdater} = require('electron-updater');
const semver = require('semver'); const semver = require('semver');
const interval48hours = 172800000; // 48 * 60 * 60 * 1000 [ms] const INTERVAL_48_HOURS_IN_MS = 172800000; // 48 * 60 * 60 * 1000 [ms]
let updaterWindow = null; let updaterWindow = null;
@@ -46,26 +46,34 @@ function createUpdaterWindow(options) {
return win; return win;
} }
function isUpdateApplicable(appState, updateInfo) { function isUpdateApplicable(now, skippedVersion, updateInfo) {
const checkedTime = appState.updateCheckedDate.getTime();
const releaseTime = new Date(updateInfo.releaseDate).getTime(); const releaseTime = new Date(updateInfo.releaseDate).getTime();
if (checkedTime - releaseTime < interval48hours) {
// 48 hours after a new version is added to releases.mattermost.com, user receives a “New update is available” dialog
if (now.getTime() - releaseTime < INTERVAL_48_HOURS_IN_MS) {
return false; return false;
} }
if (appState.skippedVersion === null) {
return true; // If a version was skipped, compare version.
if (skippedVersion) {
return semver.gt(updateInfo.version, skippedVersion);
} }
return semver.gt(updateInfo.version, appState.skippedVersion);
return true;
}
function downloadAndInstall() {
autoUpdater.downloadUpdate().then(() => {
autoUpdater.quitAndInstall();
});
} }
function initialize(appState, mainWindow) { function initialize(appState, mainWindow) {
const assetsDir = path.resolve(app.getAppPath(), 'assets'); const assetsDir = path.resolve(app.getAppPath(), 'assets');
autoUpdater.on('error', (err) => { autoUpdater.on('error', (err) => {
console.error('Error in autoUpdater:', err.message); console.error('Error in autoUpdater:', err.message);
}).on('checking-for-update', () => {
appState.updateCheckedDate = new Date();
}).on('update-available', (info) => { }).on('update-available', (info) => {
if (isUpdateApplicable(appState, info)) { if (isUpdateApplicable(new Date(), appState.skippedVersion, info)) {
updaterWindow = createUpdaterWindow({linuxAppIcon: path.join(assetsDir, 'appicon.png'), nextVersion: '0.0.0'}); updaterWindow = createUpdaterWindow({linuxAppIcon: path.join(assetsDir, 'appicon.png'), nextVersion: '0.0.0'});
updaterWindow.on('close', () => { updaterWindow.on('close', () => {
updaterWindow = null; updaterWindow = null;
@@ -74,33 +82,52 @@ function initialize(appState, mainWindow) {
appState.skippedVersion = info.version; appState.skippedVersion = info.version;
updaterWindow.close(); updaterWindow.close();
}).on('click-remind', () => { }).on('click-remind', () => {
setTimeout(autoUpdater.checkForUpdates, interval48hours); appState.updateCheckedDate = new Date();
setTimeout(autoUpdater.checkForUpdates, INTERVAL_48_HOURS_IN_MS);
updaterWindow.close(); updaterWindow.close();
}).on('click-install', () => { }).on('click-install', () => {
autoUpdater.quitAndInstall(); downloadAndInstall();
updaterWindow.close(); updaterWindow.close();
}).on('click-release-notes', () => { }).on('click-release-notes', () => {
shell.openExternal(`https://github.com/mattermost/desktop/releases/v${info.version}`); shell.openExternal(`https://github.com/mattermost/desktop/releases/v${info.version}`);
}); });
updaterWindow.focus();
} else if (autoUpdater.isManual) {
autoUpdater.emit('update-not-available');
} }
}).on('update-not-available', () => { }).on('update-not-available', () => {
dialog.showMessageBox(mainWindow, { if (autoUpdater.isManual) {
type: 'info', dialog.showMessageBox(mainWindow, {
buttons: ['Close'], type: 'info',
title: 'Your Desktop App is up to date', buttons: ['Close'],
message: 'You have the latest version of the Mattermost Desktop App.' title: 'Your Desktop App is up to date',
}, () => {}); // eslint-disable-line no-empty-function message: 'You have the latest version of the Mattermost Desktop App.'
setTimeout(autoUpdater.checkForUpdates, interval48hours); }, () => {}); // eslint-disable-line no-empty-function
}
setTimeout(autoUpdater.checkForUpdates, INTERVAL_48_HOURS_IN_MS);
}); });
} }
function checkForUpdates() { function shouldCheckForUpdatesOnStart(updateCheckedDate) {
if (updateCheckedDate) {
if (Date.now() - updateCheckedDate < INTERVAL_48_HOURS_IN_MS) {
return false;
}
}
return true;
}
function checkForUpdates(isManual = false) {
autoUpdater.isManual = isManual;
autoUpdater.autoDownload = false;
if (!updaterWindow) { if (!updaterWindow) {
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates();
} }
} }
module.exports = { module.exports = {
INTERVAL_48_HOURS_IN_MS,
checkForUpdates, checkForUpdates,
shouldCheckForUpdatesOnStart,
initialize initialize
}; };

View File

@@ -230,15 +230,10 @@ function createTemplate(mainWindow, config, isDev) {
submenu.push({ submenu.push({
label: `Version ${app.getVersion()}`, label: `Version ${app.getVersion()}`,
enabled: false, enabled: false,
}, {
type: 'separator',
}, {
label: `Version ${app.getVersion()}`,
enabled: false,
}, { }, {
label: 'Check for Updates...', label: 'Check for Updates...',
click() { click() {
ipcMain.emit('check-for-updates'); ipcMain.emit('check-for-updates', true);
}, },
}); });
template.push({label: '&Help', submenu}); template.push({label: '&Help', submenu});