1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/app-updater.ts
Sebastian Malton b0de85d21a add auto-update notifications and confirmation
Signed-off-by: Sebastian Malton <sebastian@malton.name>

Show single update notification (#1985)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

Moving notification icons to top (#1987)

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

Switch to EventEmitter (producer&consumer) model

- Main now broadcasts messages whenever updates are available and waits
  for the first response back

- Add `onCorrect` and `onceCorrect` to ipc module for typechecking ipc
  messages

- Correctly port styling from 3.6

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2021-02-02 08:44:34 -05:00

72 lines
2.6 KiB
TypeScript

import { autoUpdater, UpdateInfo } from "electron-updater";
import logger from "./logger";
import { isDevelopment, isTestEnv } from "../common/vars";
import { delay } from "../common/utils";
import { areArgsUpdateAvailableToBackchannel, AutoUpdateLogPrefix, broadcastMessage, onceCorrect, UpdateAvailableChannel, UpdateAvailableToBackchannel } from "../common/ipc";
import * as uuid from "uuid";
import { ipcMain } from "electron";
function handleAutoUpdateBackChannel(event: Electron.IpcMainEvent, ...[arg]: UpdateAvailableToBackchannel) {
if (arg.doUpdate) {
if (arg.now) {
logger.info(`${AutoUpdateLogPrefix}: User chose to update now`);
autoUpdater.downloadUpdate()
.then(() => autoUpdater.quitAndInstall())
.catch(error => logger.error(`${AutoUpdateLogPrefix}: Failed to download or install update`, { error }));
} else {
logger.info(`${AutoUpdateLogPrefix}: User chose to update on quit`);
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.downloadUpdate()
.catch(error => logger.error(`${AutoUpdateLogPrefix}: Failed to download update`, { error }));
}
} else {
logger.info(`${AutoUpdateLogPrefix}: User chose not to update`);
}
}
/**
* starts the automatic update checking
* @param interval milliseconds between interval to check on, defaults to 24h
*/
export function startUpdateChecking(interval = 1000 * 60 * 60 * 24): void {
if (isDevelopment || isTestEnv) {
return;
}
autoUpdater.logger = logger;
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = false;
autoUpdater
.on("update-available", (args: UpdateInfo) => {
try {
// use a UUID so that this back-channel is harder to discover
const backchannel = uuid.v4();
// make sure that the handler is in place before broadcasting (prevent race-condition)
onceCorrect(ipcMain, backchannel, handleAutoUpdateBackChannel, areArgsUpdateAvailableToBackchannel);
logger.info(`${AutoUpdateLogPrefix}: broadcasting update available`, { backchannel, version: args.version });
broadcastMessage(UpdateAvailableChannel, backchannel, args);
} catch (error) {
logger.error(`${AutoUpdateLogPrefix}: broadcasting failed`, { error });
}
});
async function helper() {
while (true) {
await checkForUpdates();
await delay(interval);
}
}
helper();
}
export async function checkForUpdates(): Promise<void> {
try {
await autoUpdater.checkForUpdates();
} catch (error) {
logger.error(`${AutoUpdateLogPrefix}: failed with an error`, { error: String(error) });
}
}