UNPKG

@zowe/imperative

Version:
102 lines 5.18 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 logger_1 = require("../../../../../logger"); const PMFConstants_1 = require("../../utilities/PMFConstants"); const npm_interface_1 = require("../../utilities/npm-interface"); const error_1 = require("../../../../../error"); const utilities_1 = require("../../../../../utilities"); const jsonfile_1 = require("jsonfile"); const NpmFunctions_1 = require("../../utilities/NpmFunctions"); /** * The update command handler for cli plugin install. * * @see {updateDefinition} */ class UpdateHandler { 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 update. * * @returns {Promise<ICommandResponse>} The command response * * @throws {ImperativeError} */ process(params) { return __awaiter(this, void 0, void 0, function* () { const iConsole = logger_1.Logger.getImperativeLogger(); const chalk = utilities_1.TextUtils.chalk; let packageName; this.console.debug(`Root Directory: ${PMFConstants_1.PMFConstants.instance.PLUGIN_INSTALL_LOCATION}`); const plugin = params.arguments.plugin; const registry = NpmFunctions_1.NpmRegistryUtils.getRegistry(params.arguments.registry); if (params.arguments.plugin == null || params.arguments.plugin.length === 0) { throw new error_1.ImperativeError({ msg: `${chalk.yellow.bold("Plugin name")} is required.` }); } iConsole.debug("Reading in the current configuration."); const installedPlugins = (0, jsonfile_1.readFileSync)(PMFConstants_1.PMFConstants.instance.PLUGIN_JSON); if (params.arguments.registry != null && params.arguments.login) { NpmFunctions_1.NpmRegistryUtils.npmLogin(registry); } if (Object.prototype.hasOwnProperty.call(installedPlugins, plugin)) { // Loop through the plugins and remove the uninstalled package for (const pluginName in installedPlugins) { // Only retain the plugins that aren't being uninstalled if (plugin.toString() === pluginName.toString()) { // Retrieve the package and registry values from the plugins.json file to pass to update // as package may not match the plugin value. This is true for plugins installed by // folder location. Example: plugin 'imperative-sample-plugin' installed from ../imperative-plugins packageName = installedPlugins[pluginName].package; const registryInfo = NpmFunctions_1.NpmRegistryUtils.buildRegistryInfo(installedPlugins[pluginName], params.arguments.registry); // Call update which returns the plugin's version so plugins.json can be updated installedPlugins[pluginName].version = yield (0, npm_interface_1.update)(packageName, registryInfo); installedPlugins[pluginName].location = registryInfo.location; // update in case it changed (0, jsonfile_1.writeFileSync)(PMFConstants_1.PMFConstants.instance.PLUGIN_JSON, installedPlugins, { spaces: 2 }); params.response.console.log(`Update of the npm package(${packageName}) was successful.\n`); } } } else { throw new error_1.ImperativeError({ msg: `${chalk.yellow.bold("Plugin name '")}${chalk.red.bold(plugin)}' is not installed.` }); } }); } } exports.default = UpdateHandler; //# sourceMappingURL=update.handler.js.map