2023-03-06 01:30:37 +01:00
|
|
|
import { autoUpdater } from "electron-updater"
|
|
|
|
import { app, dialog, ipcMain } from "electron"
|
2023-12-06 13:31:47 +01:00
|
|
|
import CONFIG from "../config"
|
2023-03-06 01:30:37 +01:00
|
|
|
|
|
|
|
import {
|
|
|
|
UPDATE_AVAILABLE_EVENT,
|
|
|
|
UPDATE_NOT_AVAILABLE_EVENT,
|
|
|
|
UPDATE_DOWNLOADED,
|
|
|
|
UPDATE_DOWNLOAD_PROGRESS,
|
|
|
|
UPDATE_ERROR,
|
|
|
|
UPDATE_START_DOWNLOAD,
|
|
|
|
UPDATE_INSTALL_AND_RESTART,
|
2023-03-06 10:24:23 +01:00
|
|
|
UPDATE_CHECK_FOR_UPDATES,
|
2023-12-06 13:31:47 +01:00
|
|
|
} from '../constants'
|
2024-01-06 21:26:05 +01:00
|
|
|
import { setForceQuit } from "./index";
|
2023-03-06 01:30:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
// will reference the main window
|
|
|
|
let window
|
|
|
|
|
|
|
|
// configure logging
|
|
|
|
const log = require('electron-log');
|
|
|
|
|
|
|
|
autoUpdater.logger = log
|
|
|
|
autoUpdater.logger.transports.file.level = "info"
|
|
|
|
|
|
|
|
autoUpdater.autoDownload = false
|
2023-12-07 01:50:49 +01:00
|
|
|
autoUpdater.allowDowngrade = true
|
2023-03-06 01:30:37 +01:00
|
|
|
|
|
|
|
autoUpdater.on('error', (error) => {
|
|
|
|
window?.webContents.send(UPDATE_ERROR, error == null ? "unknown" : (error.stack || error).toString())
|
|
|
|
//dialog.showErrorBox('Error: ', error == null ? "unknown" : (error.stack || error).toString())
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.on('update-available', (info) => {
|
|
|
|
window?.webContents.send(UPDATE_AVAILABLE_EVENT, {
|
|
|
|
version: info.version,
|
|
|
|
releaseDate: info.releaseDate,
|
|
|
|
currentVersion: app.getVersion(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.on('update-not-available', () => {
|
|
|
|
window?.webContents.send(UPDATE_NOT_AVAILABLE_EVENT)
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.on('update-downloaded', () => {
|
|
|
|
window?.webContents.send(UPDATE_DOWNLOADED)
|
|
|
|
})
|
|
|
|
|
|
|
|
autoUpdater.on('download-progress', (info) => {
|
|
|
|
window?.webContents.send(UPDATE_DOWNLOAD_PROGRESS, {
|
|
|
|
percent: info.percent,
|
|
|
|
total: info.total,
|
|
|
|
transferred: info.transferred,
|
|
|
|
bytesPerSecond: info.bytesPerSecond,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// handle messages from Vue components
|
|
|
|
ipcMain.handle(UPDATE_START_DOWNLOAD, () => {
|
|
|
|
autoUpdater.downloadUpdate()
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.handle(UPDATE_INSTALL_AND_RESTART, () => {
|
2024-01-06 21:26:05 +01:00
|
|
|
setImmediate(() => {
|
|
|
|
// make sure we can quite the app if it's in the Tray/Menu bar
|
|
|
|
setForceQuit()
|
|
|
|
autoUpdater.quitAndInstall(true, true)
|
|
|
|
})
|
2023-03-06 01:30:37 +01:00
|
|
|
})
|
|
|
|
|
2023-12-06 13:31:47 +01:00
|
|
|
|
|
|
|
export function checkForUpdates() {
|
2023-12-07 00:42:29 +01:00
|
|
|
autoUpdater.allowPrerelease = CONFIG.get("settings.allowBetaVersions")
|
2023-03-06 10:24:23 +01:00
|
|
|
autoUpdater.checkForUpdates()
|
2023-03-07 01:45:23 +01:00
|
|
|
// for development, the autoUpdater will not work, so we need to trigger the event manually
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
window?.webContents.send(UPDATE_NOT_AVAILABLE_EVENT)
|
|
|
|
}
|
2023-12-06 13:31:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ipcMain.handle(UPDATE_CHECK_FOR_UPDATES, () => {
|
|
|
|
checkForUpdates()
|
2023-03-06 10:24:23 +01:00
|
|
|
})
|
|
|
|
|
2023-03-07 01:45:23 +01:00
|
|
|
export function initializeAutoUpdate(win) {
|
2023-03-06 01:30:37 +01:00
|
|
|
window = win
|
2023-12-07 00:42:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* To debug auto updates (actually downloading an update won't work),
|
|
|
|
* uncomment the lines below, and create a dev-app-update.yml with the content:
|
|
|
|
*
|
|
|
|
* owner: heyman
|
|
|
|
* repo: heynote
|
|
|
|
* provider: github
|
|
|
|
*/
|
|
|
|
// Useful for some dev/debugging tasks, but download can
|
|
|
|
// not be validated becuase dev app is not signed
|
|
|
|
//autoUpdater.updateConfigPath = "/Users/heyman/projects/heynote/dev-app-update.yml" //path.join(__dirname, 'dev-app-update.yml');
|
|
|
|
//autoUpdater.forceDevUpdateConfig = true;
|
2023-03-06 01:30:37 +01:00
|
|
|
}
|