@zowe/imperative
Version:
framework for building configurable CLIs
95 lines • 4.76 kB
JavaScript
"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.YargsDefiner = void 0;
const util_1 = require("util");
const logger_1 = require("../../../logger");
const GroupCommandYargs_1 = require("./GroupCommandYargs");
const CommandYargs_1 = require("./CommandYargs");
/**
* Imperative Command Definer - Defines the Imperative command objects to Yargs for processing.
*/
class YargsDefiner {
/**
* Build the definer - maintains the Yargs instance for all future definitions.
* @param {yargs.Argv} yargsInstance: The Yargs instance used to define the commands.
* @param primaryHighlightColor - main color to highlight help text headings and other text with
* @param rootCommandName - the display name of the root command (e.g. "zowe" or "sample-cli")
* @param envVariablePrefix - the environment variable prefix
* @param helpGeneratorFactory - help generator factory that can be used to instantiate new help generators
* @param experimentalCommandDescription - optionally overridden experimental command description to
* propagate to yargs services
*/
constructor(yargsInstance, primaryHighlightColor, rootCommandName, commandLine, envVariablePrefix, helpGeneratorFactory, experimentalCommandDescription, promptPhrase) {
/**
* Logger instance
*/
this.log = logger_1.Logger.getImperativeLogger();
this.mYargsInstance = yargsInstance;
this.mPrimaryHighlightColor = primaryHighlightColor;
this.mRootCommandName = rootCommandName;
this.mCommandLine = commandLine;
this.mEnvVariablePrefix = envVariablePrefix;
this.mHelpFactory = helpGeneratorFactory;
this.mExperimentalCommandDescription = experimentalCommandDescription;
this.mPromptPhrase = promptPhrase;
}
/**
* Accepts an Imperative command definition document and defines to Yargs.
* @param {ICommandDefinition} definition: The Imperative JSON command definition document.
* @param {YargsCommandCompleted} commandExecuted: An "event-style" callback that is invoked upon
* completion of a command execution for this definition.
* @param {ICommandResponseParms} responseParms - The response object parameters used when invoking commands and help
*/
define(definition, commandExecuted, responseParms) {
/**
* Build the appropriate Yargs abject, depending on specified type. An error is thrown if the type is
* not recognized.
*/
this.log.trace("Defining a new definition to Yargs:");
this.log.trace((0, util_1.inspect)(definition));
switch (definition.type) {
// case "provider":
case "group":
new GroupCommandYargs_1.GroupCommandYargs({
yargsInstance: this.mYargsInstance,
commandDefinition: definition,
commandResponseParms: responseParms,
helpGeneratorFactory: this.mHelpFactory,
experimentalCommandDescription: this.mExperimentalCommandDescription,
rootCommandName: this.mRootCommandName,
commandLine: this.mCommandLine,
envVariablePrefix: this.mEnvVariablePrefix,
promptPhrase: this.mPromptPhrase
}).defineCommandToYargs(commandExecuted);
break;
case "command":
new CommandYargs_1.CommandYargs({
yargsInstance: this.mYargsInstance,
commandDefinition: definition,
commandResponseParms: responseParms,
helpGeneratorFactory: this.mHelpFactory,
experimentalCommandDescription: this.mExperimentalCommandDescription,
rootCommandName: this.mRootCommandName,
commandLine: this.mCommandLine,
envVariablePrefix: this.mEnvVariablePrefix,
promptPhrase: this.mPromptPhrase
}).defineCommandToYargs(commandExecuted);
break;
default:
throw new Error(`Imperative Yargs Definer Internal Error: Invalid command definition type: ` +
`${definition.type}`);
}
}
}
exports.YargsDefiner = YargsDefiner;
//# sourceMappingURL=YargsDefiner.js.map