hyper/app/auto-updater-linux.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2019-11-28 05:17:01 -09:00
import fetch from 'electron-fetch';
import {EventEmitter} from 'events';
2019-12-27 05:16:56 -09:00
class AutoUpdater extends EventEmitter implements Electron.AutoUpdater {
updateURL!: string;
quitAndInstall() {
this.emitError('QuitAndInstall unimplemented');
}
getFeedURL() {
return this.updateURL;
}
2019-12-27 05:16:56 -09:00
setFeedURL(options: Electron.FeedURLOptions) {
this.updateURL = options.url;
}
checkForUpdates() {
if (!this.updateURL) {
return this.emitError('Update URL is not set');
}
this.emit('checking-for-update');
fetch(this.updateURL)
2020-06-19 04:51:34 -08:00
.then((res) => {
if (res.status === 204) {
2020-06-19 04:51:34 -08:00
this.emit('update-not-available');
return;
}
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`
const date = new Date(pub_date || null);
this.emit('update-available', {}, notes, name, date);
});
})
.catch(this.emitError.bind(this));
}
2020-06-19 04:51:34 -08:00
emitError(error: string | Error) {
if (typeof error === 'string') {
error = new Error(error);
}
2019-12-27 05:16:56 -09:00
this.emit('error', error);
}
}
2019-11-28 05:17:01 -09:00
export default new AutoUpdater();