From 29eef3ad174bf376566bbb3416a55644170d6450 Mon Sep 17 00:00:00 2001 From: Renaud Heluin Date: Thu, 10 Oct 2024 20:55:01 +0200 Subject: [PATCH] feat: auto update for Linux --- src/class/LinuxUpdate.ts | 8 ++++++ src/interface.d.ts | 3 +++ src/locales/en/translation.json | 1 + src/locales/fr/translation.json | 1 + src/main/main.ts | 40 ++++++++++++++++++++++++++---- src/renderer/MainWindow/App.tsx | 16 ++++++++++++ src/renderer/MainWindow/preload.ts | 4 +++ src/shared/constants.ts | 1 + 8 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 src/class/LinuxUpdate.ts diff --git a/src/class/LinuxUpdate.ts b/src/class/LinuxUpdate.ts new file mode 100644 index 0000000..e63970f --- /dev/null +++ b/src/class/LinuxUpdate.ts @@ -0,0 +1,8 @@ +export class LinuxUpdate { + readonly latestReleaseVersion: string + readonly latestReleaseURL: string + public constructor(version: string, url: string) { + this.latestReleaseVersion = version + this.latestReleaseURL = url + } +} diff --git a/src/interface.d.ts b/src/interface.d.ts index b9a291c..cb23e13 100644 --- a/src/interface.d.ts +++ b/src/interface.d.ts @@ -1,3 +1,5 @@ +import { LinuxUpdate } from './class/LinuxUpdate' + export interface IVersionsAPI { node: () => string chrome: () => string @@ -17,6 +19,7 @@ export interface IElectronAPI { sendLogToFront: (callback) => string sendMessageToFrontLog: (callback) => object sendDatasToFront: (callback) => object + handleNewLinuxVersion: (callback) => LinuxUpdate // Front → Main getInitialTranslations: () => Promise handleSetFolderOuput: () => Promise diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json index dd426b2..ae8e949 100644 --- a/src/locales/en/translation.json +++ b/src/locales/en/translation.json @@ -9,6 +9,7 @@ "3. Launch the measures": "3. Launch the measures", "A new version has been downloaded. Restart the application to apply the updates.": "A new version has been downloaded. Restart the application to apply the updates.", "A new version of EcoindexApp is available!": "A new version of EcoindexApp is available!", + "A new version of the app is avalaible ({{version}}), do you want to download it?": "A new version of the app is avalaible ({{version}}), do you want to download it?", "About": "About", "Add a course": "Add a course", "Add an ExtraHeader item": "Add an ExtraHeader item", diff --git a/src/locales/fr/translation.json b/src/locales/fr/translation.json index 51ec145..ac4deda 100644 --- a/src/locales/fr/translation.json +++ b/src/locales/fr/translation.json @@ -9,6 +9,7 @@ "3. Launch the measures": "3. Lancer la mesure", "A new version has been downloaded. Restart the application to apply the updates.": "Une nouvelle version a été téléchargée. Redémarrez l'application pour appliquer les mises à jour.", "A new version of EcoindexApp is available!": "Une nouvelle version d'EcoindexApp est disponible !", + "A new version of the app is avalaible ({{version}}), do you want to download it?": "Une nouvelle version de l'application ({{version}}) est disponible, voulez-vous la télécharger ?", "About": "A propos", "Add a course": "Ajouter un parcours", "Add an ExtraHeader item": "Ajouter un élément à ExtraHeader", diff --git a/src/main/main.ts b/src/main/main.ts index 5340fc8..1fca720 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -16,8 +16,8 @@ import { handleSimpleCollect, } from './handlers/HandleCollectAll' +import { LinuxUpdate } from '../class/LinuxUpdate' import Store from 'electron-store' -import Updater from './Updater' import { channels } from '../shared/constants' import { convertVersion } from './utils' import fixPath from 'fix-path' @@ -54,10 +54,15 @@ log.info( ) // #region update app -updateElectronApp({ - updateInterval: '1 hour', - logger: updateLog, -}) +if (os.platform() !== 'linux') { + updateElectronApp({ + updateInterval: '1 hour', + logger: updateLog, + }) +} else { + // Implement alternative solution. + // see below in createMainWindow() +} // This allows TypeScript to pick up the magic constants that's auto-generated by Forge's Webpack // plugin that tells the Electron app where to look for the Webpack-bundled app code (depending on // whether you're running in development or production). @@ -254,6 +259,31 @@ const createMainWindow = (): void => { i18nInit() _changeLanguage(store.get(`language`, `en`) as string) + + // Linux Updater + const checkLinuxUpdater = async () => { + const url = + 'https://api.github.com/repos/cnumr/EcoindexApp/releases/latest' + const tags = await fetch(url).then((_) => _.json()) + const currentVersion = packageJson.version + const lastVersion = tags['tag_name'] + if (currentVersion !== lastVersion) { + const lastVersionURL = tags['html_url'] + mainLog.debug(`currentVersion`, currentVersion) + mainLog.debug(`lastVersion`, lastVersion) + mainLog.debug(`lastVersionURL`, lastVersionURL) + mainLog.debug(`Update Needed!`) + const linuxUpdate = new LinuxUpdate(lastVersion, lastVersionURL) + getMainWindow().webContents.send( + channels.ALERT_LINUX_UPDATE, + linuxUpdate + ) + } + } + + if (os.platform() === 'linux') { + checkLinuxUpdater() + } // Open the DevTools. // mainWindow.webContents.openDevTools({ mode: 'detach' }) } diff --git a/src/renderer/MainWindow/App.tsx b/src/renderer/MainWindow/App.tsx index 0ac320e..342d4d1 100644 --- a/src/renderer/MainWindow/App.tsx +++ b/src/renderer/MainWindow/App.tsx @@ -21,6 +21,7 @@ import { Header } from '../components/Header' import { InitErrorAlerts } from '../components/initialization-error-alerts' import { Input } from '../ui/input' import { JsonPanMesure } from '../components/json-pan' +import { LinuxUpdate } from '../../class/LinuxUpdate' import { MySkeleton } from '../components/my-skeleton' import { PopinLoading } from '../components/loading-popin' import { ReloadIcon } from '@radix-ui/react-icons' @@ -359,6 +360,21 @@ function TheApp() { * Detect window opening. */ useEffect(() => { + /** + * Handler (main->front), get LinuxUpdate from main + */ + window.electronAPI.handleNewLinuxVersion((linuxUpdate: LinuxUpdate) => { + frontLog.debug(`linuxUpdate`, linuxUpdate) + const resp = window.confirm( + t( + `A new version of the app is avalaible ({{version}}), do you want to download it?`, + { version: linuxUpdate.latestReleaseVersion } + ) + ) + if (resp === true) { + window.open(linuxUpdate.latestReleaseURL, `_blank`) + } + }) /** * Handler (main->front), get data from main */ diff --git a/src/renderer/MainWindow/preload.ts b/src/renderer/MainWindow/preload.ts index d774a5a..1d3cff3 100644 --- a/src/renderer/MainWindow/preload.ts +++ b/src/renderer/MainWindow/preload.ts @@ -82,6 +82,10 @@ contextBridge.exposeInMainWorld('electronAPI', { channels.CHANGE_LANGUAGE_TO_FRONT, (_event, languageCode) => callback(languageCode) ), + handleNewLinuxVersion: (callback: any) => + ipcRenderer.on(channels.ALERT_LINUX_UPDATE, (_event, linuxUpdate) => + callback(linuxUpdate) + ), }) contextBridge.exposeInMainWorld('store', { diff --git a/src/shared/constants.ts b/src/shared/constants.ts index 7a3d9d6..b239d7c 100644 --- a/src/shared/constants.ts +++ b/src/shared/constants.ts @@ -38,6 +38,7 @@ const constants: IConstants = { CHANGE_LANGUAGE_TO_FRONT: 'change-language-to-front', INSTALL_PUPPETEER_BROWSER: 'install-puppeteer-browser', SHOW_HIDE_WELCOME_WINDOW: 'show-hide-welcome-window', + ALERT_LINUX_UPDATE: 'alert-linux-update', }, scripts: { GET_NODE: 'get-node',