UNPKG

@zowe/imperative

Version:
139 lines 6.76 kB
"use strict"; /* * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * * Copyright Contributors to the Zowe Project. * */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const ConfigurationLoader_1 = require("../../../ConfigurationLoader"); const security_1 = require("../../../../../security"); const logger_1 = require("../../../../../logger/"); const PluginManagementFacility_1 = require("../../PluginManagementFacility"); const PMFConstants_1 = require("../../utilities/PMFConstants"); const npm_interface_1 = require("../../utilities/npm-interface"); const NpmFunctions_1 = require("../../utilities/NpmFunctions"); const error_1 = require("../../../../../error"); const utilities_1 = require("../../../../../utilities"); /** * The uninstall command handler for cli plugin install. * * @see {uninstallDefinition} */ class UninstallHandler { constructor() { /** * A logger for this class * * @private * @type {Logger} */ this.console = logger_1.Logger.getImperativeLogger(); } /** * Process the command and input. * * @param {IHandlerParameters} params Parameters supplied by yargs * * @param {string[]} [params.arguments.plugin] This is the plugin to uninstall. * * @returns {Promise<ICommandResponse>} The command response * * @throws {ImperativeError} */ process(params) { return __awaiter(this, void 0, void 0, function* () { const chalk = utilities_1.TextUtils.chalk; this.console.debug(`Root Directory: ${PMFConstants_1.PMFConstants.instance.PLUGIN_INSTALL_LOCATION}`); if (params.arguments.plugin == null || params.arguments.plugin.length === 0) { throw new error_1.ImperativeError({ msg: `${chalk.yellow.bold("Package name")} is required.` }); } else { try { for (const packageName of params.arguments.plugin) { // let the plugin perform any pre-uninstall operations try { yield this.callPluginPreUninstall(packageName); } catch (err) { // We do not stop on preUninstall error. We just show a message. params.response.console.log(err.message); } (0, npm_interface_1.uninstall)(packageName); } params.response.console.log("Removal of the npm package(s) was successful.\n"); } catch (e) { throw new error_1.ImperativeError({ msg: "Uninstall Failed", causeErrors: [e], additionalDetails: e.message }); } } }); } /** * Call a plugin's lifecycle hook to enable a plugin to take some action * before the plugin is uninstalled. * * @param pluginPackageNm The package name of the plugin being installed. * * @throws ImperativeError. */ callPluginPreUninstall(pluginPackageNm) { return __awaiter(this, void 0, void 0, function* () { const impLogger = logger_1.Logger.getImperativeLogger(); try { // get the plugin's Imperative config definition const packageInfo = yield (0, NpmFunctions_1.getPackageInfo)(pluginPackageNm); const requirerFunction = PluginManagementFacility_1.PluginManagementFacility.instance.requirePluginModuleCallback(pluginPackageNm); const pluginImpConfig = ConfigurationLoader_1.ConfigurationLoader.load(null, packageInfo, requirerFunction); if ((pluginImpConfig === null || pluginImpConfig === void 0 ? void 0 : pluginImpConfig.pluginLifeCycle) === undefined) { // the pluginLifeCycle was not defined by the plugin const credMgrInfo = security_1.CredentialManagerOverride.getCredMgrInfoByPlugin(pluginPackageNm); if (credMgrInfo !== null) { // this plugin is a known cred mgr override security_1.CredentialManagerOverride.recordDefaultCredMgrInConfig(credMgrInfo.credMgrDisplayName); throw new error_1.ImperativeError({ msg: `The plugin '${pluginPackageNm}', which overrides the CLI ` + `Credential Manager, does not implement the 'pluginLifeCycle' class. ` + `The CLI default Credential Manager ` + `(${security_1.CredentialManagerOverride.DEFAULT_CRED_MGR_NAME}) was automatically reinstated.` }); } return; } // call the plugin's preUninstall operation impLogger.debug(`Calling the preUninstall function for plugin '${pluginPackageNm}'`); const requirerFun = PluginManagementFacility_1.PluginManagementFacility.instance.requirePluginModuleCallback(pluginPackageNm); const lifeCycleClass = requirerFun(pluginImpConfig.pluginLifeCycle); const lifeCycleInstance = new lifeCycleClass(); yield lifeCycleInstance.preUninstall(); } catch (err) { throw new error_1.ImperativeError({ msg: `Unable to perform the 'preUninstall' action of plugin '${pluginPackageNm}'` + `\nReason: ${err.message}` }); } }); } } exports.default = UninstallHandler; //# sourceMappingURL=uninstall.handler.js.map