From ebd918fcf72d94187ea7fcfa410989be2e9d17e7 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 27 Oct 2021 14:13:38 -0400 Subject: [PATCH] Replace basically all uses of app.getPath() with AppPaths.get() Signed-off-by: Sebastian Malton --- src/common/app-paths.ts | 7 +++++++ src/main/extension-filesystem.ts | 4 ++-- src/main/index.ts | 11 +---------- src/main/initializers/ipc.ts | 5 +++-- src/main/kubeconfig-manager.ts | 4 ++-- src/main/kubectl.ts | 4 ++-- src/migrations/cluster-store/3.6.0-beta.1.ts | 4 ++-- src/migrations/cluster-store/5.0.0-beta.10.ts | 4 ++-- src/migrations/cluster-store/5.0.0-beta.13.ts | 4 ++-- src/migrations/hotbar-store/5.0.0-beta.10.ts | 4 ++-- src/migrations/user-store/5.0.3-beta.1.ts | 7 ++++--- src/migrations/user-store/file-name-migration.ts | 4 ++-- 12 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/common/app-paths.ts b/src/common/app-paths.ts index 74563d1f34..883b7053a4 100644 --- a/src/common/app-paths.ts +++ b/src/common/app-paths.ts @@ -21,6 +21,7 @@ import { app, ipcMain, ipcRenderer } from "electron"; import { observable, when } from "mobx"; +import path from "path"; import logger from "./logger"; import { fromEntries, toJS } from "./utils"; import { isWindows } from "./vars"; @@ -71,6 +72,12 @@ export class AppPaths { } private static initMain(): void { + if (process.env.CICD) { + app.setPath("appData", process.env.CICD); + } + + app.setPath("userData", path.join(app.getPath("appData"), app.getName())); + AppPaths.paths.set(fromEntries(pathNames.map(pathName => [pathName, app.getPath(pathName)]))); ipcMain.handle(AppPaths.ipcChannel, () => toJS(AppPaths.paths.get())); } diff --git a/src/main/extension-filesystem.ts b/src/main/extension-filesystem.ts index c93a304ece..849165afa4 100644 --- a/src/main/extension-filesystem.ts +++ b/src/main/extension-filesystem.ts @@ -27,7 +27,7 @@ import path from "path"; import { BaseStore } from "../common/base-store"; import type { LensExtensionId } from "../extensions/lens-extension"; import { toJS } from "../common/utils"; -import { app } from "electron"; +import { AppPaths } from "../common/app-paths"; interface FSProvisionModel { extensions: Record; // extension names to paths @@ -55,7 +55,7 @@ export class FilesystemProvisionerStore extends BaseStore { if (!this.registeredExtensions.has(extensionName)) { const salt = randomBytes(32).toString("hex"); const hashedName = SHA256(`${extensionName}/${salt}`).toString(); - const dirPath = path.resolve(app.getPath("userData"), "extension_data", hashedName); + const dirPath = path.resolve(AppPaths.get("userData"), "extension_data", hashedName); this.registeredExtensions.set(extensionName, dirPath); } diff --git a/src/main/index.ts b/src/main/index.ts index 9566db2795..786a79a7b7 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -63,14 +63,12 @@ import { ensureDir } from "fs-extra"; import { Router } from "./router"; import { initMenu } from "./menu"; import { initTray } from "./tray"; -import * as path from "path"; import { kubeApiRequest, shellApiRequest } from "./proxy-functions"; import { AppPaths } from "../common/app-paths"; const onCloseCleanup = disposer(); const onQuitCleanup = disposer(); -const workingDir = path.join(app.getPath("appData"), appName); SentryInit(); app.setName(appName); @@ -83,13 +81,6 @@ if (app.setAsDefaultProtocolClient("lens")) { logger.info("📟 Protocol client register failed ❗"); } -if (process.env.CICD) { - app.setPath("appData", process.env.CICD); - app.setPath("userData", path.join(process.env.CICD, appName)); -} else { - app.setPath("userData", workingDir); -} - AppPaths.init(); if (process.env.LENS_DISABLE_GPU) { @@ -130,7 +121,7 @@ app.on("second-instance", (event, argv) => { }); app.on("ready", async () => { - logger.info(`🚀 Starting ${productName} from "${app.getPath("exe")}"`); + logger.info(`🚀 Starting ${productName} from "${AppPaths.get("exe")}"`); logger.info("🐚 Syncing shell environment"); await shellSync(); diff --git a/src/main/initializers/ipc.ts b/src/main/initializers/ipc.ts index c16c3e1fe1..ff79bd091b 100644 --- a/src/main/initializers/ipc.ts +++ b/src/main/initializers/ipc.ts @@ -19,7 +19,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import { app, BrowserWindow, dialog, IpcMainInvokeEvent } from "electron"; +import { BrowserWindow, dialog, IpcMainInvokeEvent } from "electron"; import { KubernetesCluster } from "../../common/catalog-entities"; import { clusterFrameMap } from "../../common/cluster-frames"; import { clusterActivateHandler, clusterSetFrameIdHandler, clusterVisibilityHandler, clusterRefreshHandler, clusterDisconnectHandler, clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler, clusterDeleteHandler, clusterSetDeletingHandler, clusterClearDeletingHandler } from "../../common/cluster-ipc"; @@ -34,6 +34,7 @@ import { ResourceApplier } from "../resource-applier"; import { WindowManager } from "../window-manager"; import path from "path"; import { remove } from "fs-extra"; +import { AppPaths } from "../../common/app-paths"; export function initIpcMainHandlers() { ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => { @@ -99,7 +100,7 @@ export function initIpcMainHandlers() { try { // remove the local storage file - const localStorageFilePath = path.resolve(app.getPath("userData"), "lens-local-storage", `${cluster.id}.json`); + const localStorageFilePath = path.resolve(AppPaths.get("userData"), "lens-local-storage", `${cluster.id}.json`); await remove(localStorageFilePath); } catch {} diff --git a/src/main/kubeconfig-manager.ts b/src/main/kubeconfig-manager.ts index c9b101d59f..bd9b4a5ab0 100644 --- a/src/main/kubeconfig-manager.ts +++ b/src/main/kubeconfig-manager.ts @@ -22,15 +22,15 @@ import type { KubeConfig } from "@kubernetes/client-node"; import type { Cluster } from "./cluster"; import type { ContextHandler } from "./context-handler"; -import { app } from "electron"; import path from "path"; import fs from "fs-extra"; import { dumpConfigYaml } from "../common/kube-helpers"; import logger from "./logger"; import { LensProxy } from "./lens-proxy"; +import { AppPaths } from "../common/app-paths"; export class KubeconfigManager { - protected configDir = app.getPath("temp"); + protected configDir = AppPaths.get("temp"); protected tempFile: string = null; constructor(protected cluster: Cluster, protected contextHandler: ContextHandler) { } diff --git a/src/main/kubectl.ts b/src/main/kubectl.ts index 0c75453bd3..c68e00b2dd 100644 --- a/src/main/kubectl.ts +++ b/src/main/kubectl.ts @@ -32,7 +32,7 @@ import { getBundledKubectlVersion } from "../common/utils/app-version"; import { isDevelopment, isWindows, isTestEnv } from "../common/vars"; import { SemVer } from "semver"; import { defaultPackageMirror, packageMirrors } from "../common/user-store/preferences-helpers"; -import { app } from "electron"; +import { AppPaths } from "../common/app-paths"; const bundledVersion = getBundledKubectlVersion(); const kubectlMap: Map = new Map([ @@ -81,7 +81,7 @@ export class Kubectl { protected dirname: string; static get kubectlDir() { - return path.join(app.getPath("userData"), "binaries", "kubectl"); + return path.join(AppPaths.get("userData"), "binaries", "kubectl"); } public static readonly bundledKubectlVersion: string = bundledVersion; diff --git a/src/migrations/cluster-store/3.6.0-beta.1.ts b/src/migrations/cluster-store/3.6.0-beta.1.ts index 7ef4b042ec..00b2842983 100644 --- a/src/migrations/cluster-store/3.6.0-beta.1.ts +++ b/src/migrations/cluster-store/3.6.0-beta.1.ts @@ -23,12 +23,12 @@ // convert file path cluster icons to their base64 encoded versions import path from "path"; -import { app } from "electron"; import fse from "fs-extra"; import { loadConfigFromFileSync } from "../../common/kube-helpers"; import { MigrationDeclaration, migrationLog } from "../helpers"; import type { ClusterModel } from "../../common/cluster-types"; import { getCustomKubeConfigPath, storedKubeConfigFolder } from "../../common/utils"; +import { AppPaths } from "../../common/app-paths"; interface Pre360ClusterModel extends ClusterModel { kubeConfig: string; @@ -37,7 +37,7 @@ interface Pre360ClusterModel extends ClusterModel { export default { version: "3.6.0-beta.1", run(store) { - const userDataPath = app.getPath("userData"); + const userDataPath = AppPaths.get("userData"); const storedClusters: Pre360ClusterModel[] = store.get("clusters") ?? []; const migratedClusters: ClusterModel[] = []; diff --git a/src/migrations/cluster-store/5.0.0-beta.10.ts b/src/migrations/cluster-store/5.0.0-beta.10.ts index a26f0ed662..287f2ecd79 100644 --- a/src/migrations/cluster-store/5.0.0-beta.10.ts +++ b/src/migrations/cluster-store/5.0.0-beta.10.ts @@ -20,10 +20,10 @@ */ import path from "path"; -import { app } from "electron"; import fse from "fs-extra"; import type { ClusterModel } from "../../common/cluster-types"; import type { MigrationDeclaration } from "../helpers"; +import { AppPaths } from "../../common/app-paths"; interface Pre500WorkspaceStoreModel { workspaces: { @@ -35,7 +35,7 @@ interface Pre500WorkspaceStoreModel { export default { version: "5.0.0-beta.10", run(store) { - const userDataPath = app.getPath("userData"); + const userDataPath = AppPaths.get("userData"); try { const workspaceData: Pre500WorkspaceStoreModel = fse.readJsonSync(path.join(userDataPath, "lens-workspace-store.json")); diff --git a/src/migrations/cluster-store/5.0.0-beta.13.ts b/src/migrations/cluster-store/5.0.0-beta.13.ts index 6c8f37e366..b75b938a14 100644 --- a/src/migrations/cluster-store/5.0.0-beta.13.ts +++ b/src/migrations/cluster-store/5.0.0-beta.13.ts @@ -23,8 +23,8 @@ import type { ClusterModel, ClusterPreferences, ClusterPrometheusPreferences } f import { MigrationDeclaration, migrationLog } from "../helpers"; import { generateNewIdFor } from "../utils"; import path from "path"; -import { app } from "electron"; import { moveSync, removeSync } from "fs-extra"; +import { AppPaths } from "../../common/app-paths"; function mergePrometheusPreferences(left: ClusterPrometheusPreferences, right: ClusterPrometheusPreferences): ClusterPrometheusPreferences { if (left.prometheus && left.prometheusProvider) { @@ -106,7 +106,7 @@ function moveStorageFolder({ folder, newId, oldId }: { folder: string, newId: st export default { version: "5.0.0-beta.13", run(store) { - const folder = path.resolve(app.getPath("userData"), "lens-local-storage"); + const folder = path.resolve(AppPaths.get("userData"), "lens-local-storage"); const oldClusters: ClusterModel[] = store.get("clusters") ?? []; const clusters = new Map(); diff --git a/src/migrations/hotbar-store/5.0.0-beta.10.ts b/src/migrations/hotbar-store/5.0.0-beta.10.ts index a9a0de11b2..e5691d3622 100644 --- a/src/migrations/hotbar-store/5.0.0-beta.10.ts +++ b/src/migrations/hotbar-store/5.0.0-beta.10.ts @@ -19,11 +19,11 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import { app } from "electron"; import fse from "fs-extra"; import { isNull } from "lodash"; import path from "path"; import * as uuid from "uuid"; +import { AppPaths } from "../../common/app-paths"; import type { ClusterStoreModel } from "../../common/cluster-store"; import { defaultHotbarCells, getEmptyHotbar, Hotbar, HotbarItem } from "../../common/hotbar-types"; import { catalogEntity } from "../../main/catalog-sources/general"; @@ -48,7 +48,7 @@ export default { run(store) { const rawHotbars = store.get("hotbars"); const hotbars: Hotbar[] = Array.isArray(rawHotbars) ? rawHotbars.filter(h => h && typeof h === "object") : []; - const userDataPath = app.getPath("userData"); + const userDataPath = AppPaths.get("userData"); // Hotbars might be empty, if some of the previous migrations weren't run if (hotbars.length === 0) { diff --git a/src/migrations/user-store/5.0.3-beta.1.ts b/src/migrations/user-store/5.0.3-beta.1.ts index 05a1767f08..ec5de34d66 100644 --- a/src/migrations/user-store/5.0.3-beta.1.ts +++ b/src/migrations/user-store/5.0.3-beta.1.ts @@ -19,7 +19,6 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import { app } from "electron"; import { existsSync, readFileSync } from "fs"; import path from "path"; import os from "os"; @@ -27,14 +26,16 @@ import type { ClusterStoreModel } from "../../common/cluster-store"; import type { KubeconfigSyncEntry, UserPreferencesModel } from "../../common/user-store"; import { MigrationDeclaration, migrationLog } from "../helpers"; import { isLogicalChildPath, storedKubeConfigFolder } from "../../common/utils"; +import { AppPaths } from "../../common/app-paths"; export default { version: "5.0.3-beta.1", run(store) { try { const { syncKubeconfigEntries = [], ...preferences }: UserPreferencesModel = store.get("preferences") ?? {}; - const { clusters = [] }: ClusterStoreModel = JSON.parse(readFileSync(path.resolve(app.getPath("userData"), "lens-cluster-store.json"), "utf-8")) ?? {}; - const extensionDataDir = path.resolve(app.getPath("userData"), "extension_data"); + const userData = AppPaths.get("userData"); + const { clusters = [] }: ClusterStoreModel = JSON.parse(readFileSync(path.resolve(userData, "lens-cluster-store.json"), "utf-8")) ?? {}; + const extensionDataDir = path.resolve(userData, "extension_data"); const syncPaths = new Set(syncKubeconfigEntries.map(s => s.filePath)); syncPaths.add(path.join(os.homedir(), ".kube")); diff --git a/src/migrations/user-store/file-name-migration.ts b/src/migrations/user-store/file-name-migration.ts index 932e542642..80bff67302 100644 --- a/src/migrations/user-store/file-name-migration.ts +++ b/src/migrations/user-store/file-name-migration.ts @@ -19,12 +19,12 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import { app } from "electron"; import fse from "fs-extra"; import path from "path"; +import { AppPaths } from "../../common/app-paths"; export function fileNameMigration() { - const userDataPath = app.getPath("userData"); + const userDataPath = AppPaths.get("userData"); const configJsonPath = path.join(userDataPath, "config.json"); const lensUserStoreJsonPath = path.join(userDataPath, "lens-user-store.json");