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