UNPKG

@zowe/imperative

Version:
319 lines 12.7 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. * */ Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractCommandYargs = void 0; const lodashDeep = require("lodash-deep"); const logger_1 = require("../../../logger"); const CommandProcessor_1 = require("../CommandProcessor"); const constants_1 = require("../../../constants"); const CommandResponse_1 = require("../response/CommandResponse"); const ImperativeConfig_1 = require("../../../utilities/src/ImperativeConfig"); /** * Abstract Yargs Zowe Command - Contains base methods for defining commands and groups */ class AbstractCommandYargs { /** * Construct the yargs command instance for imperative. Provides the ability to define Imperative commands to Yargs. * @param {IYargsParms} yargsParms - Parameter object contains parms for Imperative/Yargs and command response objects */ constructor(yargsParms) { this.yargsParms = yargsParms; this.log = logger_1.Logger.getImperativeLogger(); this.mYargsInstance = yargsParms.yargsInstance; this.mDefinition = yargsParms.commandDefinition; this.mParent = yargsParms.yargsParent; this.mCommandResponseParms = yargsParms.commandResponseParms; this.mHelpGeneratorFactory = yargsParms.helpGeneratorFactory; this.mRootCommandName = yargsParms.rootCommandName; this.mCommandLine = yargsParms.commandLine; this.mEnvVariablePrefix = yargsParms.envVariablePrefix; this.mPromptPhrase = yargsParms.promptPhrase; } /** * Accessor for the root command name for the CLI * @readonly * @protected * @type {string} * @memberof AbstractCommandYargs */ get rootCommandName() { return this.mRootCommandName; } /** * Accessor for the command line * @readonly * @type {string} * @memberof AbstractCommandYargs */ get commandLine() { return this.mCommandLine; } /** * Accessor for the Environmental variable prefix * @readonly * @protected * @type {string} * @memberof AbstractCommandYargs */ get envVariablePrefix() { return this.mEnvVariablePrefix; } /** * Accessor for the CLI prompt phrase * @readonly * @type {string} * @memberof AbstractCommandYargs */ get promptPhrase() { return this.mPromptPhrase; } /** * Accessor for the command response parms (for subclasses) * @return {ICommandResponseParms} - Command response object */ get responseParms() { return this.mCommandResponseParms; } /** * Accessor for the help generator factory. * @readonly * @protected * @type {HelpGeneratorFactory} * @memberof AbstractCommandYargs */ get helpGeneratorFactory() { return this.mHelpGeneratorFactory; } /** * Returns a copy of the definition. * @return {ICommandDefinition}: A copy of the definition. */ get definition() { return JSON.parse(JSON.stringify(this.mDefinition)); } /** * Returns the Yargs instance. * @return {yargs.Argv}: The Yargs instance. */ get yargs() { return this.mYargsInstance; } /** * Get the array of parents. * @return {GroupCommandYargs[]}: The array of parents. */ get parents() { let parents = []; if (this.mParent) { parents = parents.concat(this.mParent.parents); parents.push(this.mParent); } return parents; } /** * Construct the Zowe command definition "tree" - the full definition document including all parents. * @return {ICommandDefinition}: The command definition "tree". */ constructDefinitionTree() { const parents = this.parents; return parents[0] ? JSON.parse(JSON.stringify(parents[0].definition)) : {}; } /** * Build The Zowe Yargs response for the callback. Includes the Zowe command response and status info. * @param {boolean} successful: True if the command succeeded * @param {string} responseMessage: Response message for display purposes. * @param {ImperativeYargsCommandAction} action * @param {ICommandResponse[]} responses * @return {IYargsResponse} */ getZoweYargsResponse(successful, responseMessage, action, responses) { let exitCode; if (responses != null && responses.length > 0) { for (const response of responses) { // use the maximum exit code from all command responses if (exitCode == null || response.exitCode != null && response.exitCode > exitCode) { exitCode = response.exitCode; } } } return { success: successful, message: responseMessage, exitCode, actionPerformed: action, commandResponses: responses || [] }; } /** * Execute the help Command for the definition. * @param {Arguments} args: The arguments passed by the user - used for -y. * @param {YargsCommandCompleted} commandExecuted: The callback when help is complete. */ executeHelp(args, commandExecuted) { var _a; /** * Allocate the command processor and command response object to execute the help. The command response * object is recreated/changed based on the currently specified CLI options */ let tempDefinition; if (args[constants_1.Constants.HELP_EXAMPLES] && this.definition.children.length > 0) { tempDefinition = this.getDepthExamples(); } const newHelpGenerator = this.helpGeneratorFactory.getHelpGenerator({ commandDefinition: tempDefinition ? tempDefinition : this.definition, fullCommandTree: this.constructDefinitionTree(), experimentalCommandsDescription: this.yargsParms.experimentalCommandDescription }); let invoked = false; let response; try { response = new CommandProcessor_1.CommandProcessor({ definition: tempDefinition ? tempDefinition : this.definition, fullDefinition: tempDefinition ? tempDefinition : this.constructDefinitionTree(), helpGenerator: newHelpGenerator, rootCommandName: this.rootCommandName, commandLine: this.commandLine, envVariablePrefix: this.envVariablePrefix, promptPhrase: this.promptPhrase, daemonContext: ImperativeConfig_1.ImperativeConfig.instance.daemonContext }).help(new CommandResponse_1.CommandResponse({ silent: false, responseFormat: args[constants_1.Constants.JSON_OPTION] || false ? "json" : "default", stream: (_a = ImperativeConfig_1.ImperativeConfig.instance.daemonContext) === null || _a === void 0 ? void 0 : _a.stream })); } catch (helpErr) { const errorResponse = this.getZoweYargsResponse(false, `The help for ${this.definition.name} was invoked and failed.`, "help invoked"); errorResponse.causeErrors = helpErr; invoked = true; commandExecuted(args, errorResponse); } if (!invoked) { commandExecuted(args, this.getZoweYargsResponse(true, `The help for ${this.definition.name} was invoked.`, "help invoked", [response])); } } /** * Get examples for all commands of a group * @returns {ICommandDefinition} */ getDepthExamples() { let pathToArr; let completeName; let topLevelName; let tempDesc; let tempOp; let tempPre; let tempDescPath; let tempOpPath; let tempPrePath; const commandDefinition = this.definition; if (!this.definition.examples) { commandDefinition.examples = []; } lodashDeep.deepMapValues(this.definition.children, (value, path) => { if (path.endsWith("name") && (path.includes("options") || path.includes("positionals"))) { /* Do nothing */ } else if (path.endsWith("name") && path.includes("children")) { completeName = `${topLevelName} ${value}`; } else if (path.endsWith("name")) { topLevelName = value; } if (path.includes("examples.0.")) { const tmp = path.split("examples.0."); pathToArr = `${tmp[0]}examples`; } if (path.includes(pathToArr)) { if (path.endsWith("description")) { tempDescPath = path.substring(0, path.length - "description".length); tempDesc = value; } else if (path.endsWith("options")) { tempOpPath = path.substring(0, path.length - "options".length); if (completeName) { tempOp = `${completeName} ${value}`; } else if (topLevelName) { tempOp = `${topLevelName} ${value}`; } else { tempOp = value; } } else if (path.endsWith("prefix")) { tempPrePath = path.substring(0, path.length - "prefix".length); tempPre = value; } else { tempPrePath = undefined; } if (tempDescPath === tempOpPath) { let commandExamples; tempPre && tempDescPath === tempPrePath ? commandDefinition.examples[commandDefinition.examples.length - 1].prefix = tempPre : commandExamples = { description: tempDesc, options: tempOp }; if (commandExamples) { commandDefinition.examples.push(commandExamples); } } } }); return commandDefinition; } /** * Execute the web help Command for the definition. * @param {Arguments} args: The arguments passed by the user - used for -y. * @param {YargsCommandCompleted} commandExecuted: The callback when help is complete. */ executeWebHelp(args, commandExecuted) { var _a; let fullCommandName = this.rootCommandName; for (const parent of this.parents) { fullCommandName += "_" + parent.definition.name; } let response; let invoked = false; try { response = new CommandProcessor_1.CommandProcessor({ definition: this.definition, fullDefinition: this.constructDefinitionTree(), helpGenerator: "fake", rootCommandName: this.rootCommandName, commandLine: this.commandLine, envVariablePrefix: this.envVariablePrefix, promptPhrase: this.promptPhrase, daemonContext: ImperativeConfig_1.ImperativeConfig.instance.daemonContext }).webHelp(fullCommandName + "_" + this.definition.name, new CommandResponse_1.CommandResponse({ silent: false, responseFormat: args[constants_1.Constants.JSON_OPTION] || false ? "json" : "default", stream: (_a = ImperativeConfig_1.ImperativeConfig.instance.daemonContext) === null || _a === void 0 ? void 0 : _a.stream })); } catch (helpErr) { const errorResponse = this.getZoweYargsResponse(false, `The web help for ${this.definition.name} was invoked and failed.`, "help invoked"); errorResponse.causeErrors = helpErr; invoked = true; commandExecuted(args, errorResponse); } if (!invoked) { commandExecuted(args, this.getZoweYargsResponse(true, `The web help for ${this.definition.name} was invoked.`, "help invoked", [response])); } } } exports.AbstractCommandYargs = AbstractCommandYargs; /** * TODO: REMOVE THIS, workaround for yargs.fail() problem * @type {boolean} */ AbstractCommandYargs.STOP_YARGS = false; //# sourceMappingURL=AbstractCommandYargs.js.map