OpenAsar/src/updater/hostUpdater.js

50 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
2022-04-04 14:41:49 +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
2022-04-04 07:23:02 +00:00
get(this.updateUrl, (err, res, body) => {
if (err) return this.emit('error');
if (res.statusCode === 204) return this.emit('update-not-available');
2021-12-09 16:25:14 +00:00
2022-04-04 07:23:02 +00:00
const latest = vParse(JSON.parse(body).name);
if (vNewer(latest, current)) return this.emit('update-manually', latest.join('.'));
this.emit('update-not-available');
});
} catch (e) {
log('HostLinux', 'Error', e);
2022-04-04 07:23:02 +00:00
this.emit('error');
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;
}