2017-05-03 17:48:23 +00:00
|
|
|
import electron from 'electron';
|
|
|
|
import {CHECK_FOR_UPDATES_INTERVAL, getAppVersion, isDevelopment, isLinux} from '../common/constants';
|
|
|
|
|
|
|
|
const {autoUpdater, BrowserWindow} = electron;
|
|
|
|
|
2017-05-05 18:27:08 +00:00
|
|
|
const UPDATE_URLS = isDevelopment() ? {
|
|
|
|
darwin: `http://localhost:8000/builds/check/mac?v=${getAppVersion()}`,
|
|
|
|
linux: `http://localhost:8000/builds/check/linux?v=${getAppVersion()}`,
|
|
|
|
win32: `http://localhost:8000/updates/win?v=${getAppVersion()}`
|
|
|
|
} : {
|
2017-05-03 17:48:23 +00:00
|
|
|
darwin: `https://updates.insomnia.rest/builds/check/mac?v=${getAppVersion()}`,
|
|
|
|
linux: `https://updates.insomnia.rest/builds/check/linux?v=${getAppVersion()}`,
|
2017-05-05 18:27:08 +00:00
|
|
|
win32: `https://updates.insomnia.rest/updates/win?v=${getAppVersion()}`
|
2017-05-03 17:48:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let hasPromptedForUpdates = false;
|
|
|
|
|
|
|
|
export function init () {
|
|
|
|
// Check for updates immediately
|
|
|
|
_checkForUpdates();
|
|
|
|
|
|
|
|
// Check for updates on an interval
|
|
|
|
setInterval(_checkForUpdates, CHECK_FOR_UPDATES_INTERVAL);
|
|
|
|
|
|
|
|
autoUpdater.on('error', e => {
|
2017-05-10 04:20:40 +00:00
|
|
|
console.log(`[updater] Error: ${e.message}`);
|
2017-05-03 17:48:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
autoUpdater.on('update-not-available', () => {
|
2017-05-10 04:20:40 +00:00
|
|
|
console.log('[updater] Not Available --');
|
2017-05-03 17:48:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
autoUpdater.on('update-available', () => {
|
2017-05-10 04:20:40 +00:00
|
|
|
console.log('[updater] Update Available --');
|
2017-05-03 17:48:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
autoUpdater.on('update-downloaded', (e, releaseNotes, releaseName, releaseDate, updateUrl) => {
|
2017-05-10 04:20:40 +00:00
|
|
|
console.log(`[updater] Downloaded ${releaseName} --`);
|
2017-05-03 17:48:23 +00:00
|
|
|
_showUpdateNotification();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _showUpdateNotification () {
|
|
|
|
if (hasPromptedForUpdates) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:27:08 +00:00
|
|
|
const windows = BrowserWindow.getAllWindows();
|
|
|
|
if (windows.length && windows[0].webContents) {
|
|
|
|
windows[0].webContents.send('update-available');
|
2017-05-03 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hasPromptedForUpdates = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _checkForUpdates () {
|
|
|
|
if (hasPromptedForUpdates) {
|
|
|
|
// We've already prompted for updates. Don't bug the user anymore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isLinux()) {
|
|
|
|
try {
|
|
|
|
autoUpdater.setFeedURL(UPDATE_URLS[process.platform]);
|
|
|
|
autoUpdater.checkForUpdates();
|
2017-05-05 18:27:08 +00:00
|
|
|
} catch (err) {
|
2017-05-03 17:48:23 +00:00
|
|
|
// This will fail in development
|
2017-05-10 04:20:40 +00:00
|
|
|
console.warn('[updater] Failed to check for updates:', err.message);
|
2017-05-03 17:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|