hyper/app/auto-updater.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

const {autoUpdater} = require('electron');
2016-07-07 15:19:39 -08:00
const ms = require('ms');
2016-07-06 06:58:39 -08:00
const notify = require('./notify'); // eslint-disable-line no-unused-vars
const {version} = require('./package');
// accepted values: `osx`, `win32`
// https://nuts.gitbook.com/update-windows.html
const platform = process.platform === 'darwin' ?
'osx' :
process.platform;
2016-10-06 07:28:43 -08:00
const FEED_URL = `https://hyper-updates.now.sh/update/${platform}`;
2016-07-07 15:19:39 -08:00
let isInit = false;
2016-07-06 06:58:39 -08:00
function init() {
2016-07-06 06:58:39 -08:00
autoUpdater.on('error', (err, msg) => {
console.error('Error fetching updates', msg + ' (' + err.stack + ')');
2016-07-06 06:58:39 -08:00
});
autoUpdater.setFeedURL(`${FEED_URL}/${version}`);
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 = function (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
};