From 0001b54e1531a66e7badcd7837281b41305b32e2 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sat, 20 Aug 2022 11:15:12 +0200 Subject: [PATCH] Updated updater.js --- src/client/assets/js/checkingUpdate.js | 5 +- src/client/assets/js/index.js | 1 + src/client/assets/js/login.js | 1 + src/client/assets/js/preload.js | 1 - src/server/main.js | 10 ++- src/server/updater.js | 100 +++++++++++++++---------- 6 files changed, 76 insertions(+), 42 deletions(-) diff --git a/src/client/assets/js/checkingUpdate.js b/src/client/assets/js/checkingUpdate.js index ec8ac02..5b5ebb1 100644 --- a/src/client/assets/js/checkingUpdate.js +++ b/src/client/assets/js/checkingUpdate.js @@ -1,3 +1,4 @@ +const vue = require('vue/dist/vue.cjs.js') app = vue.createApp({ data() { return { @@ -19,6 +20,6 @@ app = vue.createApp({ app.mount("#vue"); -ipcRenderer.on("update-status", (event, arg) => { - +ipcRenderer.on("update-available", (event, arg) => { + this.fullscreenText = "Mise à jour disponible, téléchargement..." }); \ No newline at end of file diff --git a/src/client/assets/js/index.js b/src/client/assets/js/index.js index 54e9acd..509b37a 100644 --- a/src/client/assets/js/index.js +++ b/src/client/assets/js/index.js @@ -1,5 +1,6 @@ const os = require('os') const totalMem = os.totalmem() / (1.049 * Math.pow(10, 6)) +const vue = require('vue/dist/vue.cjs.js') app = vue.createApp({ data() { diff --git a/src/client/assets/js/login.js b/src/client/assets/js/login.js index 8715678..5ea9a4a 100644 --- a/src/client/assets/js/login.js +++ b/src/client/assets/js/login.js @@ -1,3 +1,4 @@ +const vue = require('vue/dist/vue.cjs.js') app = vue.createApp({ data() { return { diff --git a/src/client/assets/js/preload.js b/src/client/assets/js/preload.js index 76c5a59..4fd6d9a 100644 --- a/src/client/assets/js/preload.js +++ b/src/client/assets/js/preload.js @@ -1,6 +1,5 @@ 'use strict'; const { ipcRenderer } = require('electron'); -const vue = require('vue/dist/vue.cjs.js') let app; window.addEventListener("DOMContentLoaded", () => { const minimizeButton = document.getElementById("minimize-btn") diff --git a/src/server/main.js b/src/server/main.js index 4dd8830..99ab923 100644 --- a/src/server/main.js +++ b/src/server/main.js @@ -1,6 +1,9 @@ const { app, BrowserWindow, Menu, ipcMain, autoUpdater, dialog } = require('electron') const logger = require('electron-log') const { join } = require('path') +const updater = require('./updater.js') +let updaterInstance = null + if (require('electron-squirrel-startup')) { require("./install.js").handleSquirrelEvent(app) @@ -27,7 +30,8 @@ function createWindow () { }) //Menu.setApplicationMenu(null) win.loadFile('src/client/checkingUpdate.html').then(() => { - require('./updater.js').configUpdater(app, autoUpdater, dialog, logger, showNotification) + updaterInstance = new updater.Updater(app, win, autoUpdater, dialog, logger, showNotification) + updaterInstance.configUpdater() }) win.on("close", () => { app.quit() @@ -90,4 +94,8 @@ ipcMain.on("disconnect", () => { ipcMain.on("pageReady", (event) => { event.sender.send("nick", { name: minecraft.auth.name }) minecraft.getModsInformations(event) +}) + +ipcMain.on("checking-update", () => { + updaterInstance.checkForUpdates(win, showNotification) }) \ No newline at end of file diff --git a/src/server/updater.js b/src/server/updater.js index 72bec7e..6a77aaf 100644 --- a/src/server/updater.js +++ b/src/server/updater.js @@ -2,52 +2,76 @@ const isDev = require('electron-is-dev') const pkg = require('../../package.json') const server = 'https://update.electronjs.org' -function configUpdater(app, autoUpdater, dialog, logger, showNotification) { - logger.info(`electron version: ${process.versions['electron']}`) - logger.info(`chrome version: ${process.versions['chrome']}`) - logger.info(`Node version: ${process.versions['node']}`) - logger.info(`platform: ${process.platform}`) - logger.info(`arch: ${process.arch}`) - if(isDev) { - logger.info(`developpement version ${app.getVersion()}`) - return +class Updater { + + constructor(app, win, autoUpdater, dialog, logger, ipcMain) { + this.app = app + this.win = win + this.autoUpdater = autoUpdater + this.dialog = dialog + this.logger = logger + this.ipcMain = ipcMain } - logger.info(`production version ${app.getVersion()}`) - - const feed = `${server}/${pkg.repository}/${process.platform}-${process.arch}/${app.getVersion()}` - autoUpdater.setFeedURL(feed) - logger.info("Checking for update...") - autoUpdater.checkForUpdates() - autoUpdater.on('update-downloaded', (_event, releaseNotes, releaseName) => { - const dialogOpts = { - type: 'info', - buttons: ['Rédémarrer', 'Plus tard'], - title: 'Une mise à jour du launcher est disponible', - message: process.platform === 'win32' ? releaseNotes : releaseName, - detail: 'Une nouvelle version du launcher a été téléchargé. Redémarrez l\'application pour appliquer les mises à jour.' + configUpdater() { + this.logger.info(`electron version: ${process.versions['electron']}`) + this.logger.info(`chrome version: ${process.versions['chrome']}`) + this.logger.info(`Node version: ${process.versions['node']}`) + this.logger.info(`platform: ${process.platform}`) + this.logger.info(`arch: ${process.arch}`) + if(isDev) { + this.logger.info(`developpement version ${this.app.getVersion()}`) + this.win.loadFile('src/client/login.html') + return } - - dialog.showMessageBox(dialogOpts).then((returnValue) => { - if (returnValue.response === 0) { - logger.info("Leaving application to install update...") - autoUpdater.quitAndInstall() + this.logger.info(`production version ${this.app.getVersion()}`) + + const feed = `${server}/${pkg.repository}/${process.platform}-${process.arch}/${this.app.getVersion()}` + this.autoUpdater.setFeedURL(feed) + + // TODO : replace dialog by automatic restart + this.autoUpdater.on('update-downloaded', (_event, releaseNotes, releaseName) => { + const dialogOpts = { + type: 'info', + buttons: ['Rédémarrer', 'Plus tard'], + title: 'Une mise à jour du launcher est disponible', + message: process.platform === 'win32' ? releaseNotes : releaseName, + detail: 'Une nouvelle version du launcher a été téléchargé. Redémarrez l\'application pour appliquer les mises à jour.' } + + this.dialog.showMessageBox(dialogOpts).then((returnValue) => { + if (returnValue.response === 0) { + this.logger.info("Leaving application to install update...") + this.autoUpdater.quitAndInstall() + } + }) }) - }) + + } - autoUpdater.on('error', message => { - showNotification("Impossible de mettre à jour le launcher", "vérifier votre connexion", "warning") - logger.error('There was a problem updating the application') - logger.error(message) - }) + checkForUpdates(win, showNotification) { + this.logger.info("Checking for update...") + this.autoUpdater.checkForUpdates() + this.autoUpdater.on('error', message => { + this.logger.error('There was a problem updating the application') + this.logger.error(message) + win.loadFile('src/client/login.html').then(() => { + showNotification("Une erreur est survenue lors de la vérification de la mise à jour", "Veuillez vérifier votre connexion internet et réessayer", "error") + }) + }) + + this.autoUpdater.on('update-available', () => { + this.logger.info("update available, downloading...") + win.webContents.send("update-available") + }) + this.autoUpdater.on("update-not-available", () => { + this.logger.info("update not available") + win.loadFile('src/client/login.html') + }) + } - autoUpdater.on('update-available', () => { - showNotification("Mise à jour", "Téléchargement de la mise à jour", "warning") - logger.info("update available, downloading...") - }) } module.exports = { - configUpdater + Updater }