-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
auto-updater-linux.ts
52 lines (44 loc) · 1.38 KB
/
auto-updater-linux.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import {EventEmitter} from 'events';
import fetch from 'electron-fetch';
class AutoUpdater extends EventEmitter implements Electron.AutoUpdater {
updateURL!: string;
quitAndInstall() {
this.emitError('QuitAndInstall unimplemented');
}
getFeedURL() {
return this.updateURL;
}
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)
.then((res) => {
if (res.status === 204) {
this.emit('update-not-available');
return;
}
return res.json().then(({name, notes, pub_date}: {name: string; notes: string; pub_date: string}) => {
// Only name is mandatory, needed to construct release URL.
if (!name) {
throw new Error('Malformed server response: release name is missing.');
}
const date = pub_date ? new Date(pub_date) : new Date();
this.emit('update-available', {}, notes, name, date);
});
})
.catch(this.emitError.bind(this));
}
emitError(error: string | Error) {
if (typeof error === 'string') {
error = new Error(error);
}
this.emit('error', error);
}
}
const autoUpdaterLinux = new AutoUpdater();
export default autoUpdaterLinux;