/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import type { Disposer } from "../../../../common/utils"; import { Notifications } from "../../notifications"; import { Button } from "../../button"; import type { LensExtensionId } from "../../../../extensions/lens-extension"; import React from "react"; import { shell } from "electron"; import type { InstallRequestValidated } from "./create-temp-files-and-validate/create-temp-files-and-validate"; import type { InstallRequest } from "./install-request"; import { InstallationState } from "../../../../extensions/installation-state/state"; import type { InstalledExtension } from "../../../../extensions/extension-discovery/extension-discovery"; interface Dependencies { getInstalledExtension: (extId: string) => InstalledExtension | undefined; uninstallExtension: (id: LensExtensionId) => Promise; unpackExtension: (request: InstallRequestValidated, disposeDownloading: Disposer) => Promise; createTempFilesAndValidate: (installRequest: InstallRequest) => Promise; getExtensionDestFolder: (name: string) => string; getInstallationState: (extId: string) => InstallationState; removeDir: (dir: string) => Promise; } export const attemptInstall = ({ getInstalledExtension, uninstallExtension, unpackExtension, createTempFilesAndValidate, getExtensionDestFolder, getInstallationState, removeDir, }: Dependencies) => async (request: InstallRequest, dispose: Disposer): Promise => { const validatedRequest = await createTempFilesAndValidate(request); if (!validatedRequest) { return dispose(); } const { id, manifest: { name, version, description }, } = validatedRequest; const curState = getInstallationState(id); if (curState !== InstallationState.IDLE) { dispose(); return void Notifications.error(
Extension Install Collision:

The {name} extension is currently {curState.toLowerCase()}.

Will not proceed with this current install request.

, ); } const extensionFolder = getExtensionDestFolder(name); const installedExtension = getInstalledExtension(id); if (installedExtension) { const { manifest: { version: oldVersion }} = installedExtension; // confirm uninstall and then install new version const removeNotification = Notifications.info(

Install extension{name}@{version}?

Description: {description}

shell.openPath(extensionFolder)} > Warning: {name}@{oldVersion} will be removed before installation.
, { onClose: dispose, }, ); } else { // Remove the old dir because it isn't a valid extension anyway await removeDir(extensionFolder); await unpackExtension(validatedRequest, dispose); } };