diff --git a/client/src/modules/updater.js b/client/src/modules/updater.js index 2fe582a8..5ab5662f 100644 --- a/client/src/modules/updater.js +++ b/client/src/modules/updater.js @@ -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); + } + } diff --git a/client/src/ui/components/bd/UpdaterView.vue b/client/src/ui/components/bd/UpdaterView.vue index e1fce5fc..e9457d9a 100644 --- a/client/src/ui/components/bd/UpdaterView.vue +++ b/client/src/ui/components/bd/UpdaterView.vue @@ -69,9 +69,8 @@ } }, methods: { - async update() { - // TODO - console.log('update'); + update() { + this.updater.startUpdate(); } } } diff --git a/core/src/modules/modulebase.js b/core/src/modules/modulebase.js index 5623e3e9..6a630548 100644 --- a/core/src/modules/modulebase.js +++ b/core/src/modules/modulebase.js @@ -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) {} diff --git a/core/src/modules/updater.js b/core/src/modules/updater.js index ecc75c85..891e0218 100644 --- a/core/src/modules/updater.js +++ b/core/src/modules/updater.js @@ -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); }