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 {EventEmitter} from 'events';
|
||||
|
||||
class AutoUpdater extends EventEmitter {
|
||||
class AutoUpdater extends EventEmitter implements Electron.AutoUpdater {
|
||||
updateURL!: string;
|
||||
quitAndInstall() {
|
||||
this.emitError('QuitAndInstall unimplemented');
|
||||
}
|
||||
|
|
@ -9,8 +10,8 @@ class AutoUpdater extends EventEmitter {
|
|||
return this.updateURL;
|
||||
}
|
||||
|
||||
setFeedURL(updateURL) {
|
||||
this.updateURL = updateURL;
|
||||
setFeedURL(options: Electron.FeedURLOptions) {
|
||||
this.updateURL = options.url;
|
||||
}
|
||||
|
||||
checkForUpdates() {
|
||||
|
|
@ -20,16 +21,18 @@ class AutoUpdater extends EventEmitter {
|
|||
this.emit('checking-for-update');
|
||||
|
||||
fetch(this.updateURL)
|
||||
.then(res => {
|
||||
.then((res): any => {
|
||||
if (res.status === 204) {
|
||||
return this.emit('update-not-available');
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
return res.json().then(({name, notes, pub_date}) => {
|
||||
// Only name is mandatory, needed to construct release URL.
|
||||
if (!name) {
|
||||
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`
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
const date = new Date(pub_date || null);
|
||||
this.emit('update-available', {}, notes, name, date);
|
||||
});
|
||||
|
|
@ -37,11 +40,11 @@ class AutoUpdater extends EventEmitter {
|
|||
.catch(this.emitError.bind(this));
|
||||
}
|
||||
|
||||
emitError(error) {
|
||||
emitError(error: any) {
|
||||
if (typeof error === 'string') {
|
||||
error = new Error(error);
|
||||
}
|
||||
this.emit('error', error, error.message);
|
||||
this.emit('error', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,32 +1,32 @@
|
|||
// Packages
|
||||
import electron from 'electron';
|
||||
const {app} = electron;
|
||||
import electron, {app, BrowserWindow, AutoUpdater} from 'electron';
|
||||
import ms from 'ms';
|
||||
import retry from 'async-retry';
|
||||
|
||||
// Utilities
|
||||
import {version} from './package';
|
||||
import {version} from './package.json';
|
||||
import {getDecoratedConfig} from './plugins';
|
||||
import autoUpdaterLinux from './auto-updater-linux';
|
||||
|
||||
const {platform} = process;
|
||||
const isLinux = platform === 'linux';
|
||||
|
||||
const autoUpdater = isLinux ? require('./auto-updater-linux').default : electron.autoUpdater;
|
||||
const autoUpdater: AutoUpdater = isLinux ? autoUpdaterLinux : electron.autoUpdater;
|
||||
|
||||
let isInit = false;
|
||||
// Default to the "stable" update channel
|
||||
let canaryUpdates = false;
|
||||
|
||||
const buildFeedUrl = (canary, currentVersion) => {
|
||||
const buildFeedUrl = (canary: boolean, currentVersion: string) => {
|
||||
const updatePrefix = canary ? 'releases-canary' : 'releases';
|
||||
return `https://${updatePrefix}.hyper.is/update/${isLinux ? 'deb' : platform}/${currentVersion}`;
|
||||
};
|
||||
|
||||
const isCanary = updateChannel => updateChannel === 'canary';
|
||||
const isCanary = (updateChannel: string) => updateChannel === 'canary';
|
||||
|
||||
async function init() {
|
||||
autoUpdater.on('error', (err, msg) => {
|
||||
console.error('Error fetching updates', `${msg} (${err.stack})`);
|
||||
autoUpdater.on('error', err => {
|
||||
console.error('Error fetching updates', `${err.message} (${err.stack})`);
|
||||
});
|
||||
|
||||
const config = await retry(async () => {
|
||||
|
|
@ -46,7 +46,7 @@ async function init() {
|
|||
|
||||
const feedURL = buildFeedUrl(canaryUpdates, version);
|
||||
|
||||
autoUpdater.setFeedURL(feedURL);
|
||||
autoUpdater.setFeedURL({url: feedURL});
|
||||
|
||||
setTimeout(() => {
|
||||
autoUpdater.checkForUpdates();
|
||||
|
|
@ -59,19 +59,26 @@ async function init() {
|
|||
isInit = true;
|
||||
}
|
||||
|
||||
export default win => {
|
||||
export default (win: BrowserWindow) => {
|
||||
if (!isInit) {
|
||||
init();
|
||||
}
|
||||
|
||||
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}`;
|
||||
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);
|
||||
|
||||
|
|
@ -86,7 +93,7 @@ export default win => {
|
|||
if (newUpdateIsCanary !== canaryUpdates) {
|
||||
const feedURL = buildFeedUrl(newUpdateIsCanary, version);
|
||||
|
||||
autoUpdater.setFeedURL(feedURL);
|
||||
autoUpdater.setFeedURL({url: feedURL});
|
||||
autoUpdater.checkForUpdates();
|
||||
|
||||
canaryUpdates = newUpdateIsCanary;
|
||||
Loading…
Reference in a new issue