Skip to content

Commit

Permalink
feat: auto update for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
hrenaud committed Oct 10, 2024
1 parent 65b3899 commit 29eef3a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/class/LinuxUpdate.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions src/interface.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LinuxUpdate } from './class/LinuxUpdate'

export interface IVersionsAPI {
node: () => string
chrome: () => string
Expand All @@ -17,6 +19,7 @@ export interface IElectronAPI {
sendLogToFront: (callback) => string
sendMessageToFrontLog: (callback) => object
sendDatasToFront: (callback) => object
handleNewLinuxVersion: (callback) => LinuxUpdate
// Front → Main
getInitialTranslations: () => Promise<object>
handleSetFolderOuput: () => Promise<string>
Expand Down
1 change: 1 addition & 0 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 35 additions & 5 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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' })
}
Expand Down
16 changes: 16 additions & 0 deletions src/renderer/MainWindow/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
*/
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/MainWindow/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
1 change: 1 addition & 0 deletions src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 29eef3a

Please sign in to comment.