2019-11-28 05:17:01 -09:00
|
|
|
import {EventEmitter} from 'events';
|
2017-11-29 04:26:24 -09:00
|
|
|
|
2023-07-25 09:30:19 -08:00
|
|
|
import fetch from 'electron-fetch';
|
|
|
|
|
|
2019-12-27 05:16:56 -09:00
|
|
|
class AutoUpdater extends EventEmitter implements Electron.AutoUpdater {
|
|
|
|
|
updateURL!: string;
|
2017-11-29 04:26:24 -09:00
|
|
|
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;
|
2017-11-29 04:26:24 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) => {
|
2017-11-29 04:26:24 -09:00
|
|
|
if (res.status === 204) {
|
2020-06-19 04:51:34 -08:00
|
|
|
this.emit('update-not-available');
|
|
|
|
|
return;
|
2017-11-29 04:26:24 -09:00
|
|
|
}
|
2023-06-26 01:23:37 -08:00
|
|
|
return res.json().then(({name, notes, pub_date}: {name: string; notes: string; pub_date: string}) => {
|
2017-11-29 04:26:24 -09:00
|
|
|
// Only name is mandatory, needed to construct release URL.
|
|
|
|
|
if (!name) {
|
|
|
|
|
throw new Error('Malformed server response: release name is missing.');
|
|
|
|
|
}
|
2023-06-26 01:23:37 -08:00
|
|
|
const date = pub_date ? new Date(pub_date) : new Date();
|
2017-11-29 04:26:24 -09:00
|
|
|
this.emit('update-available', {}, notes, name, date);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(this.emitError.bind(this));
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 04:51:34 -08:00
|
|
|
emitError(error: string | Error) {
|
2017-11-29 04:26:24 -09:00
|
|
|
if (typeof error === 'string') {
|
|
|
|
|
error = new Error(error);
|
|
|
|
|
}
|
2019-12-27 05:16:56 -09:00
|
|
|
this.emit('error', error);
|
2017-11-29 04:26:24 -09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 08:11:02 -08:00
|
|
|
const autoUpdaterLinux = new AutoUpdater();
|
|
|
|
|
|
|
|
|
|
export default autoUpdaterLinux;
|