diff --git a/src/common/app-paths/app-paths-state.injectable.ts b/src/common/app-paths/app-paths-state.injectable.ts new file mode 100644 index 0000000000..5487d428b2 --- /dev/null +++ b/src/common/app-paths/app-paths-state.injectable.ts @@ -0,0 +1,34 @@ +/** + * 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 type { AppPaths } from "./app-path-injection-token"; + +const appPathsStateInjectable = getInjectable({ + id: "app-paths-state", + + instantiate: () => { + let state: AppPaths; + + return { + get: () =>{ + if (!state) { + throw new Error("Tried to get app paths before state is setupped."); + } + + return state; + }, + + set: (newState: AppPaths) => { + if (state) { + throw new Error("Tried to overwrite existing state of app paths."); + } + + state = newState; + }, + }; + }, +}); + +export default appPathsStateInjectable; diff --git a/src/common/app-paths/app-paths.injectable.ts b/src/common/app-paths/app-paths.injectable.ts new file mode 100644 index 0000000000..803f9e1380 --- /dev/null +++ b/src/common/app-paths/app-paths.injectable.ts @@ -0,0 +1,15 @@ +/** + * 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 { appPathsInjectionToken } from "./app-path-injection-token"; +import appPathsStateInjectable from "./app-paths-state.injectable"; + +const appPathsInjectable = getInjectable({ + id: "app-paths", + instantiate: (di) => di.inject(appPathsStateInjectable).get(), + injectionToken: appPathsInjectionToken, +}); + +export default appPathsInjectable; diff --git a/src/common/app-paths/app-paths.test.ts b/src/common/app-paths/app-paths.test.ts index b436426e2e..b0fa396e23 100644 --- a/src/common/app-paths/app-paths.test.ts +++ b/src/common/app-paths/app-paths.test.ts @@ -40,7 +40,7 @@ describe("app-paths", () => { recent: "some-recent", temp: "some-temp", videos: "some-videos", - userData: "some-irrelevant", + userData: "some-irrelevant-user-data", }; mainDi.override( diff --git a/src/main/app-paths/app-paths.injectable.ts b/src/main/app-paths/app-paths.injectable.ts deleted file mode 100644 index 342c32252d..0000000000 --- a/src/main/app-paths/app-paths.injectable.ts +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import type { - DiContainerForSetup } from "@ogre-tools/injectable"; -import { - getInjectable, -} from "@ogre-tools/injectable"; - -import { - appPathsInjectionToken, - appPathsIpcChannel, -} from "../../common/app-paths/app-path-injection-token"; - -import registerChannelInjectable from "./register-channel/register-channel.injectable"; -import { getAppPaths } from "./get-app-paths"; -import getElectronAppPathInjectable from "./get-electron-app-path/get-electron-app-path.injectable"; -import setElectronAppPathInjectable from "./set-electron-app-path/set-electron-app-path.injectable"; -import appNameInjectable from "./app-name/app-name.injectable"; -import directoryForIntegrationTestingInjectable from "./directory-for-integration-testing/directory-for-integration-testing.injectable"; -import joinPathsInjectable from "../../common/path/join-paths.injectable"; - -const appPathsInjectable = getInjectable({ - id: "app-paths", - - setup: async (di) => { - const directoryForIntegrationTesting = await di.inject( - directoryForIntegrationTestingInjectable, - ); - - if (directoryForIntegrationTesting) { - await setupPathForAppDataInIntegrationTesting(di, directoryForIntegrationTesting); - } - - await setupPathForUserData(di); - await registerAppPathsChannel(di); - }, - - instantiate: (di) => - getAppPaths({ getAppPath: di.inject(getElectronAppPathInjectable) }), - - injectionToken: appPathsInjectionToken, -}); - -export default appPathsInjectable; - -const registerAppPathsChannel = async (di: DiContainerForSetup) => { - const registerChannel = await di.inject(registerChannelInjectable); - const appPaths = await di.inject(appPathsInjectable); - - registerChannel(appPathsIpcChannel, () => appPaths); -}; - -const setupPathForUserData = async (di: DiContainerForSetup) => { - const setElectronAppPath = await di.inject(setElectronAppPathInjectable); - const appName = await di.inject(appNameInjectable); - const getAppPath = await di.inject(getElectronAppPathInjectable); - const joinPaths = await di.inject(joinPathsInjectable); - - const appDataPath = getAppPath("appData"); - - setElectronAppPath("userData", joinPaths(appDataPath, appName)); -}; - -// Todo: this kludge is here only until we have a proper place to setup integration testing. -const setupPathForAppDataInIntegrationTesting = async (di: DiContainerForSetup, appDataPath: string) => { - const setElectronAppPath = await di.inject(setElectronAppPathInjectable); - - setElectronAppPath("appData", appDataPath); -}; diff --git a/src/main/app-paths/setup-app-paths.injectable.ts b/src/main/app-paths/setup-app-paths.injectable.ts new file mode 100644 index 0000000000..7ce1c36cb4 --- /dev/null +++ b/src/main/app-paths/setup-app-paths.injectable.ts @@ -0,0 +1,59 @@ +/** + * 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 type { AppPaths } from "../../common/app-paths/app-path-injection-token"; +import getElectronAppPathInjectable from "./get-electron-app-path/get-electron-app-path.injectable"; +import setElectronAppPathInjectable from "./set-electron-app-path/set-electron-app-path.injectable"; +import appNameInjectable from "./app-name/app-name.injectable"; +import directoryForIntegrationTestingInjectable from "./directory-for-integration-testing/directory-for-integration-testing.injectable"; +import { setupableInjectionToken } from "../../common/setupable-injection-token/setupable-injection-token"; +import appPathsStateInjectable from "../../common/app-paths/app-paths-state.injectable"; +import { pathNames } from "../../common/app-paths/app-path-names"; +import { fromPairs, map } from "lodash/fp"; +import { pipeline } from "@ogre-tools/fp"; +import { appPathsIpcChannel } from "../../common/app-paths/app-path-injection-token"; +import registerChannelInjectable from "./register-channel/register-channel.injectable"; +import joinPathsInjectable from "../../common/path/join-paths.injectable"; + +const setupAppPathsInjectable = getInjectable({ + id: "setup-app-paths", + + instantiate: (di) => { + const setElectronAppPath = di.inject(setElectronAppPathInjectable); + const appName = di.inject(appNameInjectable); + const getAppPath = di.inject(getElectronAppPathInjectable); + const appPathsState = di.inject(appPathsStateInjectable); + const registerChannel = di.inject(registerChannelInjectable); + const directoryForIntegrationTesting = di.inject(directoryForIntegrationTestingInjectable); + const joinPaths = di.inject(joinPathsInjectable); + + return { + doSetup: () => { + if (directoryForIntegrationTesting) { + setElectronAppPath("appData", directoryForIntegrationTesting); + } + + const appDataPath = getAppPath("appData"); + + setElectronAppPath("userData", joinPaths(appDataPath, appName)); + + const appPaths = pipeline( + pathNames, + map(name => [name, getAppPath(name)]), + fromPairs, + ) as AppPaths; + + appPathsState.set(appPaths); + + registerChannel(appPathsIpcChannel, () => appPaths); + }, + }; + }, + + injectionToken: setupableInjectionToken, +}); + +export default setupAppPathsInjectable; diff --git a/src/renderer/app-paths/app-paths.injectable.ts b/src/renderer/app-paths/app-paths.injectable.ts deleted file mode 100644 index e6c77c1840..0000000000 --- a/src/renderer/app-paths/app-paths.injectable.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * 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 type { AppPaths } from "../../common/app-paths/app-path-injection-token"; -import { appPathsInjectionToken, appPathsIpcChannel } from "../../common/app-paths/app-path-injection-token"; -import getValueFromRegisteredChannelInjectable from "./get-value-from-registered-channel/get-value-from-registered-channel.injectable"; - -let syncAppPaths: AppPaths; - -const appPathsInjectable = getInjectable({ - id: "app-paths", - - setup: async (di) => { - const getValueFromRegisteredChannel = await di.inject( - getValueFromRegisteredChannelInjectable, - ); - - syncAppPaths = await getValueFromRegisteredChannel(appPathsIpcChannel); - }, - - instantiate: () => syncAppPaths, - - injectionToken: appPathsInjectionToken, -}); - -export default appPathsInjectable; diff --git a/src/renderer/app-paths/setup-app-paths.injectable.ts b/src/renderer/app-paths/setup-app-paths.injectable.ts new file mode 100644 index 0000000000..2f44818c74 --- /dev/null +++ b/src/renderer/app-paths/setup-app-paths.injectable.ts @@ -0,0 +1,33 @@ +/** + * 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 { AppPaths, appPathsIpcChannel } from "../../common/app-paths/app-path-injection-token"; +import getValueFromRegisteredChannelInjectable from "./get-value-from-registered-channel/get-value-from-registered-channel.injectable"; +import { setupableInjectionToken } from "../../common/setupable-injection-token/setupable-injection-token"; +import appPathsStateInjectable from "../../common/app-paths/app-paths-state.injectable"; + +let syncAppPaths: AppPaths; + +const setupAppPathsInjectable = getInjectable({ + id: "setup-app-paths", + + instantiate: (di) => ({ + doSetup: async () => { + const getValueFromRegisteredChannel = di.inject( + getValueFromRegisteredChannelInjectable, + ); + + syncAppPaths = await getValueFromRegisteredChannel(appPathsIpcChannel); + + const appPathsState = di.inject(appPathsStateInjectable); + + appPathsState.set(syncAppPaths); + }, + }), + + injectionToken: setupableInjectionToken, +}); + +export default setupAppPathsInjectable;