mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-12 20:18:41 -09:00
port updater to ts
This commit is contained in:
parent
78ec88d1e8
commit
f6cee0520e
2 changed files with 29 additions and 19 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
import fetch from 'electron-fetch';
|
import fetch from 'electron-fetch';
|
||||||
import {EventEmitter} from 'events';
|
import {EventEmitter} from 'events';
|
||||||
|
|
||||||
class AutoUpdater extends EventEmitter {
|
class AutoUpdater extends EventEmitter implements Electron.AutoUpdater {
|
||||||
|
updateURL!: string;
|
||||||
quitAndInstall() {
|
quitAndInstall() {
|
||||||
this.emitError('QuitAndInstall unimplemented');
|
this.emitError('QuitAndInstall unimplemented');
|
||||||
}
|
}
|
||||||
|
|
@ -9,8 +10,8 @@ class AutoUpdater extends EventEmitter {
|
||||||
return this.updateURL;
|
return this.updateURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
setFeedURL(updateURL) {
|
setFeedURL(options: Electron.FeedURLOptions) {
|
||||||
this.updateURL = updateURL;
|
this.updateURL = options.url;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkForUpdates() {
|
checkForUpdates() {
|
||||||
|
|
@ -20,16 +21,18 @@ class AutoUpdater extends EventEmitter {
|
||||||
this.emit('checking-for-update');
|
this.emit('checking-for-update');
|
||||||
|
|
||||||
fetch(this.updateURL)
|
fetch(this.updateURL)
|
||||||
.then(res => {
|
.then((res): any => {
|
||||||
if (res.status === 204) {
|
if (res.status === 204) {
|
||||||
return this.emit('update-not-available');
|
return this.emit('update-not-available');
|
||||||
}
|
}
|
||||||
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||||
return res.json().then(({name, notes, pub_date}) => {
|
return res.json().then(({name, notes, pub_date}) => {
|
||||||
// Only name is mandatory, needed to construct release URL.
|
// Only name is mandatory, needed to construct release URL.
|
||||||
if (!name) {
|
if (!name) {
|
||||||
throw new Error('Malformed server response: release name is missing.');
|
throw new Error('Malformed server response: release name is missing.');
|
||||||
}
|
}
|
||||||
// If `null` is passed to Date constructor, current time will be used. This doesn't work with `undefined`
|
// If `null` is passed to Date constructor, current time will be used. This doesn't work with `undefined`
|
||||||
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||||
const date = new Date(pub_date || null);
|
const date = new Date(pub_date || null);
|
||||||
this.emit('update-available', {}, notes, name, date);
|
this.emit('update-available', {}, notes, name, date);
|
||||||
});
|
});
|
||||||
|
|
@ -37,11 +40,11 @@ class AutoUpdater extends EventEmitter {
|
||||||
.catch(this.emitError.bind(this));
|
.catch(this.emitError.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
emitError(error) {
|
emitError(error: any) {
|
||||||
if (typeof error === 'string') {
|
if (typeof error === 'string') {
|
||||||
error = new Error(error);
|
error = new Error(error);
|
||||||
}
|
}
|
||||||
this.emit('error', error, error.message);
|
this.emit('error', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1,32 +1,32 @@
|
||||||
// Packages
|
// Packages
|
||||||
import electron from 'electron';
|
import electron, {app, BrowserWindow, AutoUpdater} from 'electron';
|
||||||
const {app} = electron;
|
|
||||||
import ms from 'ms';
|
import ms from 'ms';
|
||||||
import retry from 'async-retry';
|
import retry from 'async-retry';
|
||||||
|
|
||||||
// Utilities
|
// Utilities
|
||||||
import {version} from './package';
|
import {version} from './package.json';
|
||||||
import {getDecoratedConfig} from './plugins';
|
import {getDecoratedConfig} from './plugins';
|
||||||
|
import autoUpdaterLinux from './auto-updater-linux';
|
||||||
|
|
||||||
const {platform} = process;
|
const {platform} = process;
|
||||||
const isLinux = platform === 'linux';
|
const isLinux = platform === 'linux';
|
||||||
|
|
||||||
const autoUpdater = isLinux ? require('./auto-updater-linux').default : electron.autoUpdater;
|
const autoUpdater: AutoUpdater = isLinux ? autoUpdaterLinux : electron.autoUpdater;
|
||||||
|
|
||||||
let isInit = false;
|
let isInit = false;
|
||||||
// Default to the "stable" update channel
|
// Default to the "stable" update channel
|
||||||
let canaryUpdates = false;
|
let canaryUpdates = false;
|
||||||
|
|
||||||
const buildFeedUrl = (canary, currentVersion) => {
|
const buildFeedUrl = (canary: boolean, currentVersion: string) => {
|
||||||
const updatePrefix = canary ? 'releases-canary' : 'releases';
|
const updatePrefix = canary ? 'releases-canary' : 'releases';
|
||||||
return `https://${updatePrefix}.hyper.is/update/${isLinux ? 'deb' : platform}/${currentVersion}`;
|
return `https://${updatePrefix}.hyper.is/update/${isLinux ? 'deb' : platform}/${currentVersion}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const isCanary = updateChannel => updateChannel === 'canary';
|
const isCanary = (updateChannel: string) => updateChannel === 'canary';
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
autoUpdater.on('error', (err, msg) => {
|
autoUpdater.on('error', err => {
|
||||||
console.error('Error fetching updates', `${msg} (${err.stack})`);
|
console.error('Error fetching updates', `${err.message} (${err.stack})`);
|
||||||
});
|
});
|
||||||
|
|
||||||
const config = await retry(async () => {
|
const config = await retry(async () => {
|
||||||
|
|
@ -46,7 +46,7 @@ async function init() {
|
||||||
|
|
||||||
const feedURL = buildFeedUrl(canaryUpdates, version);
|
const feedURL = buildFeedUrl(canaryUpdates, version);
|
||||||
|
|
||||||
autoUpdater.setFeedURL(feedURL);
|
autoUpdater.setFeedURL({url: feedURL});
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
autoUpdater.checkForUpdates();
|
autoUpdater.checkForUpdates();
|
||||||
|
|
@ -59,19 +59,26 @@ async function init() {
|
||||||
isInit = true;
|
isInit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default win => {
|
export default (win: BrowserWindow) => {
|
||||||
if (!isInit) {
|
if (!isInit) {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
const {rpc} = win;
|
const {rpc} = win;
|
||||||
|
|
||||||
const onupdate = (ev, releaseNotes, releaseName, date, updateUrl, onQuitAndInstall) => {
|
const onupdate = (
|
||||||
|
ev: Event,
|
||||||
|
releaseNotes: string,
|
||||||
|
releaseName: string,
|
||||||
|
date: Date,
|
||||||
|
updateUrl: string,
|
||||||
|
onQuitAndInstall: any
|
||||||
|
) => {
|
||||||
const releaseUrl = updateUrl || `https://github.com/zeit/hyper/releases/tag/${releaseName}`;
|
const releaseUrl = updateUrl || `https://github.com/zeit/hyper/releases/tag/${releaseName}`;
|
||||||
rpc.emit('update available', {releaseNotes, releaseName, releaseUrl, canInstall: !!onQuitAndInstall});
|
rpc.emit('update available', {releaseNotes, releaseName, releaseUrl, canInstall: !!onQuitAndInstall});
|
||||||
};
|
};
|
||||||
|
|
||||||
const eventName = isLinux ? 'update-available' : 'update-downloaded';
|
const eventName: any = isLinux ? 'update-available' : 'update-downloaded';
|
||||||
|
|
||||||
autoUpdater.on(eventName, onupdate);
|
autoUpdater.on(eventName, onupdate);
|
||||||
|
|
||||||
|
|
@ -86,7 +93,7 @@ export default win => {
|
||||||
if (newUpdateIsCanary !== canaryUpdates) {
|
if (newUpdateIsCanary !== canaryUpdates) {
|
||||||
const feedURL = buildFeedUrl(newUpdateIsCanary, version);
|
const feedURL = buildFeedUrl(newUpdateIsCanary, version);
|
||||||
|
|
||||||
autoUpdater.setFeedURL(feedURL);
|
autoUpdater.setFeedURL({url: feedURL});
|
||||||
autoUpdater.checkForUpdates();
|
autoUpdater.checkForUpdates();
|
||||||
|
|
||||||
canaryUpdates = newUpdateIsCanary;
|
canaryUpdates = newUpdateIsCanary;
|
||||||
Loading…
Reference in a new issue