diff --git a/src/common/utils/index.ts b/src/common/utils/index.ts index 6035b9f408..0b17076c71 100644 --- a/src/common/utils/index.ts +++ b/src/common/utils/index.ts @@ -32,14 +32,16 @@ export * from "./base64"; export * from "./camelCase"; export * from "./cloneJson"; export * from "./cluster-id-url-parsing"; +export * from "./convertCpu"; +export * from "./convertMemory"; export * from "./debouncePromise"; export * from "./defineGlobal"; export * from "./delay"; export * from "./disposer"; export * from "./downloadFile"; -export * from "./formatDuration"; export * from "./escapeRegExp"; export * from "./extended-map"; +export * from "./formatDuration"; export * from "./getPath"; export * from "./getRandId"; export * from "./hash-set"; @@ -47,6 +49,7 @@ export * from "./local-kubeconfig"; export * from "./n-fircate"; export * from "./openExternal"; export * from "./paths"; +export * from "./promise-exec"; export * from "./reject-promise"; export * from "./singleton"; export * from "./sort-compare"; @@ -56,8 +59,6 @@ export * from "./toggle-set"; export * from "./toJS"; export * from "./type-narrowing"; export * from "./types"; -export * from "./convertMemory"; -export * from "./convertCpu"; import * as iter from "./iter"; diff --git a/src/main/promise-exec.ts b/src/common/utils/promise-exec.ts similarity index 100% rename from src/main/promise-exec.ts rename to src/common/utils/promise-exec.ts diff --git a/src/main/helm/helm-chart-manager.ts b/src/main/helm/helm-chart-manager.ts index b8261845db..ec17645ce2 100644 --- a/src/main/helm/helm-chart-manager.ts +++ b/src/main/helm/helm-chart-manager.ts @@ -24,10 +24,9 @@ import v8 from "v8"; import * as yaml from "js-yaml"; import type { HelmRepo } from "./helm-repo-manager"; import logger from "../logger"; -import { promiseExec } from "../promise-exec"; import { helmCli } from "./helm-cli"; import type { RepoHelmChartList } from "../../common/k8s-api/endpoints/helm-charts.api"; -import { sortCharts } from "../../common/utils"; +import { sortCharts, promiseExec } from "../../common/utils"; export class HelmChartManager { static #cache = new Map(); diff --git a/src/main/helm/helm-release-manager.ts b/src/main/helm/helm-release-manager.ts index b8a18a1021..fbe6a67c26 100644 --- a/src/main/helm/helm-release-manager.ts +++ b/src/main/helm/helm-release-manager.ts @@ -22,10 +22,9 @@ import * as tempy from "tempy"; import fse from "fs-extra"; import * as yaml from "js-yaml"; -import { promiseExec } from "../promise-exec"; +import { promiseExec, toCamelCase } from "../../common/utils"; import { helmCli } from "./helm-cli"; import type { Cluster } from "../cluster"; -import { toCamelCase } from "../../common/utils/camelCase"; export async function listReleases(pathToKubeconfig: string, namespace?: string) { const helm = await helmCli.binaryPath(); diff --git a/src/main/helm/helm-repo-manager.ts b/src/main/helm/helm-repo-manager.ts index 9e4038a10f..3a093a18a0 100644 --- a/src/main/helm/helm-repo-manager.ts +++ b/src/main/helm/helm-repo-manager.ts @@ -21,9 +21,8 @@ import yaml from "js-yaml"; import { readFile } from "fs-extra"; -import { promiseExec } from "../promise-exec"; import { helmCli } from "./helm-cli"; -import { Singleton } from "../../common/utils/singleton"; +import { Singleton, promiseExec } from "../../common/utils"; import { customRequestPromise } from "../../common/request"; import orderBy from "lodash/orderBy"; import logger from "../logger"; diff --git a/src/main/kubectl.ts b/src/main/kubectl.ts index fbfd044dab..960e71e8cf 100644 --- a/src/main/kubectl.ts +++ b/src/main/kubectl.ts @@ -21,17 +21,15 @@ import path from "path"; import fs from "fs"; -import { promiseExec } from "./promise-exec"; import logger from "./logger"; import { ensureDir, pathExists } from "fs-extra"; import * as lockFile from "proper-lockfile"; import { helmCli } from "./helm/helm-cli"; import { UserStore } from "../common/user-store"; import { customRequest } from "../common/request"; -import { getBundledKubectlVersion } from "../common/utils/app-version"; import { isDevelopment, isWindows, isTestEnv } from "../common/vars"; import { SemVer } from "semver"; -import { getPath } from "../common/utils/getPath"; +import { getPath, promiseExec, getBundledKubectlVersion } from "../common/utils"; const bundledVersion = getBundledKubectlVersion(); const kubectlMap: Map = new Map([ diff --git a/src/renderer/components/+extensions/extensions.tsx b/src/renderer/components/+extensions/extensions.tsx index eb5bf4edec..97c9aa9e82 100644 --- a/src/renderer/components/+extensions/extensions.tsx +++ b/src/renderer/components/+extensions/extensions.tsx @@ -31,7 +31,7 @@ import path from "path"; import React from "react"; import { SemVer } from "semver"; import URLParse from "url-parse"; -import { Disposer, disposer, downloadFile, downloadJson, ExtendableDisposer, extractTar, listTarEntries, noop, readFileFromTar, } from "../../../common/utils"; +import { Disposer, disposer, downloadFile, downloadJson, ExtendableDisposer, extractTar, listTarEntries, noop, readFileFromTar, getPath, promiseExecFile } from "../../../common/utils"; import { ExtensionDiscovery, InstalledExtension, manifestFilename } from "../../../extensions/extension-discovery"; import { ExtensionLoader } from "../../../extensions/extension-loader"; import { extensionDisplayName, LensExtensionId, LensExtensionManifest, sanitizeExtensionName, } from "../../../extensions/lens-extension"; @@ -47,7 +47,6 @@ import { Notice } from "./notice"; import { SettingLayout } from "../layout/setting-layout"; import { docsUrl } from "../../../common/vars"; import { dialog } from "../../remote-helpers"; -import { getPath } from "../../../common/utils/getPath"; function getMessageFromError(error: any): string { if (!error || typeof error !== "object") { @@ -300,16 +299,49 @@ async function unpackExtension(request: InstallRequestValidated, disposeDownload } } +const defaultBaseRegistryUrl = "https://registry.npmjs.com"; + +async function getBaseRegistryUrl(): Promise { + try { + const filteredEnv = Object.fromEntries( + Object.entries(process.env) + .filter(([key]) => !key.startsWith("npm")) + ); + const { stdout } = await promiseExecFile("npm", ["config", "get", "registry"], { env: filteredEnv }); + + return stdout.trim(); + } catch (error) { + console.warn("[EXTENSIONS]: failed to get configured registry from .npmrc", error); + + return defaultBaseRegistryUrl; + } +} + export async function attemptInstallByInfo({ name, version, requireConfirmation = false }: ExtensionInfo) { const disposer = ExtensionInstallationStateStore.startPreInstall(); - const registryUrl = new URLParse("https://registry.npmjs.com").set("pathname", name).toString(); - const { promise } = downloadJson({ url: registryUrl }); - const json = await promise.catch(console.error); + const baseRegistryUrl = await getBaseRegistryUrl(); + const registryUrl = new URLParse(baseRegistryUrl).set("pathname", name).toString(); + let json: any; - if (!json || json.error || typeof json.versions !== "object" || !json.versions) { - const message = json?.error ? `: ${json.error}` : ""; + try { + json = await downloadJson({ url: registryUrl }).promise; - Notifications.error(`Failed to get registry information for that extension${message}`); + if (!json || json.error || typeof json.versions !== "object" || !json.versions) { + const message = json?.error ? `: ${json.error}` : ""; + + Notifications.error(`Failed to get registry information for that extension${message}`); + + return disposer(); + } + } catch (error) { + if (error instanceof SyntaxError) { + // assume invalid JSON + console.warn("Set registry has invalid json", { url: baseRegistryUrl }, error); + Notifications.error("Failed to get valid registry information for that extension. Registry did not return valid JSON"); + } else { + console.error("Failed to download registry information", error); + Notifications.error(`Failed to get valid registry information for that extension. ${error}`); + } return disposer(); }