From f6cee0520ecfe01b71daae97afb43e37c29a3406 Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Fri, 27 Dec 2019 19:46:56 +0530 Subject: [PATCH] port updater to ts --- ...updater-linux.js => auto-updater-linux.ts} | 15 +++++---- app/{updater.js => updater.ts} | 33 +++++++++++-------- 2 files changed, 29 insertions(+), 19 deletions(-) rename app/{auto-updater-linux.js => auto-updater-linux.ts} (74%) rename app/{updater.js => updater.ts} (68%) diff --git a/app/auto-updater-linux.js b/app/auto-updater-linux.ts similarity index 74% rename from app/auto-updater-linux.js rename to app/auto-updater-linux.ts index d3b73dd1..da83733a 100644 --- a/app/auto-updater-linux.js +++ b/app/auto-updater-linux.ts @@ -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); } } diff --git a/app/updater.js b/app/updater.ts similarity index 68% rename from app/updater.js rename to app/updater.ts index ca872ea3..92e1703a 100644 --- a/app/updater.js +++ b/app/updater.ts @@ -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;