Functional updater

This commit is contained in:
Jiiks 2019-03-07 11:35:24 +02:00
parent dd621038f9
commit 001a6e4fda
4 changed files with 85 additions and 3 deletions

View File

@ -33,6 +33,8 @@ export default new class extends Module {
bindings() {
this.restartNotif = this.restartNotif.bind(this);
this.reloadNotif = this.reloadNotif.bind(this);
this.startUpdate = this.startUpdate.bind(this);
this.setUpdateStatus = this.setUpdateStatus.bind(this);
}
restartNotif() {
@ -120,6 +122,14 @@ export default new class extends Module {
return;
}
});
ipc.on('updater-updateFinished', (_, update) => {
this.setUpdateStatus(update.id, 'updated', true);
});
ipc.on('updater-updateError', (_, update) => {
this.setUpdateStatus(update.id, 'error', update.error);
});
}
stateChanged(oldState, newState) {
@ -127,8 +137,30 @@ export default new class extends Module {
if (!oldState.updatesAvailable && newState.updatesAvailable) return Events.emit('updates-available');
}
setUpdateStatus(updateId, statusChild, statusValue) {
for (const u of this.bdUpdates) {
if (u.id === updateId) {
u.status[statusChild] = statusValue;
return;
}
}
}
toggleUpdate(update) {
update.status.update = !update.status.update;
}
async startUpdate() {
console.log('start update');
const updates = { bd: [] };
for (const update of this.bdUpdates) {
if (update.status.update) {
update.status.updating = true;
updates.bd.push(update);
}
}
console.log(updates);
this.send('updater-startUpdate', updates);
}
}

View File

@ -69,9 +69,8 @@
}
},
methods: {
async update() {
// TODO
console.log('update');
update() {
this.updater.startUpdate();
}
}
}

View File

@ -11,6 +11,9 @@
/**
* Base Module that every non-static module should extend.
*/
import { default as BDIpc } from './bdipc';
export default class Module {
constructor(args) {
@ -24,6 +27,7 @@ export default class Module {
init() {
if (this.bindings) this.bindings();
if (this.setInitialState) this.setInitialState(this.state);
if (this.events) this.events(BDIpc);
}
set args(t) {}

View File

@ -72,9 +72,56 @@ export default class Updater extends Module {
bindings() {
this.checkForUpdates = this.checkForUpdates.bind(this);
this.checkForBdUpdates = this.checkForBdUpdates.bind(this);
this.updateAll = this.updateAll.bind(this);
this.updateFinished = this.updateFinished.bind(this);
this.start = this.start.bind(this);
}
events(ipc) {
ipc.on('updater-startUpdate', (_, updates) => this.updateAll(updates));
}
async updateBd(update) {
try {
console.log('[BetterDiscord:Updater] Updating', update.id);
await this.downloadTarGz(`https://github.com/JsSucks/BetterDiscordApp${update.remote}`, this.bd.config.getPath(update.id));
console.log('[BetterDiscord:Updater] Finished updating', update.id);
this.bd.sendToDiscord('updater-updateFinished', update);
this.updateFinished(update);
} catch (err) {
console.log('[BetterDiscord:Updater] Failed to update', update.id);
console.log(err);
}
}
updateAll(updates) {
const bd = updates.bd || [];
const plugins = updates.plugins || [];
const themes = updates.themes || [];
const modules = updates.modules || [];
this.restartRequired = this.reloadRequired = false;
this.finishedUpdates = 0;
this.totalUpdates = bd.length + plugins.length + themes.length + modules.length;
if (bd.length) {
for (const update of bd) {
this.updateBd(update);
}
}
}
updateFinished(update) {
if (update.id === 'core') this.restartRequired = true;
if (update.id === 'client') this.reloadRequired = true;
this.finishedUpdates++;
if (this.finishedUpdates >= this.totalUpdates) {
this.bd.sendToDiscord('updater-updated', { restartRequired: this.restartRequired, reloadRequired: this.reloadRequired });
}
}
start(interval = 15) {
this.updaterThread = setInterval(this.checkForUpdates, interval * 60 * 1000);
}