2017-08-20 02:18:42 -08:00
|
|
|
// Packages
|
2016-09-21 06:27:11 -08:00
|
|
|
const {autoUpdater} = require('electron');
|
2016-07-07 15:19:39 -08:00
|
|
|
const ms = require('ms');
|
2017-08-30 08:54:45 -08:00
|
|
|
const retry = require('async-retry');
|
2016-07-06 06:58:39 -08:00
|
|
|
|
2017-08-20 02:18:42 -08:00
|
|
|
// Utilities
|
2016-09-21 06:27:11 -08:00
|
|
|
const notify = require('./notify'); // eslint-disable-line no-unused-vars
|
|
|
|
|
const {version} = require('./package');
|
2017-08-20 02:18:42 -08:00
|
|
|
const {getConfig} = require('./config');
|
2016-09-21 06:27:11 -08:00
|
|
|
|
2017-08-04 13:52:38 -08:00
|
|
|
const {platform} = process;
|
|
|
|
|
|
2016-07-07 15:19:39 -08:00
|
|
|
let isInit = false;
|
2016-07-06 06:58:39 -08:00
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
function init() {
|
2016-07-06 06:58:39 -08:00
|
|
|
autoUpdater.on('error', (err, msg) => {
|
2016-07-21 11:44:25 -08:00
|
|
|
console.error('Error fetching updates', msg + ' (' + err.stack + ')');
|
2016-07-06 06:58:39 -08:00
|
|
|
});
|
|
|
|
|
|
2017-08-30 08:54:45 -08:00
|
|
|
const config = retry(() => {
|
|
|
|
|
const content = getConfig();
|
|
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
|
throw new Error('No config content loaded');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
});
|
2017-08-30 04:12:59 -08:00
|
|
|
|
|
|
|
|
// Default to the "stable" update channel
|
|
|
|
|
let canaryUpdates = false;
|
|
|
|
|
|
|
|
|
|
// If defined in the config, switch to the "canary" channel
|
|
|
|
|
if (config.updateChannel && config.updateChannel === 'canary') {
|
|
|
|
|
canaryUpdates = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updatePrefix = canaryUpdates ? 'releases-canary' : 'releases';
|
2017-08-20 02:18:42 -08:00
|
|
|
const feedURL = `https://${updatePrefix}.hyper.is/update/${platform}`;
|
|
|
|
|
|
|
|
|
|
autoUpdater.setFeedURL(`${feedURL}/${version}`);
|
2016-07-06 06:58:39 -08:00
|
|
|
|
2016-07-07 15:19:39 -08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
autoUpdater.checkForUpdates();
|
|
|
|
|
}, ms('10s'));
|
|
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
|
autoUpdater.checkForUpdates();
|
2016-10-29 03:14:39 -08:00
|
|
|
}, ms('30m'));
|
2016-07-07 15:19:39 -08:00
|
|
|
|
|
|
|
|
isInit = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = function (win) {
|
2016-09-21 06:27:11 -08:00
|
|
|
if (!isInit) {
|
|
|
|
|
init();
|
|
|
|
|
}
|
2016-07-07 15:19:39 -08:00
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
const {rpc} = win;
|
2016-07-07 15:19:39 -08:00
|
|
|
|
|
|
|
|
const onupdate = (ev, releaseNotes, releaseName) => {
|
2016-09-21 06:27:11 -08:00
|
|
|
rpc.emit('update available', {releaseNotes, releaseName});
|
2016-07-07 15:19:39 -08:00
|
|
|
};
|
2016-07-06 06:58:39 -08:00
|
|
|
|
2016-07-07 15:19:39 -08:00
|
|
|
autoUpdater.on('update-downloaded', onupdate);
|
|
|
|
|
|
|
|
|
|
rpc.once('quit and install', () => {
|
2016-07-06 06:58:39 -08:00
|
|
|
autoUpdater.quitAndInstall();
|
|
|
|
|
});
|
|
|
|
|
|
2016-07-07 15:19:39 -08:00
|
|
|
win.on('close', () => {
|
|
|
|
|
autoUpdater.removeListener('update-downloaded', onupdate);
|
|
|
|
|
});
|
2016-07-06 06:58:39 -08:00
|
|
|
};
|