2017-08-20 02:18:42 -08:00
|
|
|
// Packages
|
2019-11-28 05:17:01 -09:00
|
|
|
import electron from 'electron';
|
2017-11-29 04:26:24 -09:00
|
|
|
const {app} = electron;
|
2019-11-28 05:17:01 -09:00
|
|
|
import ms from 'ms';
|
|
|
|
|
import retry from 'async-retry';
|
2016-07-06 06:58:39 -08:00
|
|
|
|
2017-08-20 02:18:42 -08:00
|
|
|
// Utilities
|
2019-11-28 05:17:01 -09:00
|
|
|
import {version} from './package';
|
|
|
|
|
import {getDecoratedConfig} from './plugins';
|
2016-09-21 06:27:11 -08:00
|
|
|
|
2017-08-04 13:52:38 -08:00
|
|
|
const {platform} = process;
|
2017-11-29 04:26:24 -09:00
|
|
|
const isLinux = platform === 'linux';
|
|
|
|
|
|
2020-01-12 04:13:48 -09:00
|
|
|
const autoUpdater = isLinux ? require('./auto-updater-linux').default : electron.autoUpdater;
|
2017-08-04 13:52:38 -08:00
|
|
|
|
2016-07-07 15:19:39 -08:00
|
|
|
let isInit = false;
|
2017-11-22 01:34:37 -09:00
|
|
|
// Default to the "stable" update channel
|
|
|
|
|
let canaryUpdates = false;
|
|
|
|
|
|
2017-11-29 04:26:24 -09:00
|
|
|
const buildFeedUrl = (canary, currentVersion) => {
|
2017-11-22 01:34:37 -09:00
|
|
|
const updatePrefix = canary ? 'releases-canary' : 'releases';
|
2017-11-29 04:26:24 -09:00
|
|
|
return `https://${updatePrefix}.hyper.is/update/${isLinux ? 'deb' : platform}/${currentVersion}`;
|
2017-11-22 01:34:37 -09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isCanary = updateChannel => updateChannel === 'canary';
|
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) => {
|
2019-11-28 05:17:01 -09:00
|
|
|
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 () => {
|
2017-11-29 04:26:24 -09:00
|
|
|
const content = await getDecoratedConfig();
|
2017-08-30 08:54:45 -08:00
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
|
throw new Error('No config content loaded');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
});
|
2017-08-30 04:12:59 -08:00
|
|
|
|
|
|
|
|
// If defined in the config, switch to the "canary" channel
|
2017-11-22 01:34:37 -09:00
|
|
|
if (config.updateChannel && isCanary(config.updateChannel)) {
|
2017-08-30 04:12:59 -08:00
|
|
|
canaryUpdates = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 04:26:24 -09:00
|
|
|
const feedURL = buildFeedUrl(canaryUpdates, version);
|
2017-08-20 02:18:42 -08:00
|
|
|
|
2017-11-29 04:26:24 -09:00
|
|
|
autoUpdater.setFeedURL(feedURL);
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export default 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
|
|
|
|
2017-11-29 04:26:24 -09:00
|
|
|
const onupdate = (ev, releaseNotes, releaseName, date, updateUrl, onQuitAndInstall) => {
|
|
|
|
|
const releaseUrl = updateUrl || `https://github.com/zeit/hyper/releases/tag/${releaseName}`;
|
|
|
|
|
rpc.emit('update available', {releaseNotes, releaseName, releaseUrl, canInstall: !!onQuitAndInstall});
|
2016-07-07 15:19:39 -08:00
|
|
|
};
|
2016-07-06 06:58:39 -08:00
|
|
|
|
2017-11-29 04:26:24 -09:00
|
|
|
const eventName = isLinux ? 'update-available' : 'update-downloaded';
|
|
|
|
|
|
|
|
|
|
autoUpdater.on(eventName, onupdate);
|
2016-07-07 15:19:39 -08:00
|
|
|
|
|
|
|
|
rpc.once('quit and install', () => {
|
2016-07-06 06:58:39 -08:00
|
|
|
autoUpdater.quitAndInstall();
|
|
|
|
|
});
|
|
|
|
|
|
2017-11-22 01:34:37 -09:00
|
|
|
app.config.subscribe(() => {
|
|
|
|
|
const {updateChannel} = app.plugins.getDecoratedConfig();
|
|
|
|
|
const newUpdateIsCanary = isCanary(updateChannel);
|
|
|
|
|
|
|
|
|
|
if (newUpdateIsCanary !== canaryUpdates) {
|
2017-11-29 04:26:24 -09:00
|
|
|
const feedURL = buildFeedUrl(newUpdateIsCanary, version);
|
2017-11-22 01:34:37 -09:00
|
|
|
|
2017-11-29 04:26:24 -09:00
|
|
|
autoUpdater.setFeedURL(feedURL);
|
2017-11-22 01:34:37 -09:00
|
|
|
autoUpdater.checkForUpdates();
|
|
|
|
|
|
|
|
|
|
canaryUpdates = newUpdateIsCanary;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-07-07 15:19:39 -08:00
|
|
|
win.on('close', () => {
|
2017-11-29 04:26:24 -09:00
|
|
|
autoUpdater.removeListener(eventName, onupdate);
|
2016-07-07 15:19:39 -08:00
|
|
|
});
|
2016-07-06 06:58:39 -08:00
|
|
|
};
|