1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add Check-For-Updates in menu and some notifications (#4437)

This commit is contained in:
Sebastian Malton 2021-11-30 10:30:42 -05:00 committed by GitHub
parent 7ce4a87ac8
commit f0274d08a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 19 deletions

View File

@ -37,6 +37,7 @@ function getBuildChannel(): string {
case "rc": case "rc":
return preRelease; return preRelease;
case undefined: case undefined:
case "latest":
return "latest"; // needed because electron-updater does not take build information into account when resolving if update is available return "latest"; // needed because electron-updater does not take build information into account when resolving if update is available
default: default:
throw new Error(`invalid pre-release ${preRelease}`); throw new Error(`invalid pre-release ${preRelease}`);

View File

@ -22,6 +22,8 @@
import type { UpdateInfo } from "electron-updater"; import type { UpdateInfo } from "electron-updater";
export const UpdateAvailableChannel = "update-available"; export const UpdateAvailableChannel = "update-available";
export const AutoUpdateChecking = "auto-update:checking";
export const AutoUpdateNoUpdateAvailable = "auto-update:no-update";
export const AutoUpdateLogPrefix = "[UPDATE-CHECKER]"; export const AutoUpdateLogPrefix = "[UPDATE-CHECKER]";
export type UpdateAvailableFromMain = [backChannel: string, updateInfo: UpdateInfo]; export type UpdateAvailableFromMain = [backChannel: string, updateInfo: UpdateInfo];

View File

@ -21,9 +21,9 @@
import { autoUpdater, UpdateInfo } from "electron-updater"; import { autoUpdater, UpdateInfo } from "electron-updater";
import logger from "./logger"; import logger from "./logger";
import { isDevelopment, isLinux, isMac, isPublishConfigured, isTestEnv } from "../common/vars"; import { isLinux, isMac, isPublishConfigured, isTestEnv } from "../common/vars";
import { delay } from "../common/utils"; import { delay } from "../common/utils";
import { areArgsUpdateAvailableToBackchannel, AutoUpdateLogPrefix, broadcastMessage, onceCorrect, UpdateAvailableChannel, UpdateAvailableToBackchannel } from "../common/ipc"; import { areArgsUpdateAvailableToBackchannel, AutoUpdateChecking, AutoUpdateLogPrefix, AutoUpdateNoUpdateAvailable, broadcastMessage, onceCorrect, UpdateAvailableChannel, UpdateAvailableToBackchannel } from "../common/ipc";
import { once } from "lodash"; import { once } from "lodash";
import { ipcMain } from "electron"; import { ipcMain } from "electron";
import { nextUpdateChannel } from "./utils/update-channel"; import { nextUpdateChannel } from "./utils/update-channel";
@ -68,23 +68,24 @@ function handleAutoUpdateBackChannel(event: Electron.IpcMainEvent, ...[arg]: Upd
} }
} }
autoUpdater.logger = {
info: message => logger.info(`[AUTO-UPDATE]: electron-updater:`, message),
warn: message => logger.warn(`[AUTO-UPDATE]: electron-updater:`, message),
error: message => logger.error(`[AUTO-UPDATE]: electron-updater:`, message),
debug: message => logger.debug(`[AUTO-UPDATE]: electron-updater:`, message),
};
/** /**
* starts the automatic update checking * starts the automatic update checking
* @param interval milliseconds between interval to check on, defaults to 24h * @param interval milliseconds between interval to check on, defaults to 24h
*/ */
export const startUpdateChecking = once(function (interval = 1000 * 60 * 60 * 24): void { export const startUpdateChecking = once(function (interval = 1000 * 60 * 60 * 24): void {
if (isDevelopment || isTestEnv) { if (!isAutoUpdateEnabled() || isTestEnv) {
return; return;
} }
const userStore = UserStore.getInstance(); const userStore = UserStore.getInstance();
autoUpdater.logger = {
info: message => logger.info(`[AUTO-UPDATE]: electron-updater: ${message}`),
warn: message => logger.warn(`[AUTO-UPDATE]: electron-updater: ${message}`),
error: message => logger.error(`[AUTO-UPDATE]: electron-updater: ${message}`),
debug: message => logger.debug(`[AUTO-UPDATE]: electron-updater: ${message}`),
};
autoUpdater.autoDownload = false; autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = false; autoUpdater.autoInstallOnAppQuit = false;
autoUpdater.channel = userStore.updateChannel; autoUpdater.channel = userStore.updateChannel;
@ -136,7 +137,13 @@ export const startUpdateChecking = once(function (interval = 1000 * 60 * 60 * 24
logger.info(`${AutoUpdateLogPrefix}: update not available from ${autoUpdater.channel}, will check ${nextChannel} channel next`); logger.info(`${AutoUpdateLogPrefix}: update not available from ${autoUpdater.channel}, will check ${nextChannel} channel next`);
autoUpdater.channel = nextChannel; if (nextChannel !== autoUpdater.channel) {
autoUpdater.channel = nextChannel;
autoUpdater.checkForUpdates()
.catch(error => logger.error(`${AutoUpdateLogPrefix}: failed with an error`, error));
} else {
broadcastMessage(AutoUpdateNoUpdateAvailable);
}
}); });
async function helper() { async function helper() {
@ -157,8 +164,9 @@ export async function checkForUpdates(): Promise<void> {
autoUpdater.channel = userStore.updateChannel; autoUpdater.channel = userStore.updateChannel;
autoUpdater.allowDowngrade = userStore.isAllowedToDowngrade; autoUpdater.allowDowngrade = userStore.isAllowedToDowngrade;
broadcastMessage(AutoUpdateChecking);
await autoUpdater.checkForUpdates(); await autoUpdater.checkForUpdates();
} catch (error) { } catch (error) {
logger.error(`${AutoUpdateLogPrefix}: failed with an error`, { error: String(error) }); logger.error(`${AutoUpdateLogPrefix}: failed with an error`, error);
} }
} }

View File

@ -29,6 +29,7 @@ import { exitApp } from "./exit-app";
import { broadcastMessage } from "../common/ipc"; import { broadcastMessage } from "../common/ipc";
import * as packageJson from "../../package.json"; import * as packageJson from "../../package.json";
import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../common/routes"; import { preferencesURL, extensionsURL, addClusterURL, catalogURL, welcomeURL } from "../common/routes";
import { checkForUpdates, isAutoUpdateEnabled } from "./app-updater";
export type MenuTopId = "mac" | "file" | "edit" | "view" | "help"; export type MenuTopId = "mac" | "file" | "edit" | "view" | "help";
@ -61,10 +62,8 @@ export function showAbout(browserWindow: BrowserWindow) {
} }
export function buildMenu(windowManager: WindowManager) { export function buildMenu(windowManager: WindowManager) {
function ignoreOnMac(menuItems: MenuItemConstructorOptions[]) { function ignoreIf(check: boolean, menuItems: MenuItemConstructorOptions[]) {
if (isMac) return []; return check ? [] : menuItems;
return menuItems;
} }
async function navigate(url: string) { async function navigate(url: string) {
@ -72,6 +71,10 @@ export function buildMenu(windowManager: WindowManager) {
await windowManager.navigate(url); await windowManager.navigate(url);
} }
const autoUpdateDisabled = !isAutoUpdateEnabled();
logger.info(`[MENU]: autoUpdateDisabled=${autoUpdateDisabled}`);
const macAppMenu: MenuItemsOpts = { const macAppMenu: MenuItemsOpts = {
label: app.getName(), label: app.getName(),
id: "root", id: "root",
@ -83,6 +86,13 @@ export function buildMenu(windowManager: WindowManager) {
showAbout(browserWindow); showAbout(browserWindow);
}, },
}, },
...ignoreIf(autoUpdateDisabled, [{
label: "Check for updates",
click() {
checkForUpdates()
.then(() => windowManager.ensureMainWindow());
},
}]),
{ type: "separator" }, { type: "separator" },
{ {
label: "Preferences", label: "Preferences",
@ -129,7 +139,7 @@ export function buildMenu(windowManager: WindowManager) {
navigate(addClusterURL()); navigate(addClusterURL());
}, },
}, },
...ignoreOnMac([ ...ignoreIf(isMac, [
{ type: "separator" }, { type: "separator" },
{ {
label: "Preferences", label: "Preferences",
@ -158,7 +168,7 @@ export function buildMenu(windowManager: WindowManager) {
}, },
] as MenuItemConstructorOptions[] : []), ] as MenuItemConstructorOptions[] : []),
...ignoreOnMac([ ...ignoreIf(isMac, [
{ {
label: "Exit", label: "Exit",
accelerator: "Alt+F4", accelerator: "Alt+F4",
@ -264,7 +274,7 @@ export function buildMenu(windowManager: WindowManager) {
shell.openExternal(supportUrl); shell.openExternal(supportUrl);
}, },
}, },
...ignoreOnMac([ ...ignoreIf(isMac, [
{ {
label: `About ${productName}`, label: `About ${productName}`,
id: "about", id: "about",
@ -272,6 +282,13 @@ export function buildMenu(windowManager: WindowManager) {
showAbout(browserWindow); showAbout(browserWindow);
}, },
}, },
...ignoreIf(autoUpdateDisabled, [{
label: "Check for updates",
click() {
checkForUpdates()
.then(() => windowManager.ensureMainWindow());
},
}]),
]), ]),
], ],
}; };

View File

@ -21,7 +21,7 @@
import React from "react"; import React from "react";
import { ipcRenderer, IpcRendererEvent } from "electron"; import { ipcRenderer, IpcRendererEvent } from "electron";
import { areArgsUpdateAvailableFromMain, UpdateAvailableChannel, onCorrect, UpdateAvailableFromMain, BackchannelArg, ClusterListNamespaceForbiddenChannel, isListNamespaceForbiddenArgs, ListNamespaceForbiddenArgs, HotbarTooManyItems } from "../../common/ipc"; import { areArgsUpdateAvailableFromMain, UpdateAvailableChannel, onCorrect, UpdateAvailableFromMain, BackchannelArg, ClusterListNamespaceForbiddenChannel, isListNamespaceForbiddenArgs, ListNamespaceForbiddenArgs, HotbarTooManyItems, ipcRendererOn, AutoUpdateChecking, AutoUpdateNoUpdateAvailable } from "../../common/ipc";
import { Notifications, notificationsStore } from "../components/notifications"; import { Notifications, notificationsStore } from "../components/notifications";
import { Button } from "../components/button"; import { Button } from "../components/button";
import { isMac } from "../../common/vars"; import { isMac } from "../../common/vars";
@ -153,4 +153,10 @@ export function registerIpcListeners() {
listener: HotbarTooManyItemsHandler, listener: HotbarTooManyItemsHandler,
verifier: (args: unknown[]): args is [] => args.length === 0, verifier: (args: unknown[]): args is [] => args.length === 0,
}); });
ipcRendererOn(AutoUpdateChecking, () => {
Notifications.shortInfo("Checking for updates");
});
ipcRendererOn(AutoUpdateNoUpdateAvailable, () => {
Notifications.shortInfo("No update is currently available");
});
} }