OpenAsar/src/updater/hostUpdater.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

const { app, autoUpdater } = require('electron');
const events = require('events');
2021-12-09 16:25:14 +00:00
const { get } = require('request');
2021-12-09 16:25:14 +00:00
const vParse = (s) => s.split('.').map((x) => parseInt(x));
const vNewer = (a, b) => a.some((x, i) => x === b[i] ? undefined : (x > b[i]));
2021-12-09 16:25:14 +00:00
class HostLinux extends events.EventEmitter {
2021-12-09 16:25:14 +00:00
setFeedURL(url) {
this.updateUrl = url;
}
quitAndInstall() {
app.relaunch();
app.quit();
2021-12-09 16:25:14 +00:00
}
async checkForUpdates() {
this.emit('checking-for-update');
try {
const current = vParse(app.getVersion());
2021-12-09 16:25:14 +00:00
get(this.updateUrl, (_e, res, body) => {
if (res.statusCode === 204) return this.emit('update-not-available');
const latest = vParse(JSON.parse(body).name);
2021-12-09 16:25:14 +00:00
if (vNewer(latest, current)) return this.emit('update-manually', latest.join('.'));
this.emit('update-not-available');
});
} catch (e) {
log('HostLinux', 'Error', e);
this.emit('error', e);
2021-12-09 16:25:14 +00:00
}
}
}
switch (process.platform) {
case 'darwin':
module.exports = autoUpdater;
2021-12-09 16:25:14 +00:00
break;
case 'linux':
module.exports = new HostLinux();
2021-12-09 16:25:14 +00:00
break;
}