mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Switch to using competition to setup app paths
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
b8fd1299ee
commit
3782b36809
34
src/common/app-paths/app-paths-state.injectable.ts
Normal file
34
src/common/app-paths/app-paths-state.injectable.ts
Normal file
@ -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;
|
||||
15
src/common/app-paths/app-paths.injectable.ts
Normal file
15
src/common/app-paths/app-paths.injectable.ts
Normal file
@ -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;
|
||||
@ -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(
|
||||
|
||||
@ -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);
|
||||
};
|
||||
59
src/main/app-paths/setup-app-paths.injectable.ts
Normal file
59
src/main/app-paths/setup-app-paths.injectable.ts
Normal file
@ -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;
|
||||
@ -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;
|
||||
33
src/renderer/app-paths/setup-app-paths.injectable.ts
Normal file
33
src/renderer/app-paths/setup-app-paths.injectable.ts
Normal file
@ -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;
|
||||
Loading…
Reference in New Issue
Block a user