migu/electron/src/main/updater.js
2024-08-14 01:48:32 +02:00

47 lines
1.3 KiB
JavaScript

import log from 'electron-log'
import { autoUpdater } from 'electron-updater'
import { ipcMain, shell } from 'electron'
log.initialize({ spyRendererConsole: true })
log.transports.file.level = 'info'
autoUpdater.logger = log
ipcMain.on('update', () => {
autoUpdater.checkForUpdatesAndNotify()
})
autoUpdater.checkForUpdatesAndNotify()
export default class Updater {
hasUpdate = false
window
torrentWindow
/**
* @param {import('electron').BrowserWindow} window
* @param {import('electron').BrowserWindow} torrentWindow
*/
constructor (window, torrentWindow) {
this.window = window
this.torrentWindow = torrentWindow
autoUpdater.on('update-available', () => {
window.webContents.send('update-available', true)
})
autoUpdater.on('update-downloaded', () => {
this.hasUpdate = true
window.webContents.send('update-downloaded', true)
})
}
install (forceRunAfter = false) {
if (this.hasUpdate) {
setImmediate(() => {
try {
this.window.close()
this.torrentWindow.close()
} catch (e) {}
autoUpdater.quitAndInstall(true, forceRunAfter)
})
if (process.platform === 'darwin') shell.openExternal('https://miru.watch/download')
this.hasUpdate = false
return true
}
}
}