Files
Bridge-Multi/src-electron/ipc/UpdateHandler.ipc.ts
2023-11-28 19:50:45 -06:00

72 lines
1.7 KiB
TypeScript

import { autoUpdater, UpdateInfo } from 'electron-updater'
import { inspect } from 'util'
import { UpdateProgress } from '../../src-shared/interfaces/update.interface'
import { emitIpcEvent } from '../main'
let updateAvailable: boolean | null = false
autoUpdater.autoDownload = false
autoUpdater.logger = null
autoUpdater.on('error', (err: Error) => {
updateAvailable = null
emitIpcEvent('updateError', inspect(err))
})
autoUpdater.on('update-available', (info: UpdateInfo) => {
updateAvailable = true
emitIpcEvent('updateAvailable', info)
})
autoUpdater.on('update-not-available', () => {
updateAvailable = false
emitIpcEvent('updateAvailable', null)
})
export async function retryUpdate() {
try {
await autoUpdater.checkForUpdates()
} catch (err) {
updateAvailable = null
emitIpcEvent('updateError', inspect(err))
}
}
export async function getUpdateAvailable() {
return updateAvailable
}
/**
* @returns the current version of Bridge.
*/
export async function getCurrentVersion() {
return autoUpdater.currentVersion.raw
}
/**
* Begins the process of downloading the latest update.
*/
export function downloadUpdate() {
if (this.downloading) { return }
this.downloading = true
autoUpdater.on('download-progress', (updateProgress: UpdateProgress) => {
emitIpcEvent('updateProgress', updateProgress)
})
autoUpdater.on('update-downloaded', () => {
emitIpcEvent('updateDownloaded', undefined)
})
autoUpdater.downloadUpdate()
}
/**
* Immediately closes the application and installs the update.
*/
export function quitAndInstall() {
autoUpdater.quitAndInstall() // autoUpdater installs a downloaded update on the next program restart by default
}