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:
1 parent
5ca3082572
commit
911481ea0f
11 files changed
+50
-29
No files matched your search
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user