import { app } from 'electron' import electronUpdater, { type UpdateInfo, type ProgressInfo } from 'electron-updater' import { emit } from './events' /** * Auto-update du launcher via electron-updater (provider "generic" pointant sur * une release Gitea à tag fixe, voir electron-builder.yml). * * UX : check au démarrage -> téléchargement en fond -> on relaie l'état vers le * renderer (bandeau). L'install effective se fait quand l'utilisateur clique * "Redémarrer pour installer" (quitAndInstallUpdate), ou à la fermeture. */ // electron-updater est CommonJS : on récupère autoUpdater via le default export. const { autoUpdater } = electronUpdater /** Re-check périodique (6 h) tant que le launcher reste ouvert. */ const RECHECK_INTERVAL_MS = 6 * 60 * 60 * 1000 export function initUpdater(): void { // L'updater ne fonctionne que sur une app packagée (sauf dev-app-update.yml). if (!app.isPackaged) { console.info('[updater] dev non packagé : auto-update désactivé.') return } autoUpdater.autoDownload = true // Sur Linux (AppImage), l'install automatique au quit utilise execFileSync // et bloque le processus en attendant que le nouveau AppImage se ferme — // comportement très inattendu. On désactive : l'utilisateur clique le bouton // "Redémarrer pour installer" qui, lui, utilise spawnLog (async) et fonctionne. autoUpdater.autoInstallOnAppQuit = process.platform !== 'linux' autoUpdater.on('checking-for-update', () => { emit.updateStatus({ state: 'checking' }) }) autoUpdater.on('update-available', (info: UpdateInfo) => { emit.updateStatus({ state: 'available', version: info.version }) }) autoUpdater.on('update-not-available', () => { emit.updateStatus({ state: 'none' }) }) autoUpdater.on('download-progress', (p: ProgressInfo) => { emit.updateStatus({ state: 'downloading', progress: p.percent / 100 }) }) autoUpdater.on('update-downloaded', (info: UpdateInfo) => { emit.updateStatus({ state: 'downloaded', version: info.version }) }) autoUpdater.on('error', (err: Error) => { console.error('[updater]', err) emit.updateStatus({ state: 'error', message: err.message }) }) void autoUpdater.checkForUpdates() setInterval(() => void autoUpdater.checkForUpdates(), RECHECK_INTERVAL_MS) } /** Quitte et installe la mise à jour téléchargée (appelé par le bouton UI). */ export function quitAndInstallUpdate(): void { autoUpdater.quitAndInstall() }