mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Stop using @electron/remote to obtain app.getPath() (#4078)
* Stop using remote to obtain app.getPath() - Initialize entire map in bootstrap() - Updated unit tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Resolve PR comments Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix test Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix build Signed-off-by: Sebastian Malton <sebastian@malton.name> * Ensure that init can only be called once and catch errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Replace basically all uses of app.getPath() with AppPaths.get() Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix unit tests Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
65b6908bf1
commit
f297407156
@ -26,11 +26,6 @@ export default {
|
|||||||
getLocale: jest.fn().mockRejectedValue("en"),
|
getLocale: jest.fn().mockRejectedValue("en"),
|
||||||
getPath: jest.fn(() => "tmp"),
|
getPath: jest.fn(() => "tmp"),
|
||||||
},
|
},
|
||||||
remote: {
|
|
||||||
app: {
|
|
||||||
getPath: jest.fn()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
dialog: jest.fn(),
|
dialog: jest.fn(),
|
||||||
ipcRenderer: {
|
ipcRenderer: {
|
||||||
on: jest.fn()
|
on: jest.fn()
|
||||||
|
|||||||
@ -26,7 +26,7 @@ import * as uuid from "uuid";
|
|||||||
import { ElectronApplication, Frame, Page, _electron as electron } from "playwright";
|
import { ElectronApplication, Frame, Page, _electron as electron } from "playwright";
|
||||||
import { noop } from "lodash";
|
import { noop } from "lodash";
|
||||||
|
|
||||||
export const AppPaths: Partial<Record<NodeJS.Platform, string>> = {
|
export const appPaths: Partial<Record<NodeJS.Platform, string>> = {
|
||||||
"win32": "./dist/win-unpacked/OpenLens.exe",
|
"win32": "./dist/win-unpacked/OpenLens.exe",
|
||||||
"linux": "./dist/linux-unpacked/open-lens",
|
"linux": "./dist/linux-unpacked/open-lens",
|
||||||
"darwin": "./dist/mac/OpenLens.app/Contents/MacOS/OpenLens",
|
"darwin": "./dist/mac/OpenLens.app/Contents/MacOS/OpenLens",
|
||||||
@ -65,7 +65,7 @@ export async function start() {
|
|||||||
|
|
||||||
const app = await electron.launch({
|
const app = await electron.launch({
|
||||||
args: ["--integration-testing"], // this argument turns off the blocking of quit
|
args: ["--integration-testing"], // this argument turns off the blocking of quit
|
||||||
executablePath: AppPaths[process.platform],
|
executablePath: appPaths[process.platform],
|
||||||
bypassCSP: true,
|
bypassCSP: true,
|
||||||
env: {
|
env: {
|
||||||
CICD,
|
CICD,
|
||||||
|
|||||||
@ -30,6 +30,7 @@ import { Console } from "console";
|
|||||||
import { stdout, stderr } from "process";
|
import { stdout, stderr } from "process";
|
||||||
import type { ClusterId } from "../cluster-types";
|
import type { ClusterId } from "../cluster-types";
|
||||||
import { getCustomKubeConfigPath } from "../utils";
|
import { getCustomKubeConfigPath } from "../utils";
|
||||||
|
import { AppPaths } from "../app-paths";
|
||||||
|
|
||||||
console = new Console(stdout, stderr);
|
console = new Console(stdout, stderr);
|
||||||
|
|
||||||
@ -67,23 +68,26 @@ function embed(clusterId: ClusterId, contents: any): string {
|
|||||||
return absPath;
|
return absPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
jest.mock("electron", () => {
|
jest.mock("electron", () => ({
|
||||||
return {
|
app: {
|
||||||
app: {
|
getVersion: () => "99.99.99",
|
||||||
getVersion: () => "99.99.99",
|
getName: () => "lens",
|
||||||
getPath: () => "tmp",
|
setName: jest.fn(),
|
||||||
getLocale: () => "en",
|
setPath: jest.fn(),
|
||||||
setLoginItemSettings: jest.fn(),
|
getPath: () => "tmp",
|
||||||
},
|
getLocale: () => "en",
|
||||||
ipcMain: {
|
setLoginItemSettings: jest.fn(),
|
||||||
handle: jest.fn(),
|
},
|
||||||
on: jest.fn(),
|
ipcMain: {
|
||||||
removeAllListeners: jest.fn(),
|
handle: jest.fn(),
|
||||||
off: jest.fn(),
|
on: jest.fn(),
|
||||||
send: jest.fn(),
|
removeAllListeners: jest.fn(),
|
||||||
}
|
off: jest.fn(),
|
||||||
};
|
send: jest.fn(),
|
||||||
});
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
describe("empty config", () => {
|
describe("empty config", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
|||||||
@ -22,6 +22,7 @@
|
|||||||
import { anyObject } from "jest-mock-extended";
|
import { anyObject } from "jest-mock-extended";
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
|
import { AppPaths } from "../app-paths";
|
||||||
import { ClusterStore } from "../cluster-store";
|
import { ClusterStore } from "../cluster-store";
|
||||||
import { HotbarStore } from "../hotbar-store";
|
import { HotbarStore } from "../hotbar-store";
|
||||||
|
|
||||||
@ -116,16 +117,23 @@ const awsCluster = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
jest.mock("electron", () => {
|
jest.mock("electron", () => ({
|
||||||
return {
|
app: {
|
||||||
app: {
|
getVersion: () => "99.99.99",
|
||||||
getVersion: () => "99.99.99",
|
getName: () => "lens",
|
||||||
getPath: () => "tmp",
|
setName: jest.fn(),
|
||||||
getLocale: () => "en",
|
setPath: jest.fn(),
|
||||||
setLoginItemSettings: (): void => void 0,
|
getPath: () => "tmp",
|
||||||
}
|
getLocale: () => "en",
|
||||||
};
|
setLoginItemSettings: jest.fn(),
|
||||||
});
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
describe("HotbarStore", () => {
|
describe("HotbarStore", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|||||||
@ -21,16 +21,21 @@
|
|||||||
|
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
|
|
||||||
jest.mock("electron", () => {
|
jest.mock("electron", () => ({
|
||||||
return {
|
app: {
|
||||||
app: {
|
getVersion: () => "99.99.99",
|
||||||
getVersion: () => "99.99.99",
|
getName: () => "lens",
|
||||||
getPath: () => "tmp",
|
setName: jest.fn(),
|
||||||
getLocale: () => "en",
|
setPath: jest.fn(),
|
||||||
setLoginItemSettings: (): void => void 0,
|
getPath: () => "tmp",
|
||||||
}
|
getLocale: () => "en",
|
||||||
};
|
setLoginItemSettings: jest.fn(),
|
||||||
});
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
import { UserStore } from "../user-store";
|
import { UserStore } from "../user-store";
|
||||||
import { Console } from "console";
|
import { Console } from "console";
|
||||||
@ -39,8 +44,10 @@ import electron from "electron";
|
|||||||
import { stdout, stderr } from "process";
|
import { stdout, stderr } from "process";
|
||||||
import { ThemeStore } from "../../renderer/theme.store";
|
import { ThemeStore } from "../../renderer/theme.store";
|
||||||
import type { ClusterStoreModel } from "../cluster-store";
|
import type { ClusterStoreModel } from "../cluster-store";
|
||||||
|
import { AppPaths } from "../app-paths";
|
||||||
|
|
||||||
console = new Console(stdout, stderr);
|
console = new Console(stdout, stderr);
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
describe("user store tests", () => {
|
describe("user store tests", () => {
|
||||||
describe("for an empty config", () => {
|
describe("for an empty config", () => {
|
||||||
|
|||||||
117
src/common/app-paths.ts
Normal file
117
src/common/app-paths.ts
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2021 OpenLens Authors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
* this software and associated documentation files (the "Software"), to deal in
|
||||||
|
* the Software without restriction, including without limitation the rights to
|
||||||
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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";
|
||||||
|
|
||||||
|
export type PathName = Parameters<typeof app["getPath"]>[0];
|
||||||
|
|
||||||
|
const pathNames: PathName[] = [
|
||||||
|
"home",
|
||||||
|
"appData",
|
||||||
|
"userData",
|
||||||
|
"cache",
|
||||||
|
"temp",
|
||||||
|
"exe",
|
||||||
|
"module",
|
||||||
|
"desktop",
|
||||||
|
"documents",
|
||||||
|
"downloads",
|
||||||
|
"music",
|
||||||
|
"pictures",
|
||||||
|
"videos",
|
||||||
|
"logs",
|
||||||
|
"crashDumps",
|
||||||
|
];
|
||||||
|
|
||||||
|
if (isWindows) {
|
||||||
|
pathNames.push("recent");
|
||||||
|
}
|
||||||
|
|
||||||
|
export class AppPaths {
|
||||||
|
private static paths = observable.box<Record<PathName, string> | undefined>();
|
||||||
|
private static readonly ipcChannel = "get-app-paths";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the local copy of the paths from electron.
|
||||||
|
*/
|
||||||
|
static async init(): Promise<void> {
|
||||||
|
logger.info(`[APP-PATHS]: initializing`);
|
||||||
|
|
||||||
|
if (AppPaths.paths.get()) {
|
||||||
|
return void logger.error("[APP-PATHS]: init called more than once");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ipcMain) {
|
||||||
|
AppPaths.initMain();
|
||||||
|
} else {
|
||||||
|
await AppPaths.initRenderer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async initRenderer(): Promise<void> {
|
||||||
|
const paths = await ipcRenderer.invoke(AppPaths.ipcChannel);
|
||||||
|
|
||||||
|
if (!paths || typeof paths !== "object") {
|
||||||
|
throw Object.assign(new Error("[APP-PATHS]: ipc handler returned unexpected data"), { data: paths });
|
||||||
|
}
|
||||||
|
|
||||||
|
AppPaths.paths.set(paths);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An alternative to `app.getPath()` for use in renderer and common.
|
||||||
|
* This function throws if called before initialization.
|
||||||
|
* @param name The name of the path field
|
||||||
|
*/
|
||||||
|
static get(name: PathName): string {
|
||||||
|
if (!AppPaths.paths.get()) {
|
||||||
|
throw new Error("AppPaths.init() has not been called");
|
||||||
|
}
|
||||||
|
|
||||||
|
return AppPaths.paths.get()[name];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An async version of `AppPaths.get()` which waits for `AppPaths.init()` to
|
||||||
|
* be called before returning
|
||||||
|
*/
|
||||||
|
static async getAsync(name: PathName): Promise<string> {
|
||||||
|
await when(() => Boolean(AppPaths.paths.get()));
|
||||||
|
|
||||||
|
return AppPaths.paths.get()[name];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -30,7 +30,7 @@ import { broadcastMessage, ipcMainOn, ipcRendererOn } from "./ipc";
|
|||||||
import isEqual from "lodash/isEqual";
|
import isEqual from "lodash/isEqual";
|
||||||
import { isTestEnv } from "./vars";
|
import { isTestEnv } from "./vars";
|
||||||
import { kebabCase } from "lodash";
|
import { kebabCase } from "lodash";
|
||||||
import { getPath } from "./utils/getPath";
|
import { AppPaths } from "./app-paths";
|
||||||
|
|
||||||
export interface BaseStoreParams<T> extends ConfOptions<T> {
|
export interface BaseStoreParams<T> extends ConfOptions<T> {
|
||||||
syncOptions?: IReactionOptions;
|
syncOptions?: IReactionOptions;
|
||||||
@ -93,7 +93,7 @@ export abstract class BaseStore<T> extends Singleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected cwd() {
|
protected cwd() {
|
||||||
return getPath("userData");
|
return AppPaths.get("userData");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async saveToFile(model: T) {
|
protected async saveToFile(model: T) {
|
||||||
|
|||||||
@ -19,13 +19,12 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { ipcMain } from "electron";
|
import { app, ipcMain } from "electron";
|
||||||
import winston, { format } from "winston";
|
import winston, { format } from "winston";
|
||||||
import type Transport from "winston-transport";
|
import type Transport from "winston-transport";
|
||||||
import { consoleFormat } from "winston-console-format";
|
import { consoleFormat } from "winston-console-format";
|
||||||
import { isDebugging, isTestEnv } from "./vars";
|
import { isDebugging, isTestEnv } from "./vars";
|
||||||
import BrowserConsole from "winston-transport-browserconsole";
|
import BrowserConsole from "winston-transport-browserconsole";
|
||||||
import { getPath } from "./utils/getPath";
|
|
||||||
|
|
||||||
const logLevel = process.env.LOG_LEVEL
|
const logLevel = process.env.LOG_LEVEL
|
||||||
? process.env.LOG_LEVEL
|
? process.env.LOG_LEVEL
|
||||||
@ -66,7 +65,11 @@ if (ipcMain) {
|
|||||||
handleExceptions: false,
|
handleExceptions: false,
|
||||||
level: logLevel,
|
level: logLevel,
|
||||||
filename: "lens.log",
|
filename: "lens.log",
|
||||||
dirname: getPath("logs"),
|
/**
|
||||||
|
* SAFTEY: the `ipcMain` check above should mean that this is only
|
||||||
|
* called in the main process
|
||||||
|
*/
|
||||||
|
dirname: app.getPath("logs"),
|
||||||
maxsize: 16 * 1024,
|
maxsize: 16 * 1024,
|
||||||
maxFiles: 16,
|
maxFiles: 16,
|
||||||
tailable: true,
|
tailable: true,
|
||||||
|
|||||||
@ -33,7 +33,7 @@ import { ObservableToggleSet, toJS } from "../../renderer/utils";
|
|||||||
import { DESCRIPTORS, KubeconfigSyncValue, UserPreferencesModel, EditorConfiguration } from "./preferences-helpers";
|
import { DESCRIPTORS, KubeconfigSyncValue, UserPreferencesModel, EditorConfiguration } from "./preferences-helpers";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
import type { monaco } from "react-monaco-editor";
|
import type { monaco } from "react-monaco-editor";
|
||||||
import { getPath } from "../utils/getPath";
|
import { AppPaths } from "../app-paths";
|
||||||
|
|
||||||
export interface UserStoreModel {
|
export interface UserStoreModel {
|
||||||
lastSeenAppVersion: string;
|
lastSeenAppVersion: string;
|
||||||
@ -257,5 +257,5 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
|
|||||||
* @returns string
|
* @returns string
|
||||||
*/
|
*/
|
||||||
export function getDefaultKubectlDownloadPath(): string {
|
export function getDefaultKubectlDownloadPath(): string {
|
||||||
return path.join(getPath("userData"), "binaries");
|
return path.join(AppPaths.get("userData"), "binaries");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,6 +46,11 @@ export function getClusterFrameUrl(clusterId: ClusterId) {
|
|||||||
* Get the result of `getClusterIdFromHost` from the current `location.host`
|
* Get the result of `getClusterIdFromHost` from the current `location.host`
|
||||||
*/
|
*/
|
||||||
export function getHostedClusterId(): ClusterId | undefined {
|
export function getHostedClusterId(): ClusterId | undefined {
|
||||||
|
// catch being called in main
|
||||||
|
if (typeof location === "undefined") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
return getClusterIdFromHost(location.host);
|
return getClusterIdFromHost(location.host);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -32,19 +32,21 @@ export * from "./base64";
|
|||||||
export * from "./camelCase";
|
export * from "./camelCase";
|
||||||
export * from "./cloneJson";
|
export * from "./cloneJson";
|
||||||
export * from "./cluster-id-url-parsing";
|
export * from "./cluster-id-url-parsing";
|
||||||
|
export * from "./convertCpu";
|
||||||
|
export * from "./convertMemory";
|
||||||
export * from "./debouncePromise";
|
export * from "./debouncePromise";
|
||||||
export * from "./defineGlobal";
|
export * from "./defineGlobal";
|
||||||
export * from "./delay";
|
export * from "./delay";
|
||||||
export * from "./disposer";
|
export * from "./disposer";
|
||||||
export * from "./downloadFile";
|
export * from "./downloadFile";
|
||||||
export * from "./formatDuration";
|
|
||||||
export * from "./escapeRegExp";
|
export * from "./escapeRegExp";
|
||||||
export * from "./extended-map";
|
export * from "./extended-map";
|
||||||
export * from "./getPath";
|
export * from "./formatDuration";
|
||||||
export * from "./getRandId";
|
export * from "./getRandId";
|
||||||
export * from "./hash-set";
|
export * from "./hash-set";
|
||||||
export * from "./local-kubeconfig";
|
export * from "./local-kubeconfig";
|
||||||
export * from "./n-fircate";
|
export * from "./n-fircate";
|
||||||
|
export * from "./objects";
|
||||||
export * from "./openExternal";
|
export * from "./openExternal";
|
||||||
export * from "./paths";
|
export * from "./paths";
|
||||||
export * from "./reject-promise";
|
export * from "./reject-promise";
|
||||||
@ -56,8 +58,6 @@ export * from "./toggle-set";
|
|||||||
export * from "./toJS";
|
export * from "./toJS";
|
||||||
export * from "./type-narrowing";
|
export * from "./type-narrowing";
|
||||||
export * from "./types";
|
export * from "./types";
|
||||||
export * from "./convertMemory";
|
|
||||||
export * from "./convertCpu";
|
|
||||||
|
|
||||||
import * as iter from "./iter";
|
import * as iter from "./iter";
|
||||||
import * as array from "./array";
|
import * as array from "./array";
|
||||||
|
|||||||
@ -21,11 +21,11 @@
|
|||||||
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import * as uuid from "uuid";
|
import * as uuid from "uuid";
|
||||||
|
import { AppPaths } from "../app-paths";
|
||||||
import type { ClusterId } from "../cluster-types";
|
import type { ClusterId } from "../cluster-types";
|
||||||
import { getPath } from "./getPath";
|
|
||||||
|
|
||||||
export function storedKubeConfigFolder(): string {
|
export function storedKubeConfigFolder(): string {
|
||||||
return path.resolve(getPath("userData"), "kubeconfigs");
|
return path.resolve(AppPaths.get("userData"), "kubeconfigs");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCustomKubeConfigPath(clusterId: ClusterId = uuid.v4()): string {
|
export function getCustomKubeConfigPath(clusterId: ClusterId = uuid.v4()): string {
|
||||||
|
|||||||
@ -19,19 +19,10 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { app, ipcMain } from "electron";
|
|
||||||
|
|
||||||
const remote = ipcMain ? null : require("@electron/remote");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* calls getPath either on app or on the remote's app
|
* A better typed version of `Object.fromEntries` where the keys are known to
|
||||||
*
|
* be a specific subset
|
||||||
* @deprecated Use a different method for accessing the getPath function
|
|
||||||
*/
|
*/
|
||||||
export function getPath(name: Parameters<typeof app["getPath"]>[0]): string {
|
export function fromEntries<T, Key extends string>(entries: Iterable<readonly [Key, T]>): { [k in Key]: T } {
|
||||||
if (app) {
|
return Object.fromEntries(entries) as { [k in Key]: T };
|
||||||
return app.getPath(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
return remote.app.getPath(name);
|
|
||||||
}
|
}
|
||||||
@ -26,6 +26,7 @@ import path from "path";
|
|||||||
import { ExtensionDiscovery } from "../extension-discovery";
|
import { ExtensionDiscovery } from "../extension-discovery";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
import { Console } from "console";
|
import { Console } from "console";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
jest.setTimeout(60_000);
|
jest.setTimeout(60_000);
|
||||||
|
|
||||||
@ -41,11 +42,22 @@ jest.mock("../extension-installer", () => ({
|
|||||||
}));
|
}));
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
setLoginItemSettings: jest.fn(),
|
setLoginItemSettings: jest.fn(),
|
||||||
},
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
console = new Console(process.stdout, process.stderr); // fix mockFS
|
console = new Console(process.stdout, process.stderr); // fix mockFS
|
||||||
const mockedWatch = watch as jest.MockedFunction<typeof watch>;
|
const mockedWatch = watch as jest.MockedFunction<typeof watch>;
|
||||||
|
|
||||||
|
|||||||
@ -24,9 +24,10 @@ import { EventEmitter } from "events";
|
|||||||
import { isEqual } from "lodash";
|
import { isEqual } from "lodash";
|
||||||
import { action, computed, makeObservable, observable, observe, reaction, when } from "mobx";
|
import { action, computed, makeObservable, observable, observe, reaction, when } from "mobx";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import { AppPaths } from "../common/app-paths";
|
||||||
import { ClusterStore } from "../common/cluster-store";
|
import { ClusterStore } from "../common/cluster-store";
|
||||||
import { broadcastMessage, ipcMainOn, ipcRendererOn, requestMain, ipcMainHandle } from "../common/ipc";
|
import { broadcastMessage, ipcMainOn, ipcRendererOn, requestMain, ipcMainHandle } from "../common/ipc";
|
||||||
import { Disposer, getHostedClusterId, Singleton, toJS, getPath } from "../common/utils";
|
import { Disposer, getHostedClusterId, Singleton, toJS } from "../common/utils";
|
||||||
import logger from "../main/logger";
|
import logger from "../main/logger";
|
||||||
import type { InstalledExtension } from "./extension-discovery";
|
import type { InstalledExtension } from "./extension-discovery";
|
||||||
import { ExtensionsStore } from "./extensions-store";
|
import { ExtensionsStore } from "./extensions-store";
|
||||||
@ -36,7 +37,7 @@ import type { LensRendererExtension } from "./lens-renderer-extension";
|
|||||||
import * as registries from "./registries";
|
import * as registries from "./registries";
|
||||||
|
|
||||||
export function extensionPackagesRoot() {
|
export function extensionPackagesRoot() {
|
||||||
return path.join(getPath("userData"));
|
return path.join(AppPaths.get("userData"));
|
||||||
}
|
}
|
||||||
|
|
||||||
const logModule = "[EXTENSIONS-LOADER]";
|
const logModule = "[EXTENSIONS-LOADER]";
|
||||||
|
|||||||
@ -28,15 +28,28 @@ import { stdout, stderr } from "process";
|
|||||||
import { ThemeStore } from "../../../renderer/theme.store";
|
import { ThemeStore } from "../../../renderer/theme.store";
|
||||||
import { TerminalStore } from "../../renderer-api/components";
|
import { TerminalStore } from "../../renderer-api/components";
|
||||||
import { UserStore } from "../../../common/user-store";
|
import { UserStore } from "../../../common/user-store";
|
||||||
|
import { AppPaths } from "../../../common/app-paths";
|
||||||
|
|
||||||
jest.mock("react-monaco-editor", () => null);
|
jest.mock("react-monaco-editor", () => null);
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
console = new Console(stdout, stderr);
|
console = new Console(stdout, stderr);
|
||||||
|
|
||||||
let ext: LensExtension = null;
|
let ext: LensExtension = null;
|
||||||
|
|||||||
@ -23,12 +23,22 @@ import { UserStore } from "../../common/user-store";
|
|||||||
import { ContextHandler } from "../context-handler";
|
import { ContextHandler } from "../context-handler";
|
||||||
import { PrometheusProvider, PrometheusProviderRegistry, PrometheusService } from "../prometheus";
|
import { PrometheusProvider, PrometheusProviderRegistry, PrometheusService } from "../prometheus";
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
setLoginItemSettings: jest.fn(),
|
setLoginItemSettings: jest.fn(),
|
||||||
},
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
enum ServiceResult {
|
enum ServiceResult {
|
||||||
@ -76,6 +86,8 @@ function getHandler() {
|
|||||||
}) as any);
|
}) as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
describe("ContextHandler", () => {
|
describe("ContextHandler", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockFs({
|
mockFs({
|
||||||
|
|||||||
@ -19,15 +19,6 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const logger = {
|
|
||||||
silly: jest.fn(),
|
|
||||||
debug: jest.fn(),
|
|
||||||
log: jest.fn(),
|
|
||||||
info: jest.fn(),
|
|
||||||
error: jest.fn(),
|
|
||||||
crit: jest.fn(),
|
|
||||||
};
|
|
||||||
|
|
||||||
jest.mock("winston", () => ({
|
jest.mock("winston", () => ({
|
||||||
format: {
|
format: {
|
||||||
colorize: jest.fn(),
|
colorize: jest.fn(),
|
||||||
@ -35,26 +26,27 @@ jest.mock("winston", () => ({
|
|||||||
simple: jest.fn(),
|
simple: jest.fn(),
|
||||||
label: jest.fn(),
|
label: jest.fn(),
|
||||||
timestamp: jest.fn(),
|
timestamp: jest.fn(),
|
||||||
printf: jest.fn()
|
printf: jest.fn(),
|
||||||
|
padLevels: jest.fn(),
|
||||||
|
ms: jest.fn(),
|
||||||
},
|
},
|
||||||
createLogger: jest.fn().mockReturnValue(logger),
|
createLogger: jest.fn().mockReturnValue({
|
||||||
|
silly: jest.fn(),
|
||||||
|
debug: jest.fn(),
|
||||||
|
log: jest.fn(),
|
||||||
|
info: jest.fn(),
|
||||||
|
error: jest.fn(),
|
||||||
|
crit: jest.fn(),
|
||||||
|
}),
|
||||||
transports: {
|
transports: {
|
||||||
Console: jest.fn(),
|
Console: jest.fn(),
|
||||||
File: jest.fn(),
|
File: jest.fn(),
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
|
||||||
app: {
|
|
||||||
getPath: () => "tmp",
|
|
||||||
setLoginItemSettings: jest.fn(),
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
jest.mock("../../common/ipc");
|
jest.mock("../../common/ipc");
|
||||||
jest.mock("child_process");
|
jest.mock("child_process");
|
||||||
jest.mock("tcp-port-used");
|
jest.mock("tcp-port-used");
|
||||||
//jest.mock("../utils/get-port");
|
|
||||||
|
|
||||||
import { Cluster } from "../cluster";
|
import { Cluster } from "../cluster";
|
||||||
import { KubeAuthProxy } from "../kube-auth-proxy";
|
import { KubeAuthProxy } from "../kube-auth-proxy";
|
||||||
@ -68,6 +60,7 @@ import { UserStore } from "../../common/user-store";
|
|||||||
import { Console } from "console";
|
import { Console } from "console";
|
||||||
import { stdout, stderr } from "process";
|
import { stdout, stderr } from "process";
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
console = new Console(stdout, stderr);
|
console = new Console(stdout, stderr);
|
||||||
|
|
||||||
@ -75,6 +68,23 @@ const mockBroadcastIpc = broadcastMessage as jest.MockedFunction<typeof broadcas
|
|||||||
const mockSpawn = spawn as jest.MockedFunction<typeof spawn>;
|
const mockSpawn = spawn as jest.MockedFunction<typeof spawn>;
|
||||||
const mockWaitUntilUsed = waitUntilUsed as jest.MockedFunction<typeof waitUntilUsed>;
|
const mockWaitUntilUsed = waitUntilUsed as jest.MockedFunction<typeof waitUntilUsed>;
|
||||||
|
|
||||||
|
jest.mock("electron", () => ({
|
||||||
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
describe("kube auth proxy tests", () => {
|
describe("kube auth proxy tests", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
|
|||||||
@ -28,12 +28,6 @@ const logger = {
|
|||||||
crit: jest.fn(),
|
crit: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
|
||||||
app: {
|
|
||||||
getPath: () => `/tmp`,
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
||||||
jest.mock("winston", () => ({
|
jest.mock("winston", () => ({
|
||||||
format: {
|
format: {
|
||||||
colorize: jest.fn(),
|
colorize: jest.fn(),
|
||||||
@ -41,7 +35,9 @@ jest.mock("winston", () => ({
|
|||||||
simple: jest.fn(),
|
simple: jest.fn(),
|
||||||
label: jest.fn(),
|
label: jest.fn(),
|
||||||
timestamp: jest.fn(),
|
timestamp: jest.fn(),
|
||||||
printf: jest.fn()
|
padLevels: jest.fn(),
|
||||||
|
ms: jest.fn(),
|
||||||
|
printf: jest.fn(),
|
||||||
},
|
},
|
||||||
createLogger: jest.fn().mockReturnValue(logger),
|
createLogger: jest.fn().mockReturnValue(logger),
|
||||||
transports: {
|
transports: {
|
||||||
@ -58,6 +54,25 @@ import fse from "fs-extra";
|
|||||||
import { loadYaml } from "@kubernetes/client-node";
|
import { loadYaml } from "@kubernetes/client-node";
|
||||||
import { Console } from "console";
|
import { Console } from "console";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
|
jest.mock("electron", () => ({
|
||||||
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
console = new Console(process.stdout, process.stderr); // fix mockFS
|
console = new Console(process.stdout, process.stderr); // fix mockFS
|
||||||
|
|
||||||
@ -111,7 +126,7 @@ describe("kubeconfig manager tests", () => {
|
|||||||
const kubeConfManager = new KubeconfigManager(cluster, contextHandler);
|
const kubeConfManager = new KubeconfigManager(cluster, contextHandler);
|
||||||
|
|
||||||
expect(logger.error).not.toBeCalled();
|
expect(logger.error).not.toBeCalled();
|
||||||
expect(await kubeConfManager.getPath()).toBe(`${path.sep}tmp${path.sep}kubeconfig-foo`);
|
expect(await kubeConfManager.getPath()).toBe(`tmp${path.sep}kubeconfig-foo`);
|
||||||
// this causes an intermittent "ENXIO: no such device or address, read" error
|
// this causes an intermittent "ENXIO: no such device or address, read" error
|
||||||
// const file = await fse.readFile(await kubeConfManager.getPath());
|
// const file = await fse.readFile(await kubeConfManager.getPath());
|
||||||
const file = fse.readFileSync(await kubeConfManager.getPath());
|
const file = fse.readFileSync(await kubeConfManager.getPath());
|
||||||
|
|||||||
@ -19,8 +19,27 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
import { Router } from "../router";
|
import { Router } from "../router";
|
||||||
|
|
||||||
|
jest.mock("electron", () => ({
|
||||||
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
describe("Router", () => {
|
describe("Router", () => {
|
||||||
it("blocks path traversal attacks", async () => {
|
it("blocks path traversal attacks", async () => {
|
||||||
const response: any = {
|
const response: any = {
|
||||||
|
|||||||
@ -28,13 +28,26 @@ import mockFs from "mock-fs";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { ClusterStore } from "../../../common/cluster-store";
|
import { ClusterStore } from "../../../common/cluster-store";
|
||||||
import { ClusterManager } from "../../cluster-manager";
|
import { ClusterManager } from "../../cluster-manager";
|
||||||
|
import { AppPaths } from "../../../common/app-paths";
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
getPath: () => "/foo",
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
describe("kubeconfig-sync.source tests", () => {
|
describe("kubeconfig-sync.source tests", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockFs();
|
mockFs();
|
||||||
|
|||||||
@ -27,7 +27,7 @@ import path from "path";
|
|||||||
import { BaseStore } from "../common/base-store";
|
import { BaseStore } from "../common/base-store";
|
||||||
import type { LensExtensionId } from "../extensions/lens-extension";
|
import type { LensExtensionId } from "../extensions/lens-extension";
|
||||||
import { toJS } from "../common/utils";
|
import { toJS } from "../common/utils";
|
||||||
import { getPath } from "../common/utils/getPath";
|
import { AppPaths } from "../common/app-paths";
|
||||||
|
|
||||||
interface FSProvisionModel {
|
interface FSProvisionModel {
|
||||||
extensions: Record<string, string>; // extension names to paths
|
extensions: Record<string, string>; // extension names to paths
|
||||||
@ -55,7 +55,7 @@ export class FilesystemProvisionerStore extends BaseStore<FSProvisionModel> {
|
|||||||
if (!this.registeredExtensions.has(extensionName)) {
|
if (!this.registeredExtensions.has(extensionName)) {
|
||||||
const salt = randomBytes(32).toString("hex");
|
const salt = randomBytes(32).toString("hex");
|
||||||
const hashedName = SHA256(`${extensionName}/${salt}`).toString();
|
const hashedName = SHA256(`${extensionName}/${salt}`).toString();
|
||||||
const dirPath = path.resolve(getPath("userData"), "extension_data", hashedName);
|
const dirPath = path.resolve(AppPaths.get("userData"), "extension_data", hashedName);
|
||||||
|
|
||||||
this.registeredExtensions.set(extensionName, dirPath);
|
this.registeredExtensions.set(extensionName, dirPath);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,13 +63,12 @@ import { ensureDir } from "fs-extra";
|
|||||||
import { Router } from "./router";
|
import { Router } from "./router";
|
||||||
import { initMenu } from "./menu";
|
import { initMenu } from "./menu";
|
||||||
import { initTray } from "./tray";
|
import { initTray } from "./tray";
|
||||||
import * as path from "path";
|
|
||||||
import { kubeApiRequest, shellApiRequest } from "./proxy-functions";
|
import { kubeApiRequest, shellApiRequest } from "./proxy-functions";
|
||||||
|
import { AppPaths } from "../common/app-paths";
|
||||||
|
|
||||||
const onCloseCleanup = disposer();
|
const onCloseCleanup = disposer();
|
||||||
const onQuitCleanup = disposer();
|
const onQuitCleanup = disposer();
|
||||||
|
|
||||||
const workingDir = path.join(app.getPath("appData"), appName);
|
|
||||||
|
|
||||||
SentryInit();
|
SentryInit();
|
||||||
app.setName(appName);
|
app.setName(appName);
|
||||||
@ -82,12 +81,7 @@ if (app.setAsDefaultProtocolClient("lens")) {
|
|||||||
logger.info("📟 Protocol client register failed ❗");
|
logger.info("📟 Protocol client register failed ❗");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.env.CICD) {
|
AppPaths.init();
|
||||||
app.setPath("appData", process.env.CICD);
|
|
||||||
app.setPath("userData", path.join(process.env.CICD, appName));
|
|
||||||
} else {
|
|
||||||
app.setPath("userData", workingDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.LENS_DISABLE_GPU) {
|
if (process.env.LENS_DISABLE_GPU) {
|
||||||
app.disableHardwareAcceleration();
|
app.disableHardwareAcceleration();
|
||||||
@ -127,7 +121,7 @@ app.on("second-instance", (event, argv) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.on("ready", async () => {
|
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");
|
logger.info("🐚 Syncing shell environment");
|
||||||
await shellSync();
|
await shellSync();
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,7 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* 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 { KubernetesCluster } from "../../common/catalog-entities";
|
||||||
import { clusterFrameMap } from "../../common/cluster-frames";
|
import { clusterFrameMap } from "../../common/cluster-frames";
|
||||||
import { clusterActivateHandler, clusterSetFrameIdHandler, clusterVisibilityHandler, clusterRefreshHandler, clusterDisconnectHandler, clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler, clusterDeleteHandler, clusterSetDeletingHandler, clusterClearDeletingHandler } from "../../common/cluster-ipc";
|
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 { WindowManager } from "../window-manager";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { remove } from "fs-extra";
|
import { remove } from "fs-extra";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
export function initIpcMainHandlers() {
|
export function initIpcMainHandlers() {
|
||||||
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
||||||
@ -99,7 +100,7 @@ export function initIpcMainHandlers() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// remove the local storage file
|
// 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);
|
await remove(localStorageFilePath);
|
||||||
} catch {}
|
} catch {}
|
||||||
|
|||||||
@ -22,15 +22,15 @@
|
|||||||
import type { KubeConfig } from "@kubernetes/client-node";
|
import type { KubeConfig } from "@kubernetes/client-node";
|
||||||
import type { Cluster } from "./cluster";
|
import type { Cluster } from "./cluster";
|
||||||
import type { ContextHandler } from "./context-handler";
|
import type { ContextHandler } from "./context-handler";
|
||||||
import { app } from "electron";
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import fs from "fs-extra";
|
import fs from "fs-extra";
|
||||||
import { dumpConfigYaml } from "../common/kube-helpers";
|
import { dumpConfigYaml } from "../common/kube-helpers";
|
||||||
import logger from "./logger";
|
import logger from "./logger";
|
||||||
import { LensProxy } from "./lens-proxy";
|
import { LensProxy } from "./lens-proxy";
|
||||||
|
import { AppPaths } from "../common/app-paths";
|
||||||
|
|
||||||
export class KubeconfigManager {
|
export class KubeconfigManager {
|
||||||
protected configDir = app.getPath("temp");
|
protected configDir = AppPaths.get("temp");
|
||||||
protected tempFile: string = null;
|
protected tempFile: string = null;
|
||||||
|
|
||||||
constructor(protected cluster: Cluster, protected contextHandler: ContextHandler) { }
|
constructor(protected cluster: Cluster, protected contextHandler: ContextHandler) { }
|
||||||
|
|||||||
@ -31,8 +31,8 @@ import { customRequest } from "../common/request";
|
|||||||
import { getBundledKubectlVersion } from "../common/utils/app-version";
|
import { getBundledKubectlVersion } from "../common/utils/app-version";
|
||||||
import { isDevelopment, isWindows, isTestEnv } from "../common/vars";
|
import { isDevelopment, isWindows, isTestEnv } from "../common/vars";
|
||||||
import { SemVer } from "semver";
|
import { SemVer } from "semver";
|
||||||
import { getPath } from "../common/utils/getPath";
|
|
||||||
import { defaultPackageMirror, packageMirrors } from "../common/user-store/preferences-helpers";
|
import { defaultPackageMirror, packageMirrors } from "../common/user-store/preferences-helpers";
|
||||||
|
import { AppPaths } from "../common/app-paths";
|
||||||
|
|
||||||
const bundledVersion = getBundledKubectlVersion();
|
const bundledVersion = getBundledKubectlVersion();
|
||||||
const kubectlMap: Map<string, string> = new Map([
|
const kubectlMap: Map<string, string> = new Map([
|
||||||
@ -81,7 +81,7 @@ export class Kubectl {
|
|||||||
protected dirname: string;
|
protected dirname: string;
|
||||||
|
|
||||||
static get kubectlDir() {
|
static get kubectlDir() {
|
||||||
return path.join(getPath("userData"), "binaries", "kubectl");
|
return path.join(AppPaths.get("userData"), "binaries", "kubectl");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly bundledKubectlVersion: string = bundledVersion;
|
public static readonly bundledKubectlVersion: string = bundledVersion;
|
||||||
|
|||||||
@ -29,16 +29,28 @@ import { ExtensionLoader } from "../../../extensions/extension-loader";
|
|||||||
import { ExtensionsStore } from "../../../extensions/extensions-store";
|
import { ExtensionsStore } from "../../../extensions/extensions-store";
|
||||||
import { LensProtocolRouterMain } from "../router";
|
import { LensProtocolRouterMain } from "../router";
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
|
import { AppPaths } from "../../../common/app-paths";
|
||||||
|
|
||||||
jest.mock("../../../common/ipc");
|
jest.mock("../../../common/ipc");
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
setLoginItemSettings: jest.fn(),
|
setLoginItemSettings: jest.fn(),
|
||||||
},
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
function throwIfDefined(val: any): void {
|
function throwIfDefined(val: any): void {
|
||||||
if (val != null) {
|
if (val != null) {
|
||||||
throw val;
|
throw val;
|
||||||
|
|||||||
@ -23,12 +23,12 @@
|
|||||||
// convert file path cluster icons to their base64 encoded versions
|
// convert file path cluster icons to their base64 encoded versions
|
||||||
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { app } from "electron";
|
|
||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import { loadConfigFromFileSync } from "../../common/kube-helpers";
|
import { loadConfigFromFileSync } from "../../common/kube-helpers";
|
||||||
import { MigrationDeclaration, migrationLog } from "../helpers";
|
import { MigrationDeclaration, migrationLog } from "../helpers";
|
||||||
import type { ClusterModel } from "../../common/cluster-types";
|
import type { ClusterModel } from "../../common/cluster-types";
|
||||||
import { getCustomKubeConfigPath, storedKubeConfigFolder } from "../../common/utils";
|
import { getCustomKubeConfigPath, storedKubeConfigFolder } from "../../common/utils";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
interface Pre360ClusterModel extends ClusterModel {
|
interface Pre360ClusterModel extends ClusterModel {
|
||||||
kubeConfig: string;
|
kubeConfig: string;
|
||||||
@ -37,7 +37,7 @@ interface Pre360ClusterModel extends ClusterModel {
|
|||||||
export default {
|
export default {
|
||||||
version: "3.6.0-beta.1",
|
version: "3.6.0-beta.1",
|
||||||
run(store) {
|
run(store) {
|
||||||
const userDataPath = app.getPath("userData");
|
const userDataPath = AppPaths.get("userData");
|
||||||
const storedClusters: Pre360ClusterModel[] = store.get("clusters") ?? [];
|
const storedClusters: Pre360ClusterModel[] = store.get("clusters") ?? [];
|
||||||
const migratedClusters: ClusterModel[] = [];
|
const migratedClusters: ClusterModel[] = [];
|
||||||
|
|
||||||
|
|||||||
@ -20,10 +20,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { app } from "electron";
|
|
||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import type { ClusterModel } from "../../common/cluster-types";
|
import type { ClusterModel } from "../../common/cluster-types";
|
||||||
import type { MigrationDeclaration } from "../helpers";
|
import type { MigrationDeclaration } from "../helpers";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
interface Pre500WorkspaceStoreModel {
|
interface Pre500WorkspaceStoreModel {
|
||||||
workspaces: {
|
workspaces: {
|
||||||
@ -35,7 +35,7 @@ interface Pre500WorkspaceStoreModel {
|
|||||||
export default {
|
export default {
|
||||||
version: "5.0.0-beta.10",
|
version: "5.0.0-beta.10",
|
||||||
run(store) {
|
run(store) {
|
||||||
const userDataPath = app.getPath("userData");
|
const userDataPath = AppPaths.get("userData");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const workspaceData: Pre500WorkspaceStoreModel = fse.readJsonSync(path.join(userDataPath, "lens-workspace-store.json"));
|
const workspaceData: Pre500WorkspaceStoreModel = fse.readJsonSync(path.join(userDataPath, "lens-workspace-store.json"));
|
||||||
|
|||||||
@ -23,8 +23,8 @@ import type { ClusterModel, ClusterPreferences, ClusterPrometheusPreferences } f
|
|||||||
import { MigrationDeclaration, migrationLog } from "../helpers";
|
import { MigrationDeclaration, migrationLog } from "../helpers";
|
||||||
import { generateNewIdFor } from "../utils";
|
import { generateNewIdFor } from "../utils";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { app } from "electron";
|
|
||||||
import { moveSync, removeSync } from "fs-extra";
|
import { moveSync, removeSync } from "fs-extra";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
function mergePrometheusPreferences(left: ClusterPrometheusPreferences, right: ClusterPrometheusPreferences): ClusterPrometheusPreferences {
|
function mergePrometheusPreferences(left: ClusterPrometheusPreferences, right: ClusterPrometheusPreferences): ClusterPrometheusPreferences {
|
||||||
if (left.prometheus && left.prometheusProvider) {
|
if (left.prometheus && left.prometheusProvider) {
|
||||||
@ -106,7 +106,7 @@ function moveStorageFolder({ folder, newId, oldId }: { folder: string, newId: st
|
|||||||
export default {
|
export default {
|
||||||
version: "5.0.0-beta.13",
|
version: "5.0.0-beta.13",
|
||||||
run(store) {
|
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 oldClusters: ClusterModel[] = store.get("clusters") ?? [];
|
||||||
const clusters = new Map<string, ClusterModel>();
|
const clusters = new Map<string, ClusterModel>();
|
||||||
|
|||||||
@ -19,11 +19,11 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { app } from "electron";
|
|
||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import { isNull } from "lodash";
|
import { isNull } from "lodash";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import * as uuid from "uuid";
|
import * as uuid from "uuid";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
import type { ClusterStoreModel } from "../../common/cluster-store";
|
import type { ClusterStoreModel } from "../../common/cluster-store";
|
||||||
import { defaultHotbarCells, getEmptyHotbar, Hotbar, HotbarItem } from "../../common/hotbar-types";
|
import { defaultHotbarCells, getEmptyHotbar, Hotbar, HotbarItem } from "../../common/hotbar-types";
|
||||||
import { catalogEntity } from "../../main/catalog-sources/general";
|
import { catalogEntity } from "../../main/catalog-sources/general";
|
||||||
@ -48,7 +48,7 @@ export default {
|
|||||||
run(store) {
|
run(store) {
|
||||||
const rawHotbars = store.get("hotbars");
|
const rawHotbars = store.get("hotbars");
|
||||||
const hotbars: Hotbar[] = Array.isArray(rawHotbars) ? rawHotbars.filter(h => h && typeof h === "object") : [];
|
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
|
// Hotbars might be empty, if some of the previous migrations weren't run
|
||||||
if (hotbars.length === 0) {
|
if (hotbars.length === 0) {
|
||||||
|
|||||||
@ -19,7 +19,6 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { app } from "electron";
|
|
||||||
import { existsSync, readFileSync } from "fs";
|
import { existsSync, readFileSync } from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import os from "os";
|
import os from "os";
|
||||||
@ -27,14 +26,16 @@ import type { ClusterStoreModel } from "../../common/cluster-store";
|
|||||||
import type { KubeconfigSyncEntry, UserPreferencesModel } from "../../common/user-store";
|
import type { KubeconfigSyncEntry, UserPreferencesModel } from "../../common/user-store";
|
||||||
import { MigrationDeclaration, migrationLog } from "../helpers";
|
import { MigrationDeclaration, migrationLog } from "../helpers";
|
||||||
import { isLogicalChildPath, storedKubeConfigFolder } from "../../common/utils";
|
import { isLogicalChildPath, storedKubeConfigFolder } from "../../common/utils";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
version: "5.0.3-beta.1",
|
version: "5.0.3-beta.1",
|
||||||
run(store) {
|
run(store) {
|
||||||
try {
|
try {
|
||||||
const { syncKubeconfigEntries = [], ...preferences }: UserPreferencesModel = store.get("preferences") ?? {};
|
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 userData = AppPaths.get("userData");
|
||||||
const extensionDataDir = path.resolve(app.getPath("userData"), "extension_data");
|
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));
|
const syncPaths = new Set(syncKubeconfigEntries.map(s => s.filePath));
|
||||||
|
|
||||||
syncPaths.add(path.join(os.homedir(), ".kube"));
|
syncPaths.add(path.join(os.homedir(), ".kube"));
|
||||||
|
|||||||
@ -19,12 +19,12 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { app } from "electron";
|
|
||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
export function fileNameMigration() {
|
export function fileNameMigration() {
|
||||||
const userDataPath = app.getPath("userData");
|
const userDataPath = AppPaths.get("userData");
|
||||||
const configJsonPath = path.join(userDataPath, "config.json");
|
const configJsonPath = path.join(userDataPath, "config.json");
|
||||||
const lensUserStoreJsonPath = path.join(userDataPath, "lens-user-store.json");
|
const lensUserStoreJsonPath = path.join(userDataPath, "lens-user-store.json");
|
||||||
|
|
||||||
|
|||||||
@ -36,8 +36,6 @@ import { ClusterStore } from "../common/cluster-store";
|
|||||||
import { UserStore } from "../common/user-store";
|
import { UserStore } from "../common/user-store";
|
||||||
import { ExtensionDiscovery } from "../extensions/extension-discovery";
|
import { ExtensionDiscovery } from "../extensions/extension-discovery";
|
||||||
import { ExtensionLoader } from "../extensions/extension-loader";
|
import { ExtensionLoader } from "../extensions/extension-loader";
|
||||||
import { App } from "./components/app";
|
|
||||||
import { LensApp } from "./lens-app";
|
|
||||||
import { HelmRepoManager } from "../main/helm/helm-repo-manager";
|
import { HelmRepoManager } from "../main/helm/helm-repo-manager";
|
||||||
import { ExtensionInstallationStateStore } from "./components/+extensions/extension-install.store";
|
import { ExtensionInstallationStateStore } from "./components/+extensions/extension-install.store";
|
||||||
import { DefaultProps } from "./mui-base-theme";
|
import { DefaultProps } from "./mui-base-theme";
|
||||||
@ -51,6 +49,7 @@ import { ThemeStore } from "./theme.store";
|
|||||||
import { SentryInit } from "../common/sentry";
|
import { SentryInit } from "../common/sentry";
|
||||||
import { TerminalStore } from "./components/dock/terminal.store";
|
import { TerminalStore } from "./components/dock/terminal.store";
|
||||||
import cloudsMidnight from "./monaco-themes/Clouds Midnight.json";
|
import cloudsMidnight from "./monaco-themes/Clouds Midnight.json";
|
||||||
|
import { AppPaths } from "../common/app-paths";
|
||||||
|
|
||||||
configurePackages();
|
configurePackages();
|
||||||
|
|
||||||
@ -69,7 +68,8 @@ type AppComponent = React.ComponentType & {
|
|||||||
init?(rootElem: HTMLElement): Promise<void>;
|
init?(rootElem: HTMLElement): Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function bootstrap(App: AppComponent) {
|
export async function bootstrap(comp: () => Promise<AppComponent>) {
|
||||||
|
await AppPaths.init();
|
||||||
const rootElem = document.getElementById("app");
|
const rootElem = document.getElementById("app");
|
||||||
|
|
||||||
await attachChromeDebugger();
|
await attachChromeDebugger();
|
||||||
@ -124,9 +124,10 @@ export async function bootstrap(App: AppComponent) {
|
|||||||
cs.registerIpcListener();
|
cs.registerIpcListener();
|
||||||
|
|
||||||
// init app's dependencies if any
|
// init app's dependencies if any
|
||||||
if (App.init) {
|
const App = await comp();
|
||||||
await App.init(rootElem);
|
|
||||||
}
|
await App.init(rootElem);
|
||||||
|
|
||||||
render(<>
|
render(<>
|
||||||
{isMac && <div id="draggable-top" />}
|
{isMac && <div id="draggable-top" />}
|
||||||
{DefaultProps(App)}
|
{DefaultProps(App)}
|
||||||
@ -134,7 +135,11 @@ export async function bootstrap(App: AppComponent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// run
|
// run
|
||||||
bootstrap(process.isMainFrame ? LensApp : App);
|
bootstrap(
|
||||||
|
async () => process.isMainFrame
|
||||||
|
? (await import("./lens-app")).LensApp
|
||||||
|
: (await import("./components/app")).App
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -31,26 +31,30 @@ import { CatalogEntityRegistry } from "../../../renderer/api/catalog-entity-regi
|
|||||||
import { CatalogEntityDetailRegistry } from "../../../extensions/registries";
|
import { CatalogEntityDetailRegistry } from "../../../extensions/registries";
|
||||||
import { CatalogEntityItem } from "./catalog-entity-item";
|
import { CatalogEntityItem } from "./catalog-entity-item";
|
||||||
import { CatalogEntityStore } from "./catalog-entity.store";
|
import { CatalogEntityStore } from "./catalog-entity.store";
|
||||||
|
import { AppPaths } from "../../../common/app-paths";
|
||||||
|
|
||||||
mockWindow();
|
mockWindow();
|
||||||
|
jest.mock("electron", () => ({
|
||||||
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
// avoid TypeError: Cannot read property 'getPath' of undefined
|
AppPaths.init();
|
||||||
jest.mock("@electron/remote", () => {
|
|
||||||
return {
|
|
||||||
app: {
|
|
||||||
getPath: () => {
|
|
||||||
// avoid TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
|
|
||||||
return "";
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
jest.mock("./hotbar-toggle-menu-item", () => {
|
jest.mock("./hotbar-toggle-menu-item", () => ({
|
||||||
return {
|
HotbarToggleMenuItem: () => <div>menu item</div>
|
||||||
HotbarToggleMenuItem: () => <div>menu item</div>
|
}));
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
class MockCatalogEntity extends CatalogEntity {
|
class MockCatalogEntity extends CatalogEntity {
|
||||||
public apiVersion = "api";
|
public apiVersion = "api";
|
||||||
|
|||||||
@ -37,7 +37,7 @@ import { CatalogAddButton } from "./catalog-add-button";
|
|||||||
import type { RouteComponentProps } from "react-router";
|
import type { RouteComponentProps } from "react-router";
|
||||||
import { Notifications } from "../notifications";
|
import { Notifications } from "../notifications";
|
||||||
import { MainLayout } from "../layout/main-layout";
|
import { MainLayout } from "../layout/main-layout";
|
||||||
import { createAppStorage, cssNames, prevDefault } from "../../utils";
|
import { createStorage, cssNames, prevDefault } from "../../utils";
|
||||||
import { makeCss } from "../../../common/utils/makeCss";
|
import { makeCss } from "../../../common/utils/makeCss";
|
||||||
import { CatalogEntityDetails } from "./catalog-entity-details";
|
import { CatalogEntityDetails } from "./catalog-entity-details";
|
||||||
import { browseCatalogTab, catalogURL, CatalogViewRouteParam } from "../../../common/routes";
|
import { browseCatalogTab, catalogURL, CatalogViewRouteParam } from "../../../common/routes";
|
||||||
@ -47,7 +47,7 @@ import { RenderDelay } from "../render-delay/render-delay";
|
|||||||
import { Icon } from "../icon";
|
import { Icon } from "../icon";
|
||||||
import { HotbarToggleMenuItem } from "./hotbar-toggle-menu-item";
|
import { HotbarToggleMenuItem } from "./hotbar-toggle-menu-item";
|
||||||
|
|
||||||
export const previousActiveTab = createAppStorage("catalog-previous-active-tab", browseCatalogTab);
|
export const previousActiveTab = createStorage("catalog-previous-active-tab", browseCatalogTab);
|
||||||
|
|
||||||
enum sortBy {
|
enum sortBy {
|
||||||
name = "name",
|
name = "name",
|
||||||
|
|||||||
@ -31,6 +31,7 @@ import { ExtensionInstallationStateStore } from "../extension-install.store";
|
|||||||
import { Extensions } from "../extensions";
|
import { Extensions } from "../extensions";
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
import { mockWindow } from "../../../../../__mocks__/windowMock";
|
import { mockWindow } from "../../../../../__mocks__/windowMock";
|
||||||
|
import { AppPaths } from "../../../../common/app-paths";
|
||||||
|
|
||||||
mockWindow();
|
mockWindow();
|
||||||
|
|
||||||
@ -38,23 +39,39 @@ jest.setTimeout(30000);
|
|||||||
jest.mock("fs-extra");
|
jest.mock("fs-extra");
|
||||||
jest.mock("../../notifications");
|
jest.mock("../../notifications");
|
||||||
|
|
||||||
jest.mock("../../../../common/utils", () => ({
|
jest.mock("../../../../common/utils/downloadFile", () => ({
|
||||||
...jest.requireActual<any>("../../../../common/utils"),
|
downloadFile: jest.fn(({ url }) => ({
|
||||||
downloadFile: jest.fn(() => ({
|
promise: Promise.resolve(),
|
||||||
promise: Promise.resolve()
|
url,
|
||||||
|
cancel: () => {},
|
||||||
|
})),
|
||||||
|
downloadJson: jest.fn(({ url }) => ({
|
||||||
|
promise: Promise.resolve({}),
|
||||||
|
url,
|
||||||
|
cancel: () => { },
|
||||||
})),
|
})),
|
||||||
extractTar: jest.fn(() => Promise.resolve())
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.mock("../../../../common/utils/tar");
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
getVersion: () => "99.99.99",
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
getLocale: () => "en",
|
getLocale: () => "en",
|
||||||
setLoginItemSettings: (): void => void 0,
|
setLoginItemSettings: jest.fn(),
|
||||||
}
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
describe("Extensions", () => {
|
describe("Extensions", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
mockFs({
|
mockFs({
|
||||||
|
|||||||
@ -47,7 +47,7 @@ import { Notice } from "./notice";
|
|||||||
import { SettingLayout } from "../layout/setting-layout";
|
import { SettingLayout } from "../layout/setting-layout";
|
||||||
import { docsUrl } from "../../../common/vars";
|
import { docsUrl } from "../../../common/vars";
|
||||||
import { dialog } from "../../remote-helpers";
|
import { dialog } from "../../remote-helpers";
|
||||||
import { getPath } from "../../../common/utils/getPath";
|
import { AppPaths } from "../../../common/app-paths";
|
||||||
|
|
||||||
function getMessageFromError(error: any): string {
|
function getMessageFromError(error: any): string {
|
||||||
if (!error || typeof error !== "object") {
|
if (!error || typeof error !== "object") {
|
||||||
@ -469,7 +469,7 @@ const supportedFormats = ["tar", "tgz"];
|
|||||||
|
|
||||||
async function installFromSelectFileDialog() {
|
async function installFromSelectFileDialog() {
|
||||||
const { canceled, filePaths } = await dialog.showOpenDialog({
|
const { canceled, filePaths } = await dialog.showOpenDialog({
|
||||||
defaultPath: getPath("downloads"),
|
defaultPath: AppPaths.get("downloads"),
|
||||||
properties: ["openFile", "multiSelections"],
|
properties: ["openFile", "multiSelections"],
|
||||||
message: `Select extensions to install (formats: ${supportedFormats.join(", ")}), `,
|
message: `Select extensions to install (formats: ${supportedFormats.join(", ")}), `,
|
||||||
buttonLabel: "Use configuration",
|
buttonLabel: "Use configuration",
|
||||||
|
|||||||
@ -27,13 +27,26 @@ import selectEvent from "react-select-event";
|
|||||||
|
|
||||||
import { Cluster } from "../../../../main/cluster";
|
import { Cluster } from "../../../../main/cluster";
|
||||||
import { DeleteClusterDialog } from "../delete-cluster-dialog";
|
import { DeleteClusterDialog } from "../delete-cluster-dialog";
|
||||||
|
import { AppPaths } from "../../../../common/app-paths";
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
const kubeconfig = `
|
const kubeconfig = `
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
clusters:
|
clusters:
|
||||||
|
|||||||
@ -30,6 +30,7 @@ import { noop } from "../../../utils";
|
|||||||
import { ThemeStore } from "../../../theme.store";
|
import { ThemeStore } from "../../../theme.store";
|
||||||
import { TerminalStore } from "../terminal.store";
|
import { TerminalStore } from "../terminal.store";
|
||||||
import { UserStore } from "../../../../common/user-store";
|
import { UserStore } from "../../../../common/user-store";
|
||||||
|
import { AppPaths } from "../../../../common/app-paths";
|
||||||
|
|
||||||
jest.mock("react-monaco-editor", () => ({
|
jest.mock("react-monaco-editor", () => ({
|
||||||
monaco: {
|
monaco: {
|
||||||
@ -41,9 +42,20 @@ jest.mock("react-monaco-editor", () => ({
|
|||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
const initialTabs: DockTab[] = [
|
const initialTabs: DockTab[] = [
|
||||||
{ id: "terminal", kind: TabKind.TERMINAL, title: "Terminal", pinned: false, },
|
{ id: "terminal", kind: TabKind.TERMINAL, title: "Terminal", pinned: false, },
|
||||||
|
|||||||
@ -31,15 +31,28 @@ import { dockerPod, deploymentPod1 } from "./pod.mock";
|
|||||||
import { ThemeStore } from "../../../theme.store";
|
import { ThemeStore } from "../../../theme.store";
|
||||||
import { UserStore } from "../../../../common/user-store";
|
import { UserStore } from "../../../../common/user-store";
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
|
import { AppPaths } from "../../../../common/app-paths";
|
||||||
|
|
||||||
jest.mock("react-monaco-editor", () => null);
|
jest.mock("react-monaco-editor", () => null);
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
const getComponent = (tabData: LogTabData) => {
|
const getComponent = (tabData: LogTabData) => {
|
||||||
return (
|
return (
|
||||||
<LogResourceSelector
|
<LogResourceSelector
|
||||||
|
|||||||
@ -28,6 +28,7 @@ import { logTabStore } from "../log-tab.store";
|
|||||||
import { deploymentPod1, deploymentPod2, deploymentPod3, dockerPod } from "./pod.mock";
|
import { deploymentPod1, deploymentPod2, deploymentPod3, dockerPod } from "./pod.mock";
|
||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import { mockWindow } from "../../../../../__mocks__/windowMock";
|
import { mockWindow } from "../../../../../__mocks__/windowMock";
|
||||||
|
import { AppPaths } from "../../../../common/app-paths";
|
||||||
|
|
||||||
mockWindow();
|
mockWindow();
|
||||||
|
|
||||||
@ -35,10 +36,22 @@ jest.mock("react-monaco-editor", () => null);
|
|||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
|
setLoginItemSettings: jest.fn(),
|
||||||
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
podsStore.items.push(new Pod(dockerPod));
|
podsStore.items.push(new Pod(dockerPod));
|
||||||
podsStore.items.push(new Pod(deploymentPod1));
|
podsStore.items.push(new Pod(deploymentPod1));
|
||||||
podsStore.items.push(new Pod(deploymentPod2));
|
podsStore.items.push(new Pod(deploymentPod2));
|
||||||
|
|||||||
@ -27,14 +27,26 @@ import { ThemeStore } from "../../../theme.store";
|
|||||||
import { UserStore } from "../../../../common/user-store";
|
import { UserStore } from "../../../../common/user-store";
|
||||||
import { Notifications } from "../../notifications";
|
import { Notifications } from "../../notifications";
|
||||||
import mockFs from "mock-fs";
|
import mockFs from "mock-fs";
|
||||||
|
import { AppPaths } from "../../../../common/app-paths";
|
||||||
|
|
||||||
jest.mock("electron", () => ({
|
jest.mock("electron", () => ({
|
||||||
app: {
|
app: {
|
||||||
|
getVersion: () => "99.99.99",
|
||||||
|
getName: () => "lens",
|
||||||
|
setName: jest.fn(),
|
||||||
|
setPath: jest.fn(),
|
||||||
getPath: () => "tmp",
|
getPath: () => "tmp",
|
||||||
|
getLocale: () => "en",
|
||||||
setLoginItemSettings: jest.fn(),
|
setLoginItemSettings: jest.fn(),
|
||||||
},
|
},
|
||||||
|
ipcMain: {
|
||||||
|
on: jest.fn(),
|
||||||
|
handle: jest.fn(),
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
AppPaths.init();
|
||||||
|
|
||||||
const mockHotbars: {[id: string]: any} = {
|
const mockHotbars: {[id: string]: any} = {
|
||||||
"1": {
|
"1": {
|
||||||
id: "1",
|
id: "1",
|
||||||
|
|||||||
@ -26,7 +26,6 @@ import { comparer, observable, reaction, toJS, when } from "mobx";
|
|||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import { StorageHelper } from "./storageHelper";
|
import { StorageHelper } from "./storageHelper";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
import { getHostedClusterId, getPath } from "../../common/utils";
|
|
||||||
import { isTestEnv } from "../../common/vars";
|
import { isTestEnv } from "../../common/vars";
|
||||||
|
|
||||||
const storage = observable({
|
const storage = observable({
|
||||||
@ -37,55 +36,48 @@ const storage = observable({
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
* @param key
|
* @param key The descriptor of the data
|
||||||
* @param defaultValue
|
* @param defaultValue The default value of the data, must be JSON serializable
|
||||||
*/
|
*/
|
||||||
export function createStorage<T>(key: string, defaultValue: T) {
|
export function createStorage<T>(key: string, defaultValue: T) {
|
||||||
return createAppStorage(key, defaultValue, getHostedClusterId());
|
|
||||||
}
|
|
||||||
|
|
||||||
export function createAppStorage<T>(key: string, defaultValue: T, clusterId?: string | undefined) {
|
|
||||||
const { logPrefix } = StorageHelper;
|
const { logPrefix } = StorageHelper;
|
||||||
const folder = path.resolve(getPath("userData"), "lens-local-storage");
|
|
||||||
const fileName = `${clusterId ?? "app"}.json`;
|
|
||||||
const filePath = path.resolve(folder, fileName);
|
|
||||||
|
|
||||||
if (!storage.initialized) {
|
if (!storage.initialized) {
|
||||||
init(); // called once per cluster-view
|
|
||||||
}
|
|
||||||
|
|
||||||
function init() {
|
|
||||||
storage.initialized = true;
|
storage.initialized = true;
|
||||||
|
|
||||||
// read previously saved state (if any)
|
(async () => {
|
||||||
fse.readJson(filePath)
|
const filePath = await StorageHelper.getLocalStoragePath();
|
||||||
.then(data => storage.data = data)
|
|
||||||
.catch(() => null) // ignore empty / non-existing / invalid json files
|
try {
|
||||||
.finally(() => {
|
storage.data = await fse.readJson(filePath);
|
||||||
|
} catch {} finally {
|
||||||
if (!isTestEnv) {
|
if (!isTestEnv) {
|
||||||
logger.info(`${logPrefix} loading finished for ${filePath}`);
|
logger.info(`${logPrefix} loading finished for ${filePath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
storage.loaded = true;
|
storage.loaded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// bind auto-saving data changes to %storage-file.json
|
||||||
|
reaction(() => toJS(storage.data), saveFile, {
|
||||||
|
delay: 250, // lazy, avoid excessive writes to fs
|
||||||
|
equals: comparer.structural, // save only when something really changed
|
||||||
});
|
});
|
||||||
|
|
||||||
// bind auto-saving data changes to %storage-file.json
|
async function saveFile(state: Record<string, any> = {}) {
|
||||||
reaction(() => toJS(storage.data), saveFile, {
|
logger.info(`${logPrefix} saving ${filePath}`);
|
||||||
delay: 250, // lazy, avoid excessive writes to fs
|
|
||||||
equals: comparer.structural, // save only when something really changed
|
|
||||||
});
|
|
||||||
|
|
||||||
async function saveFile(state: Record<string, any> = {}) {
|
try {
|
||||||
logger.info(`${logPrefix} saving ${filePath}`);
|
await fse.ensureDir(path.dirname(filePath), { mode: 0o755 });
|
||||||
|
await fse.writeJson(filePath, state, { spaces: 2 });
|
||||||
try {
|
} catch (error) {
|
||||||
await fse.ensureDir(folder, { mode: 0o755 });
|
logger.error(`${logPrefix} saving failed: ${error}`, {
|
||||||
await fse.writeJson(filePath, state, { spaces: 2 });
|
json: state, jsonFilePath: filePath
|
||||||
} catch (error) {
|
});
|
||||||
logger.error(`${logPrefix} saving failed: ${error}`, {
|
}
|
||||||
json: state, jsonFilePath: filePath
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
})()
|
||||||
|
.catch(error => logger.error(`${logPrefix} Failed to initialize storage: ${error}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StorageHelper<T>(key, {
|
return new StorageHelper<T>(key, {
|
||||||
|
|||||||
@ -24,6 +24,9 @@ import { action, comparer, makeObservable, observable, toJS, when, } from "mobx"
|
|||||||
import produce, { Draft, isDraft } from "immer";
|
import produce, { Draft, isDraft } from "immer";
|
||||||
import { isEqual, isPlainObject } from "lodash";
|
import { isEqual, isPlainObject } from "lodash";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
|
import { getHostedClusterId } from "../../common/utils";
|
||||||
|
import path from "path";
|
||||||
|
import { AppPaths } from "../../common/app-paths";
|
||||||
|
|
||||||
export interface StorageAdapter<T> {
|
export interface StorageAdapter<T> {
|
||||||
[metadata: string]: any;
|
[metadata: string]: any;
|
||||||
@ -40,6 +43,10 @@ export interface StorageHelperOptions<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class StorageHelper<T> {
|
export class StorageHelper<T> {
|
||||||
|
static async getLocalStoragePath() {
|
||||||
|
return path.resolve(await AppPaths.getAsync("userData"), "lens-local-storage", `${getHostedClusterId() || "app"}.json`);
|
||||||
|
}
|
||||||
|
|
||||||
static logPrefix = "[StorageHelper]:";
|
static logPrefix = "[StorageHelper]:";
|
||||||
readonly storage: StorageAdapter<T>;
|
readonly storage: StorageAdapter<T>;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user