mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Abstract even more Electron specifics to make them overridable in unit tests
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
068f9f67da
commit
b76ff351ac
@ -44,9 +44,8 @@ autoUpdater.logger = {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* starts the automatic update checking
|
* starts the automatic update checking
|
||||||
* @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 = (isAutoUpdateEnabled : () => boolean) => once(function (interval = 1000 * 60 * 60 * 24): void {
|
||||||
if (!isAutoUpdateEnabled() || isTestEnv) {
|
if (!isAutoUpdateEnabled() || isTestEnv) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/main/electron-app/get-command-line-switch.injectable.ts
Normal file
18
src/main/electron-app/get-command-line-switch.injectable.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import electronAppInjectable from "./electron-app.injectable";
|
||||||
|
|
||||||
|
const getCommandLineSwitchInjectable = getInjectable({
|
||||||
|
id: "get-command-line-switch",
|
||||||
|
|
||||||
|
instantiate: (di) => {
|
||||||
|
const app = di.inject(electronAppInjectable);
|
||||||
|
|
||||||
|
return (name: string) => app.commandLine.getSwitchValue(name);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default getCommandLineSwitchInjectable;
|
||||||
@ -42,6 +42,8 @@ import whenSecondInstanceInjectable from "./electron-app/when-second-instance.in
|
|||||||
import whenSystemShutdownInjectable from "./electron-app/when-system-shutdown.injectable";
|
import whenSystemShutdownInjectable from "./electron-app/when-system-shutdown.injectable";
|
||||||
import exitAppInjectable from "./electron-app/exit-app.injectable";
|
import exitAppInjectable from "./electron-app/exit-app.injectable";
|
||||||
import setApplicationNameInjectable from "./electron-app/set-application-name.injectable";
|
import setApplicationNameInjectable from "./electron-app/set-application-name.injectable";
|
||||||
|
import getCommandLineSwitchInjectable from "./electron-app/get-command-line-switch.injectable";
|
||||||
|
import isAutoUpdateEnabledInjectable from "./is-auto-update-enabled.injectable";
|
||||||
|
|
||||||
export const getDiForUnitTesting = (
|
export const getDiForUnitTesting = (
|
||||||
{ doGeneralOverrides } = { doGeneralOverrides: false },
|
{ doGeneralOverrides } = { doGeneralOverrides: false },
|
||||||
@ -77,6 +79,8 @@ export const getDiForUnitTesting = (
|
|||||||
di.override(whenSystemShutdownInjectable, () => () => {});
|
di.override(whenSystemShutdownInjectable, () => () => {});
|
||||||
di.override(exitAppInjectable, () => () => {});
|
di.override(exitAppInjectable, () => () => {});
|
||||||
di.override(setApplicationNameInjectable, () => () => {});
|
di.override(setApplicationNameInjectable, () => () => {});
|
||||||
|
di.override(getCommandLineSwitchInjectable, () => () => "irrelevant");
|
||||||
|
di.override(isAutoUpdateEnabledInjectable, () => () => false);
|
||||||
|
|
||||||
// eslint-disable-next-line unused-imports/no-unused-vars-ts
|
// eslint-disable-next-line unused-imports/no-unused-vars-ts
|
||||||
di.override(extensionsStoreInjectable, () => ({ isEnabled: ({ id, isBundled }) => false }) as ExtensionsStore);
|
di.override(extensionsStoreInjectable, () => ({ isEnabled: ({ id, isBundled }) => false }) as ExtensionsStore);
|
||||||
|
|||||||
@ -3,11 +3,16 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { isAutoUpdateEnabled } from "./app-updater";
|
import { isPublishConfigured } from "../common/vars";
|
||||||
|
import { autoUpdater } from "electron-updater";
|
||||||
|
|
||||||
const isAutoUpdateEnabledInjectable = getInjectable({
|
const isAutoUpdateEnabledInjectable = getInjectable({
|
||||||
id: "is-auto-update-enabled",
|
id: "is-auto-update-enabled",
|
||||||
instantiate: () => isAutoUpdateEnabled,
|
|
||||||
|
instantiate: () => () => {
|
||||||
|
return autoUpdater.isUpdaterActive() && isPublishConfigured;
|
||||||
|
},
|
||||||
|
|
||||||
causesSideEffects: true,
|
causesSideEffects: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,27 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { app } from "electron";
|
|
||||||
|
|
||||||
export function mangleProxyEnv() {
|
|
||||||
const switchValue = app.commandLine.getSwitchValue("proxy-server");
|
|
||||||
|
|
||||||
let httpsProxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || "";
|
|
||||||
|
|
||||||
delete process.env.HTTPS_PROXY;
|
|
||||||
delete process.env.HTTP_PROXY;
|
|
||||||
|
|
||||||
if (switchValue !== "") {
|
|
||||||
httpsProxy = switchValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (httpsProxy !== "") {
|
|
||||||
process.env.APP_HTTPS_PROXY = httpsProxy;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (app.commandLine.getSwitchValue("proxy-server") !== "") {
|
|
||||||
process.env.HTTPS_PROXY = app.commandLine.getSwitchValue("proxy-server");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -3,17 +3,39 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { mangleProxyEnv } from "../../../proxy-env";
|
|
||||||
import { beforeApplicationIsReadyInjectionToken } from "../before-application-is-ready-injection-token";
|
import { beforeApplicationIsReadyInjectionToken } from "../before-application-is-ready-injection-token";
|
||||||
|
import getCommandLineSwitchInjectable from "../../../electron-app/get-command-line-switch.injectable";
|
||||||
|
|
||||||
const setupProxyEnvInjectable = getInjectable({
|
const setupProxyEnvInjectable = getInjectable({
|
||||||
id: "setup-proxy-env",
|
id: "setup-proxy-env",
|
||||||
|
|
||||||
instantiate: () => ({
|
instantiate: (di) => {
|
||||||
run: () => {
|
const getCommandLineSwitch = di.inject(getCommandLineSwitchInjectable);
|
||||||
mangleProxyEnv();
|
|
||||||
},
|
return {
|
||||||
}),
|
run: () => {
|
||||||
|
const switchValue = getCommandLineSwitch("proxy-server");
|
||||||
|
|
||||||
|
let httpsProxy =
|
||||||
|
process.env.HTTPS_PROXY || process.env.HTTP_PROXY || "";
|
||||||
|
|
||||||
|
delete process.env.HTTPS_PROXY;
|
||||||
|
delete process.env.HTTP_PROXY;
|
||||||
|
|
||||||
|
if (switchValue !== "") {
|
||||||
|
httpsProxy = switchValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (httpsProxy !== "") {
|
||||||
|
process.env.APP_HTTPS_PROXY = httpsProxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getCommandLineSwitch("proxy-server") !== "") {
|
||||||
|
process.env.HTTPS_PROXY = getCommandLineSwitch("proxy-server");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
injectionToken: beforeApplicationIsReadyInjectionToken,
|
injectionToken: beforeApplicationIsReadyInjectionToken,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,15 +5,21 @@
|
|||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { onRootFrameRenderInjectionToken } from "../on-root-frame-render-injection-token";
|
import { onRootFrameRenderInjectionToken } from "../on-root-frame-render-injection-token";
|
||||||
import { startUpdateChecking } from "../../../app-updater";
|
import { startUpdateChecking } from "../../../app-updater";
|
||||||
|
import isAutoUpdateEnabledInjectable from "../../../is-auto-update-enabled.injectable";
|
||||||
|
|
||||||
const startUpdateCheckingInjectable = getInjectable({
|
const startUpdateCheckingInjectable = getInjectable({
|
||||||
id: "start-update-checking",
|
id: "start-update-checking",
|
||||||
|
|
||||||
instantiate: () => ({
|
instantiate: (di) => {
|
||||||
run: () => {
|
const isAutoUpdateEnabled = di.inject(isAutoUpdateEnabledInjectable);
|
||||||
startUpdateChecking();
|
|
||||||
},
|
|
||||||
}),
|
return {
|
||||||
|
run: () => {
|
||||||
|
startUpdateChecking(isAutoUpdateEnabled)();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
causesSideEffects: true,
|
causesSideEffects: true,
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import trayMenuItemsInjectable from "./tray-menu-items.injectable";
|
|||||||
import navigateToPreferencesInjectable from "../../common/front-end-routing/routes/preferences/navigate-to-preferences.injectable";
|
import navigateToPreferencesInjectable from "../../common/front-end-routing/routes/preferences/navigate-to-preferences.injectable";
|
||||||
import stopServicesAndExitAppInjectable from "../stop-services-and-exit-app.injectable";
|
import stopServicesAndExitAppInjectable from "../stop-services-and-exit-app.injectable";
|
||||||
import { getStartableStoppable } from "../../common/utils/get-startable-stoppable";
|
import { getStartableStoppable } from "../../common/utils/get-startable-stoppable";
|
||||||
|
import isAutoUpdateEnabledInjectable from "../is-auto-update-enabled.injectable";
|
||||||
|
|
||||||
const trayInjectable = getInjectable({
|
const trayInjectable = getInjectable({
|
||||||
id: "tray",
|
id: "tray",
|
||||||
@ -18,6 +19,7 @@ const trayInjectable = getInjectable({
|
|||||||
const trayMenuItems = di.inject(trayMenuItemsInjectable);
|
const trayMenuItems = di.inject(trayMenuItemsInjectable);
|
||||||
const navigateToPreferences = di.inject(navigateToPreferencesInjectable);
|
const navigateToPreferences = di.inject(navigateToPreferencesInjectable);
|
||||||
const stopServicesAndExitApp = di.inject(stopServicesAndExitAppInjectable);
|
const stopServicesAndExitApp = di.inject(stopServicesAndExitAppInjectable);
|
||||||
|
const isAutoUpdateEnabled = di.inject(isAutoUpdateEnabledInjectable);
|
||||||
|
|
||||||
return getStartableStoppable(() =>
|
return getStartableStoppable(() =>
|
||||||
initTray(
|
initTray(
|
||||||
@ -25,6 +27,7 @@ const trayInjectable = getInjectable({
|
|||||||
trayMenuItems,
|
trayMenuItems,
|
||||||
navigateToPreferences,
|
navigateToPreferences,
|
||||||
stopServicesAndExitApp,
|
stopServicesAndExitApp,
|
||||||
|
isAutoUpdateEnabled,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import { Menu, Tray } from "electron";
|
|||||||
import type { IComputedValue } from "mobx";
|
import type { IComputedValue } from "mobx";
|
||||||
import { autorun } from "mobx";
|
import { autorun } from "mobx";
|
||||||
import { showAbout } from "../menu/menu";
|
import { showAbout } from "../menu/menu";
|
||||||
import { checkForUpdates, isAutoUpdateEnabled } from "../app-updater";
|
import { checkForUpdates } from "../app-updater";
|
||||||
import type { WindowManager } from "../window-manager";
|
import type { WindowManager } from "../window-manager";
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
import { isDevelopment, isWindows, productName, staticFilesDirectory } from "../../common/vars";
|
import { isDevelopment, isWindows, productName, staticFilesDirectory } from "../../common/vars";
|
||||||
@ -35,6 +35,7 @@ export function initTray(
|
|||||||
trayMenuItems: IComputedValue<TrayMenuRegistration[]>,
|
trayMenuItems: IComputedValue<TrayMenuRegistration[]>,
|
||||||
navigateToPreferences: () => void,
|
navigateToPreferences: () => void,
|
||||||
stopServicesAndExitApp: () => void,
|
stopServicesAndExitApp: () => void,
|
||||||
|
isAutoUpdateEnabled: () => boolean,
|
||||||
): Disposer {
|
): Disposer {
|
||||||
const icon = getTrayIconPath();
|
const icon = getTrayIconPath();
|
||||||
|
|
||||||
@ -53,7 +54,7 @@ export function initTray(
|
|||||||
return disposer(
|
return disposer(
|
||||||
autorun(() => {
|
autorun(() => {
|
||||||
try {
|
try {
|
||||||
const menu = createTrayMenu(windowManager, toJS(trayMenuItems.get()), navigateToPreferences, stopServicesAndExitApp);
|
const menu = createTrayMenu(windowManager, toJS(trayMenuItems.get()), navigateToPreferences, stopServicesAndExitApp, isAutoUpdateEnabled);
|
||||||
|
|
||||||
tray.setContextMenu(menu);
|
tray.setContextMenu(menu);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -82,6 +83,7 @@ function createTrayMenu(
|
|||||||
extensionTrayItems: TrayMenuRegistration[],
|
extensionTrayItems: TrayMenuRegistration[],
|
||||||
navigateToPreferences: () => void,
|
navigateToPreferences: () => void,
|
||||||
stopServicesAndExitApp: () => void,
|
stopServicesAndExitApp: () => void,
|
||||||
|
isAutoUpdateEnabled: () => boolean,
|
||||||
): Menu {
|
): Menu {
|
||||||
let template: Electron.MenuItemConstructorOptions[] = [
|
let template: Electron.MenuItemConstructorOptions[] = [
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user