mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Switch to using abstraction for getting absolute path to control explicit side effect
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
5ca3082572
commit
911481ea0f
@ -4,13 +4,20 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import directoryForUserDataInjectable from "../directory-for-user-data/directory-for-user-data.injectable";
|
||||
import path from "path";
|
||||
import getAbsolutePathInjectable from "../../path/get-absolute-path.injectable";
|
||||
|
||||
const directoryForKubeConfigsInjectable = getInjectable({
|
||||
id: "directory-for-kube-configs",
|
||||
|
||||
instantiate: (di) =>
|
||||
path.resolve(di.inject(directoryForUserDataInjectable), "kubeconfigs"),
|
||||
instantiate: (di) => {
|
||||
const getAbsolutePath = di.inject(getAbsolutePathInjectable);
|
||||
const directoryForUserData = di.inject(directoryForUserDataInjectable);
|
||||
|
||||
return getAbsolutePath(
|
||||
directoryForUserData,
|
||||
"kubeconfigs",
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
export default directoryForKubeConfigsInjectable;
|
||||
|
||||
@ -3,19 +3,18 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import path from "path";
|
||||
import directoryForKubeConfigsInjectable from "../directory-for-kube-configs/directory-for-kube-configs.injectable";
|
||||
import getAbsolutePathInjectable from "../../path/get-absolute-path.injectable";
|
||||
|
||||
const getCustomKubeConfigDirectoryInjectable = getInjectable({
|
||||
id: "get-custom-kube-config-directory",
|
||||
|
||||
instantiate: (di) => (directoryName: string) => {
|
||||
instantiate: (di) => {
|
||||
const directoryForKubeConfigs = di.inject(directoryForKubeConfigsInjectable);
|
||||
const getAbsolutePath = di.inject(getAbsolutePathInjectable);
|
||||
|
||||
return path.resolve(
|
||||
directoryForKubeConfigs,
|
||||
directoryName,
|
||||
);
|
||||
return (directoryName: string) =>
|
||||
getAbsolutePath(directoryForKubeConfigs, directoryName);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -3,17 +3,21 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import path from "path";
|
||||
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
|
||||
import getAbsolutePathInjectable from "../path/get-absolute-path.injectable";
|
||||
|
||||
const directoryForLensLocalStorageInjectable = getInjectable({
|
||||
id: "directory-for-lens-local-storage",
|
||||
|
||||
instantiate: (di) =>
|
||||
path.resolve(
|
||||
di.inject(directoryForUserDataInjectable),
|
||||
instantiate: (di) => {
|
||||
const getAbsolutePath = di.inject(getAbsolutePathInjectable);
|
||||
const directoryForUserData = di.inject(directoryForUserDataInjectable);
|
||||
|
||||
return getAbsolutePath(
|
||||
directoryForUserData,
|
||||
"lens-local-storage",
|
||||
),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
export default directoryForLensLocalStorageInjectable;
|
||||
|
||||
@ -14,7 +14,7 @@ const writeFileInjectable = getInjectable({
|
||||
|
||||
return async (filePath: string, content: string | Buffer) => {
|
||||
await ensureDir(path.dirname(filePath), { mode: 0o755 });
|
||||
|
||||
|
||||
await writeFile(filePath, content, {
|
||||
encoding: "utf-8",
|
||||
});
|
||||
|
||||
@ -6,6 +6,7 @@ import { getInjectable } from "@ogre-tools/injectable";
|
||||
import directoryForLensLocalStorageInjectable from "../../../common/directory-for-lens-local-storage/directory-for-lens-local-storage.injectable";
|
||||
import { initIpcMainHandlers } from "./init-ipc-main-handlers";
|
||||
import getAppMenuItemsInjectable from "../../menu/get-app-menu-items.injectable";
|
||||
import getAbsolutePathInjectable from "../../../common/path/get-absolute-path.injectable";
|
||||
|
||||
const initIpcMainHandlersInjectable = getInjectable({
|
||||
id: "init-ipc-main-handlers",
|
||||
@ -13,6 +14,7 @@ const initIpcMainHandlersInjectable = getInjectable({
|
||||
instantiate: (di) => initIpcMainHandlers({
|
||||
getAppMenuItems: () => di.inject(getAppMenuItemsInjectable)(),
|
||||
directoryForLensLocalStorage: di.inject(directoryForLensLocalStorageInjectable),
|
||||
getAbsolutePath: di.inject(getAbsolutePathInjectable),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@ -14,7 +14,6 @@ import { catalogEntityRegistry } from "../../catalog";
|
||||
import { pushCatalogToRenderer } from "../../catalog-pusher";
|
||||
import { ClusterManager } from "../../cluster-manager";
|
||||
import { ResourceApplier } from "../../resource-applier";
|
||||
import path from "path";
|
||||
import { remove } from "fs-extra";
|
||||
import { onLocationChange, handleWindowAction } from "../../ipc/window";
|
||||
import { openFilePickingDialogChannel } from "../../../common/ipc/dialog";
|
||||
@ -23,13 +22,15 @@ import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppM
|
||||
import { getNativeColorTheme } from "../../native-theme";
|
||||
import { getNativeThemeChannel } from "../../../common/ipc/native-theme";
|
||||
import type { MenuItemsOpts } from "../../menu/get-app-menu-items.injectable";
|
||||
import type { GetAbsolutePath } from "../../../common/path/get-absolute-path.injectable";
|
||||
|
||||
interface Dependencies {
|
||||
getAppMenuItems: () => MenuItemsOpts[];
|
||||
directoryForLensLocalStorage: string;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
}
|
||||
|
||||
export const initIpcMainHandlers = ({ getAppMenuItems, directoryForLensLocalStorage }: Dependencies) => () => {
|
||||
export const initIpcMainHandlers = ({ getAppMenuItems, directoryForLensLocalStorage, getAbsolutePath }: Dependencies) => () => {
|
||||
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
||||
return ClusterStore.getInstance()
|
||||
.getById(clusterId)
|
||||
@ -85,7 +86,7 @@ export const initIpcMainHandlers = ({ getAppMenuItems, directoryForLensLocalStor
|
||||
|
||||
try {
|
||||
// remove the local storage file
|
||||
const localStorageFilePath = path.resolve(directoryForLensLocalStorage, `${cluster.id}.json`);
|
||||
const localStorageFilePath = getAbsolutePath(directoryForLensLocalStorage, `${cluster.id}.json`);
|
||||
|
||||
await remove(localStorageFilePath);
|
||||
} catch {
|
||||
|
||||
@ -7,7 +7,6 @@ import Call from "@hapi/call";
|
||||
import type http from "http";
|
||||
import type httpProxy from "http-proxy";
|
||||
import { toPairs } from "lodash/fp";
|
||||
import path from "path";
|
||||
import type { Cluster } from "../../common/cluster/cluster";
|
||||
import type { LensApiResultContentType } from "./router-content-types";
|
||||
import { contentTypes } from "./router-content-types";
|
||||
@ -51,7 +50,6 @@ interface Dependencies {
|
||||
|
||||
export class Router {
|
||||
protected router = new Call.Router();
|
||||
protected static rootPath = path.resolve(__static);
|
||||
|
||||
constructor(routes: Route<unknown>[], private dependencies: Dependencies) {
|
||||
routes.forEach(route => {
|
||||
|
||||
@ -12,20 +12,22 @@ import path from "path";
|
||||
import isDevelopmentInjectable from "../../common/vars/is-development.injectable";
|
||||
import httpProxy from "http-proxy";
|
||||
import readFileBufferInjectable from "../../common/fs/read-file-buffer.injectable";
|
||||
import getAbsolutePathInjectable, { GetAbsolutePath } from "../../common/path/get-absolute-path.injectable";
|
||||
|
||||
interface ProductionDependencies {
|
||||
readFileBuffer: (path: string) => Promise<Buffer>;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
}
|
||||
|
||||
const handleStaticFileInProduction =
|
||||
({ readFileBuffer }: ProductionDependencies) =>
|
||||
({ readFileBuffer, getAbsolutePath }: ProductionDependencies) =>
|
||||
async ({ params }: LensApiRequest) => {
|
||||
const staticPath = path.resolve(__static);
|
||||
const staticPath = getAbsolutePath(__static);
|
||||
let filePath = params.path;
|
||||
|
||||
for (let retryCount = 0; retryCount < 5; retryCount += 1) {
|
||||
const asset = path.join(staticPath, filePath);
|
||||
const normalizedFilePath = path.resolve(asset);
|
||||
const normalizedFilePath = getAbsolutePath(asset);
|
||||
|
||||
if (!normalizedFilePath.startsWith(staticPath)) {
|
||||
return { statusCode: 404 };
|
||||
@ -79,13 +81,14 @@ const staticFileRouteInjectable = getInjectable({
|
||||
instantiate: (di): Route<Buffer> => {
|
||||
const isDevelopment = di.inject(isDevelopmentInjectable);
|
||||
const readFileBuffer = di.inject(readFileBufferInjectable);
|
||||
const getAbsolutePath = di.inject(getAbsolutePathInjectable);
|
||||
|
||||
return {
|
||||
method: "get",
|
||||
path: `/{path*}`,
|
||||
handler: isDevelopment
|
||||
? handleStaticFileInDevelopment({ proxy: httpProxy.createProxy() })
|
||||
: handleStaticFileInProduction({ readFileBuffer }),
|
||||
: handleStaticFileInProduction({ readFileBuffer, getAbsolutePath }),
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
@ -9,14 +9,16 @@ import "../../../../common/vars";
|
||||
import readFileInjectable from "../../../../common/fs/read-file.injectable";
|
||||
import readDirInjectable from "../../../../common/fs/read-dir.injectable";
|
||||
import type { RawTemplates } from "./create-resource-templates.injectable";
|
||||
import getAbsolutePathInjectable, { GetAbsolutePath } from "../../../../common/path/get-absolute-path.injectable";
|
||||
|
||||
interface Dependencies {
|
||||
readDir: (dirPath: string) => Promise<string[]>;
|
||||
readFile: (filePath: string) => Promise<string>;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
}
|
||||
|
||||
async function getTemplates({ readDir, readFile }: Dependencies) {
|
||||
const templatesFolder = path.resolve(__static, "../templates/create-resource");
|
||||
async function getTemplates({ readDir, readFile, getAbsolutePath }: Dependencies) {
|
||||
const templatesFolder = getAbsolutePath(__static, "../templates/create-resource");
|
||||
|
||||
/**
|
||||
* Mapping between file names and their contents
|
||||
@ -39,6 +41,7 @@ const lensCreateResourceTemplatesInjectable = getInjectable({
|
||||
const templates = await getTemplates({
|
||||
readFile: di.inject(readFileInjectable),
|
||||
readDir: di.inject(readDirInjectable),
|
||||
getAbsolutePath: di.inject(getAbsolutePathInjectable),
|
||||
});
|
||||
|
||||
return ["lens", templates];
|
||||
|
||||
@ -9,6 +9,7 @@ import readJsonFileInjectable from "../../../common/fs/read-json-file.injectable
|
||||
import writeJsonFileInjectable from "../../../common/fs/write-json-file.injectable";
|
||||
import { observable } from "mobx";
|
||||
import loggerInjectable from "../../../common/logger.injectable";
|
||||
import getAbsolutePathInjectable from "../../../common/path/get-absolute-path.injectable";
|
||||
|
||||
const createStorageInjectable = getInjectable({
|
||||
id: "create-storage",
|
||||
@ -28,6 +29,8 @@ const createStorageInjectable = getInjectable({
|
||||
directoryForLensLocalStorage: di.inject(
|
||||
directoryForLensLocalStorageInjectable,
|
||||
),
|
||||
|
||||
getAbsolutePath: di.inject(getAbsolutePathInjectable),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@ -5,7 +5,6 @@
|
||||
|
||||
// Keeps window.localStorage state in external JSON-files.
|
||||
// Because app creates random port between restarts => storage session wiped out each time.
|
||||
import path from "path";
|
||||
import { comparer, reaction, toJS, when } from "mobx";
|
||||
import { StorageHelper } from "../storageHelper";
|
||||
import { isTestEnv } from "../../../common/vars";
|
||||
@ -13,6 +12,7 @@ import { isTestEnv } from "../../../common/vars";
|
||||
import { getHostedClusterId } from "../../../common/utils";
|
||||
import type { JsonObject, JsonValue } from "type-fest";
|
||||
import type { Logger } from "../../../common/logger";
|
||||
import type { GetAbsolutePath } from "../../../common/path/get-absolute-path.injectable";
|
||||
|
||||
interface Dependencies {
|
||||
storage: { initialized: boolean; loaded: boolean; data: Record<string, any> };
|
||||
@ -20,19 +20,20 @@ interface Dependencies {
|
||||
directoryForLensLocalStorage: string;
|
||||
readJsonFile: (filePath: string) => Promise<JsonValue>;
|
||||
writeJsonFile: (filePath: string, contentObject: JsonObject) => Promise<void>;
|
||||
getAbsolutePath: GetAbsolutePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a helper for saving data under the "key" intended for window.localStorage
|
||||
*/
|
||||
export const createStorage = ({ storage, logger, directoryForLensLocalStorage, readJsonFile, writeJsonFile }: Dependencies) => <T>(key: string, defaultValue: T) => {
|
||||
export const createStorage = ({ storage, getAbsolutePath, logger, directoryForLensLocalStorage, readJsonFile, writeJsonFile }: Dependencies) => <T>(key: string, defaultValue: T) => {
|
||||
const { logPrefix } = StorageHelper;
|
||||
|
||||
if (!storage.initialized) {
|
||||
storage.initialized = true;
|
||||
|
||||
(async () => {
|
||||
const filePath = path.resolve(directoryForLensLocalStorage, `${getHostedClusterId() || "app"}.json`);
|
||||
const filePath = getAbsolutePath(directoryForLensLocalStorage, `${getHostedClusterId() || "app"}.json`);
|
||||
|
||||
try {
|
||||
storage.data = (await readJsonFile(filePath)) as JsonObject;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user