hyper/app/auto-updater.js

77 lines
1.7 KiB
JavaScript
Raw Normal View History

// Packages
const {autoUpdater} = require('electron');
2016-07-07 15:19:39 -08:00
const ms = require('ms');
const retry = require('async-retry');
2016-07-06 06:58:39 -08:00
// Utilities
// eslint-disable-next-line no-unused-vars
const notify = require('./notify');
const {version} = require('./package');
const {getConfig} = require('./config');
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
2017-09-20 09:35:50 -08:00
async function init() {
2016-07-06 06:58:39 -08:00
autoUpdater.on('error', (err, msg) => {
//eslint-disable-next-line no-console
console.error('Error fetching updates', msg + ' (' + err.stack + ')');
2016-07-06 06:58:39 -08:00
});
2017-09-20 09:35:50 -08:00
const config = await retry(async () => {
const content = await getConfig();
if (!content) {
throw new Error('No config content loaded');
}
return content;
});
// 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';
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();
}, ms('30m'));
2016-07-07 15:19:39 -08:00
isInit = true;
}
module.exports = win => {
if (!isInit) {
init();
}
2016-07-07 15:19:39 -08:00
const {rpc} = win;
2016-07-07 15:19:39 -08:00
const onupdate = (ev, releaseNotes, releaseName) => {
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
};