Last active
December 2, 2023 08:20
-
-
Save tkroo/62c3a9827b0316f0b577e15f0093376c to your computer and use it in GitHub Desktop.
electron-updater with confirm dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage | |
// in main: | |
// import { checkForUpdates } from './updater' | |
// app.whenReady().then(() => { | |
// ... | |
// checkForUpdates() | |
// createWindow() | |
// ... | |
// } | |
// you may need to adjust the getMainWindow function depending on your app | |
import { app, dialog, ipcMain } from 'electron' | |
import { autoUpdater } from 'electron-updater' | |
let dialogShown = false | |
autoUpdater.autoDownload = false | |
autoUpdater.autoInstallOnAppQuit = false | |
ipcMain.handle('showUpdateMessage', showUpdateMessage) | |
function showUpdateMessage(message) { | |
getMainWindow().webContents.send('showUpdateMessage', message) | |
} | |
function confirmUpdate(info) { | |
const options = { | |
type: 'question', | |
title: 'imageconverter2 update available', | |
message: `Update to version ${info.version} ?`, | |
detail: `current version: ${app.getVersion()}`, | |
buttons: ['no', 'yes'], | |
cancelId: 0 | |
} | |
dialog.showMessageBox(getMainWindow(), options) | |
.then((result) => { | |
dialogShown = true | |
if (result.response === 0) return | |
autoUpdater.autoDownload = true | |
autoUpdater.on('update-available', (info) => { | |
console.log('updating...') | |
}) | |
autoUpdater.checkForUpdates() | |
}) | |
} | |
export function checkForUpdates() { | |
if (app.isPackaged) { | |
autoUpdater.checkForUpdates() | |
autoUpdater.on('update-available', (info) => { | |
showUpdateMessage(`${app.name} ${app.getVersion()}<br/>update available: ${info.version}`) | |
if(!dialogShown) confirmUpdate(info) | |
}) | |
autoUpdater.on('update-not-available', (info) => { | |
showUpdateMessage(`${app.name} ${app.getVersion()}`) | |
}) | |
autoUpdater.on('update-downloaded', (info) => { | |
showUpdateMessage(`version ${info.version} downloaded.`) | |
autoUpdater.quitAndInstall() | |
}) | |
} | |
} | |
function getMainWindow() { | |
const allWindows = BrowserWindow.getAllWindows() | |
const win = allWindows[allWindows.length - 1] | |
return win | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment