mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Abstract command line arguments to make them overridable in tests
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
605ab0683a
commit
8d2639532b
@ -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 commandLineArgumentsInjectable from "../../main/utils/command-line-arguments.injectable";
|
||||||
|
|
||||||
const isIntegrationTestingInjectable = getInjectable({
|
const isIntegrationTestingInjectable = getInjectable({
|
||||||
id: "is-integration-testing",
|
id: "is-integration-testing",
|
||||||
instantiate: () => process.argv.includes("--integration-testing"),
|
|
||||||
causesSideEffects: true,
|
instantiate: (di) => {
|
||||||
|
const commandLineArguments = di.inject(commandLineArgumentsInjectable);
|
||||||
|
|
||||||
|
return commandLineArguments.includes("--integration-testing");
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default isIntegrationTestingInjectable;
|
export default isIntegrationTestingInjectable;
|
||||||
|
|||||||
28
src/main/electron-app/register-protocol-client.injectable.ts
Normal file
28
src/main/electron-app/register-protocol-client.injectable.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
* 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 loggerInjectable from "../../common/logger.injectable";
|
||||||
|
import electronAppInjectable from "./electron-app.injectable";
|
||||||
|
|
||||||
|
const registerProtocolClientInjectable = getInjectable({
|
||||||
|
id: "register-protocol-client",
|
||||||
|
|
||||||
|
instantiate: (di) => {
|
||||||
|
const logger = di.inject(loggerInjectable);
|
||||||
|
const app = di.inject(electronAppInjectable);
|
||||||
|
|
||||||
|
return (protocol: string) => {
|
||||||
|
logger.info(`📟 Setting protocol client for ${protocol}://`);
|
||||||
|
|
||||||
|
if (app.setAsDefaultProtocolClient(protocol)) {
|
||||||
|
logger.info("📟 Protocol client register succeeded ✅");
|
||||||
|
} else {
|
||||||
|
logger.info("📟 Protocol client register failed ❗");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default registerProtocolClientInjectable;
|
||||||
@ -47,6 +47,8 @@ import isAutoUpdateEnabledInjectable from "./is-auto-update-enabled.injectable";
|
|||||||
import appEventBusInjectable from "../common/app-event-bus/app-event-bus.injectable";
|
import appEventBusInjectable from "../common/app-event-bus/app-event-bus.injectable";
|
||||||
import { EventEmitter } from "../common/event-emitter";
|
import { EventEmitter } from "../common/event-emitter";
|
||||||
import type { AppEvent } from "../common/app-event-bus/event-bus";
|
import type { AppEvent } from "../common/app-event-bus/event-bus";
|
||||||
|
import registerProtocolClientInjectable from "./electron-app/register-protocol-client.injectable";
|
||||||
|
import commandLineArgumentsInjectable from "./utils/command-line-arguments.injectable";
|
||||||
|
|
||||||
export const getDiForUnitTesting = (
|
export const getDiForUnitTesting = (
|
||||||
{ doGeneralOverrides } = { doGeneralOverrides: false },
|
{ doGeneralOverrides } = { doGeneralOverrides: false },
|
||||||
@ -84,6 +86,9 @@ export const getDiForUnitTesting = (
|
|||||||
di.override(setApplicationNameInjectable, () => () => {});
|
di.override(setApplicationNameInjectable, () => () => {});
|
||||||
di.override(getCommandLineSwitchInjectable, () => () => "irrelevant");
|
di.override(getCommandLineSwitchInjectable, () => () => "irrelevant");
|
||||||
di.override(isAutoUpdateEnabledInjectable, () => () => false);
|
di.override(isAutoUpdateEnabledInjectable, () => () => false);
|
||||||
|
di.override(registerProtocolClientInjectable, () => () => {});
|
||||||
|
|
||||||
|
di.override(commandLineArgumentsInjectable, () => []);
|
||||||
|
|
||||||
// TODO: Remove usages of globally exported appEventBus to get rid of this
|
// TODO: Remove usages of globally exported appEventBus to get rid of this
|
||||||
di.override(appEventBusInjectable, () => new EventEmitter<[AppEvent]>());
|
di.override(appEventBusInjectable, () => new EventEmitter<[AppEvent]>());
|
||||||
|
|||||||
@ -8,11 +8,11 @@ import windowManagerInjectable from "../../../window-manager.injectable";
|
|||||||
import { afterApplicationIsReadyInjectionToken } from "../after-application-is-ready-injection-token";
|
import { afterApplicationIsReadyInjectionToken } from "../after-application-is-ready-injection-token";
|
||||||
import whenOpeningUrlInjectable from "../../../electron-app/when-opening-url.injectable";
|
import whenOpeningUrlInjectable from "../../../electron-app/when-opening-url.injectable";
|
||||||
import whenSecondInstanceInjectable from "../../../electron-app/when-second-instance.injectable";
|
import whenSecondInstanceInjectable from "../../../electron-app/when-second-instance.injectable";
|
||||||
import loggerInjectable from "../../../../common/logger.injectable";
|
|
||||||
import electronAppInjectable from "../../../electron-app/electron-app.injectable";
|
|
||||||
import type { LensProtocolRouterMain } from "../../../protocol-handler";
|
import type { LensProtocolRouterMain } from "../../../protocol-handler";
|
||||||
import { pipeline } from "@ogre-tools/fp";
|
import { pipeline } from "@ogre-tools/fp";
|
||||||
import { find, map, startsWith, toLower } from "lodash/fp";
|
import { find, map, startsWith, toLower } from "lodash/fp";
|
||||||
|
import registerProtocolClientInjectable from "../../../electron-app/register-protocol-client.injectable";
|
||||||
|
import commandLineArgumentsInjectable from "../../../utils/command-line-arguments.injectable";
|
||||||
|
|
||||||
const setupDeepLinkingInjectable = getInjectable({
|
const setupDeepLinkingInjectable = getInjectable({
|
||||||
id: "setup-deep-linking",
|
id: "setup-deep-linking",
|
||||||
@ -21,10 +21,10 @@ const setupDeepLinkingInjectable = getInjectable({
|
|||||||
const windowManager = di.inject(windowManagerInjectable);
|
const windowManager = di.inject(windowManagerInjectable);
|
||||||
const whenOpeningUrl = di.inject(whenOpeningUrlInjectable);
|
const whenOpeningUrl = di.inject(whenOpeningUrlInjectable);
|
||||||
const whenSecondInstance = di.inject(whenSecondInstanceInjectable);
|
const whenSecondInstance = di.inject(whenSecondInstanceInjectable);
|
||||||
const logger = di.inject(loggerInjectable);
|
|
||||||
const app = di.inject(electronAppInjectable);
|
|
||||||
const protocolRouter = di.inject(lensProtocolRouterMainInjectable);
|
const protocolRouter = di.inject(lensProtocolRouterMainInjectable);
|
||||||
const routeWithProtocolRouter = routeWithProtocolRouterFor(protocolRouter);
|
const routeWithProtocolRouter = routeWithProtocolRouterFor(protocolRouter);
|
||||||
|
const registerProtocolClient = di.inject(registerProtocolClientInjectable);
|
||||||
|
const commandLineArguments = di.inject(commandLineArgumentsInjectable);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
run: async () => {
|
run: async () => {
|
||||||
@ -34,20 +34,14 @@ const setupDeepLinkingInjectable = getInjectable({
|
|||||||
await protocolRouter.route(url);
|
await protocolRouter.route(url);
|
||||||
});
|
});
|
||||||
|
|
||||||
whenSecondInstance(async ({ commandLineArguments }) => {
|
whenSecondInstance(async ({ commandLineArguments: secondInstanceArguments }) => {
|
||||||
await routeWithProtocolRouter(commandLineArguments);
|
await routeWithProtocolRouter(secondInstanceArguments);
|
||||||
await windowManager.ensureMainWindow();
|
await windowManager.ensureMainWindow();
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`📟 Setting protocol client for lens://`);
|
registerProtocolClient("lens");
|
||||||
|
|
||||||
if (app.setAsDefaultProtocolClient("lens")) {
|
await routeWithProtocolRouter(commandLineArguments);
|
||||||
logger.info("📟 Protocol client register succeeded ✅");
|
|
||||||
} else {
|
|
||||||
logger.info("📟 Protocol client register failed ❗");
|
|
||||||
}
|
|
||||||
|
|
||||||
await routeWithProtocolRouter(process.argv);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import electronAppInjectable from "../../../electron-app/electron-app.injectable
|
|||||||
import loggerInjectable from "../../../../common/logger.injectable";
|
import loggerInjectable from "../../../../common/logger.injectable";
|
||||||
import isMacInjectable from "../../../../common/vars/is-mac.injectable";
|
import isMacInjectable from "../../../../common/vars/is-mac.injectable";
|
||||||
import { afterApplicationIsReadyInjectionToken } from "../after-application-is-ready-injection-token";
|
import { afterApplicationIsReadyInjectionToken } from "../after-application-is-ready-injection-token";
|
||||||
|
import commandLineArgumentsInjectable from "../../../utils/command-line-arguments.injectable";
|
||||||
|
|
||||||
const startMainWindowInjectable = getInjectable({
|
const startMainWindowInjectable = getInjectable({
|
||||||
id: "start-main-window",
|
id: "start-main-window",
|
||||||
@ -17,13 +18,15 @@ const startMainWindowInjectable = getInjectable({
|
|||||||
const app = di.inject(electronAppInjectable);
|
const app = di.inject(electronAppInjectable);
|
||||||
const logger = di.inject(loggerInjectable);
|
const logger = di.inject(loggerInjectable);
|
||||||
const isMac = di.inject(isMacInjectable);
|
const isMac = di.inject(isMacInjectable);
|
||||||
|
const commandLineArguments = di.inject(commandLineArgumentsInjectable);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
run: async () => {
|
run: async () => {
|
||||||
|
|
||||||
// Start the app without showing the main window when auto starting on login
|
// Start the app without showing the main window when auto starting on login
|
||||||
// (On Windows and Linux, we get a flag. On MacOS, we get special API.)
|
// (On Windows and Linux, we get a flag. On MacOS, we get special API.)
|
||||||
const startHidden =
|
const startHidden =
|
||||||
process.argv.includes("--hidden") ||
|
commandLineArguments.includes("--hidden") ||
|
||||||
(isMac && app.getLoginItemSettings().wasOpenedAsHidden);
|
(isMac && app.getLoginItemSettings().wasOpenedAsHidden);
|
||||||
|
|
||||||
logger.info("🖥️ Starting WindowManager");
|
logger.info("🖥️ Starting WindowManager");
|
||||||
|
|||||||
13
src/main/utils/command-line-arguments.injectable.ts
Normal file
13
src/main/utils/command-line-arguments.injectable.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* 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";
|
||||||
|
|
||||||
|
const commandLineArgumentsInjectable = getInjectable({
|
||||||
|
id: "command-line-arguments",
|
||||||
|
instantiate: () => process.argv,
|
||||||
|
causesSideEffects: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default commandLineArgumentsInjectable;
|
||||||
Loading…
Reference in New Issue
Block a user