hyper/app/auto-updater.js
2017-09-21 00:09:04 +02:00

76 lines
1.7 KiB
JavaScript

// Packages
const {autoUpdater} = require('electron');
const ms = require('ms');
const retry = require('async-retry');
// Utilities
// eslint-disable-next-line no-unused-vars
const notify = require('./notify');
const {version} = require('./package');
const {getConfig} = require('./config');
const {platform} = process;
let isInit = false;
async function init() {
autoUpdater.on('error', (err, msg) => {
//eslint-disable-next-line no-console
console.error('Error fetching updates', msg + ' (' + err.stack + ')');
});
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}`);
setTimeout(() => {
autoUpdater.checkForUpdates();
}, ms('10s'));
setInterval(() => {
autoUpdater.checkForUpdates();
}, ms('30m'));
isInit = true;
}
module.exports = win => {
if (!isInit) {
init();
}
const {rpc} = win;
const onupdate = (ev, releaseNotes, releaseName) => {
rpc.emit('update available', {releaseNotes, releaseName});
};
autoUpdater.on('update-downloaded', onupdate);
rpc.once('quit and install', () => {
autoUpdater.quitAndInstall();
});
win.on('close', () => {
autoUpdater.removeListener('update-downloaded', onupdate);
});
};