insomnia/packages/insomnia-app/app/main/updates.js

151 lines
3.8 KiB
JavaScript
Raw Normal View History

// @flow
import electron from 'electron';
2018-06-25 17:42:50 +00:00
import {
CHECK_FOR_UPDATES_INTERVAL,
getAppVersion,
2019-09-18 19:07:09 +00:00
getAppId,
2018-06-25 17:42:50 +00:00
isDevelopment,
UPDATE_URL_MAC,
UPDATE_URL_WINDOWS,
2018-06-25 17:42:50 +00:00
} from '../common/constants';
import * as models from '../models/index';
2018-10-17 16:42:33 +00:00
import { buildQueryStringFromParams, joinUrlAndQueryString } from 'insomnia-url';
2018-06-25 17:42:50 +00:00
import { delay } from '../common/misc';
2018-06-25 17:42:50 +00:00
const { autoUpdater, BrowserWindow, ipcMain } = electron;
2018-06-25 17:42:50 +00:00
async function getUpdateUrl(force: boolean): Promise<string | null> {
const platform = process.platform;
const settings = await models.settings.getOrCreate();
let updateUrl = null;
if (platform === 'win32') {
updateUrl = UPDATE_URL_WINDOWS;
} else if (platform === 'darwin') {
updateUrl = UPDATE_URL_MAC;
} else {
return null;
}
const params = [
2018-06-25 17:42:50 +00:00
{ name: 'v', value: getAppVersion() },
2019-09-18 19:07:09 +00:00
{ name: 'app', value: getAppId() },
{ name: 'channel', value: settings.updateChannel },
];
const qs = buildQueryStringFromParams(params);
const fullUrl = joinUrlAndQueryString(updateUrl, qs);
console.log(`[updater] Using url ${fullUrl}`);
if (process.env.INSOMNIA_DISABLE_AUTOMATIC_UPDATES) {
2018-10-17 16:42:33 +00:00
console.log(`[updater] Disabled by INSOMNIA_DISABLE_AUTOMATIC_UPDATES environment variable`);
return null;
}
if (isDevelopment()) {
return null;
}
if (!force && !settings.updateAutomatically) {
return null;
}
return fullUrl;
}
2018-06-25 17:42:50 +00:00
function _sendUpdateStatus(status) {
const windows = BrowserWindow.getAllWindows();
for (const w of windows) {
w.send('updater.check.status', status);
}
}
2018-06-25 17:42:50 +00:00
function _sendUpdateComplete(success: boolean, msg: string) {
const windows = BrowserWindow.getAllWindows();
for (const w of windows) {
w.send('updater.check.complete', success, msg);
}
}
let hasPromptedForUpdates = false;
2018-06-25 17:42:50 +00:00
export async function init() {
autoUpdater.on('error', e => {
console.warn(`[updater] Error: ${e.message}`);
});
autoUpdater.on('update-not-available', () => {
2017-11-22 00:07:28 +00:00
console.log('[updater] Not Available');
_sendUpdateComplete(false, 'Up to Date');
});
autoUpdater.on('update-available', () => {
2017-11-22 00:07:28 +00:00
console.log('[updater] Update Available');
_sendUpdateStatus('Downloading...');
});
2018-10-17 16:42:33 +00:00
autoUpdater.on('update-downloaded', (e, releaseNotes, releaseName, releaseDate, updateUrl) => {
console.log(`[updater] Downloaded ${releaseName}`);
_sendUpdateComplete(true, 'Updated (Restart Required)');
_showUpdateNotification();
});
ipcMain.on('updater.check', async e => {
await _checkForUpdates(true);
});
// Check for updates on an interval
setInterval(async () => {
await _checkForUpdates(false);
}, CHECK_FOR_UPDATES_INTERVAL);
// Check for updates immediately
await _checkForUpdates(false);
}
2018-06-25 17:42:50 +00:00
function _showUpdateNotification() {
if (hasPromptedForUpdates) {
return;
}
const windows = BrowserWindow.getAllWindows();
if (windows.length && windows[0].webContents) {
windows[0].webContents.send('update-available');
}
hasPromptedForUpdates = true;
}
2018-06-25 17:42:50 +00:00
async function _checkForUpdates(force: boolean) {
_sendUpdateStatus('Checking');
await delay(500);
if (force) {
hasPromptedForUpdates = false;
}
if (hasPromptedForUpdates) {
// We've already prompted for updates. Don't bug the user anymore
return;
}
const updateUrl = await getUpdateUrl(force);
if (updateUrl === null) {
2018-06-25 17:42:50 +00:00
console.log(
`[updater] Updater not running platform=${process.platform} dev=${isDevelopment()}`,
2018-06-25 17:42:50 +00:00
);
_sendUpdateComplete(false, 'Updates Not Supported');
return;
}
try {
2017-11-22 00:07:28 +00:00
console.log(`[updater] Checking for updates url=${updateUrl}`);
autoUpdater.setFeedURL(updateUrl);
autoUpdater.checkForUpdates();
} catch (err) {
console.warn('[updater] Failed to check for updates:', err.message);
_sendUpdateComplete(false, 'Update Error');
}
}