This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
YargsConfigurer.ts
312 lines (280 loc) · 14.5 KB
/
YargsConfigurer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* 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.
*
*/
import { format, inspect, isNullOrUndefined } from "util";
import { Arguments, Argv } from "yargs";
import { Logger } from "../../../logger";
import { Constants } from "../../../constants";
import { AbstractCommandYargs } from "./AbstractCommandYargs";
import { ICommandDefinition } from "../doc/ICommandDefinition";
import { ICommandResponseParms } from "../doc/response/parms/ICommandResponseParms";
import { CommandProcessor } from "../CommandProcessor";
import { CommandUtils } from "../utils/CommandUtils";
import { IProfileManagerFactory } from "../../../profiles";
import { ICommandProfileTypeConfiguration } from "../doc/profiles/definition/ICommandProfileTypeConfiguration";
import { IHelpGeneratorFactory } from "../help/doc/IHelpGeneratorFactory";
import { CliUtils } from "../../../utilities/src/CliUtils";
/**
* Before invoking commands, this class configures some settings and callbacks in Yargs,
* including what happens on syntax failures.
*/
export class YargsConfigurer {
constructor(private rootCommand: ICommandDefinition,
private yargs: any,
private commandRespParms: ICommandResponseParms,
private profileManagerFactory: IProfileManagerFactory<ICommandProfileTypeConfiguration>,
private helpGeneratorFactory: IHelpGeneratorFactory,
private experimentalCommandDescription: string,
private rootCommandName: string,
private commandLine: string,
private envVariablePrefix: string,
private promptPhrase: string
) {
}
public configure() {
/**
* Add the command definitions to yargs
*/
const jsonResponseFormat =
(process.argv.indexOf(CliUtils.getDashFormOfOption(Constants.JSON_OPTION)) >= 0 ||
process.argv.indexOf(CliUtils.getDashFormOfOption(Constants.JSON_OPTION_ALIAS)) >= 0);
const logger = Logger.getImperativeLogger();
const jsonArg: any = {};
if (jsonResponseFormat) {
const jsonOptionName: string = Constants.JSON_OPTION;
jsonArg[jsonOptionName] = true;
}
const preferredTerminalWidth = 100;
const failedCommandHandler = __dirname + "/../handlers/FailedCommandHandler";
const failedCommandDefinition: ICommandDefinition = {
name: this.rootCommandName + " " + this.commandLine,
handler: failedCommandHandler,
type: "command",
description: "The command you tried to invoke failed"
};
this.yargs.help(false);
this.yargs.version(false);
this.yargs.showHelpOnFail(false);
// finally, catch any undefined commands
this.yargs.command({
command: "*",
description: "Unknown group",
handler: (argv: any) => {
const attemptedCommand = argv._.join(" ");
if (attemptedCommand.trim().length === 0) {
if (argv.V) {
argv.version = true;
}
const isJson = argv[Constants.JSON_OPTION] || argv[Constants.JSON_OPTION_ALIAS];
// Allocate a help generator from the factory
const rootHelpGenerator = this.helpGeneratorFactory.getHelpGenerator({
commandDefinition: this.rootCommand,
fullCommandTree: this.rootCommand,
experimentalCommandsDescription: this.experimentalCommandDescription
});
new CommandProcessor({
definition: this.rootCommand, fullDefinition: this.rootCommand,
helpGenerator: rootHelpGenerator,
profileManagerFactory: this.profileManagerFactory,
rootCommandName: this.rootCommandName,
commandLine: this.commandLine,
envVariablePrefix: this.envVariablePrefix,
promptPhrase: this.promptPhrase
}).invoke({ arguments: argv, silent: false, responseFormat: (jsonResponseFormat) ? "json" : "default" })
.then((response) => {
Logger.getImperativeLogger().debug("Root help complete.");
})
.catch((rejected) => {
process.stderr.write("Internal Imperative Error: Root command help error occurred: "
+ rejected.message + "\n");
Logger.getImperativeLogger().error(`Root unexpected help error: ${inspect(rejected)}`);
});
} else {
// unknown command, not successful
process.exitCode = Constants.ERROR_EXIT_CODE;
const lev = require("levenshtein");
let minimumLevDistance: number = 999999;
let closestCommand: string;
const commandTree = CommandUtils.flattenCommandTree(this.rootCommand);
for (const command of commandTree) {
if (command.fullName.trim().length === 0) {
continue;
}
const compare = new lev(attemptedCommand, command.fullName);
if (compare.distance < minimumLevDistance) {
minimumLevDistance = compare.distance;
closestCommand = command.fullName;
}
}
argv.failureMessage = this.buildFailureMessage(closestCommand);
// Allocate a help generator from the factory
const rootHelpGenerator = this.helpGeneratorFactory.getHelpGenerator({
commandDefinition: failedCommandDefinition,
fullCommandTree: failedCommandDefinition,
experimentalCommandsDescription: this.experimentalCommandDescription
});
// Create the command processor for the fail command
const failCommand = new CommandProcessor({
definition: failedCommandDefinition,
fullDefinition: failedCommandDefinition,
helpGenerator: rootHelpGenerator,
profileManagerFactory: this.profileManagerFactory,
rootCommandName: this.rootCommandName,
commandLine: this.commandLine,
envVariablePrefix: this.envVariablePrefix,
promptPhrase: this.promptPhrase
});
// Invoke the fail command
failCommand.invoke({ arguments: argv, silent: false, responseFormat: (jsonResponseFormat) ? "json" : "default" })
.then((failedCommandResponse) => {
logger.debug("Finished invoking the 'FailedCommand' handler");
}).catch((err) => {
logger.error("%s", err.msg);
});
}
}
});
this.yargs.fail((msg: string, error: Error, failedYargs: any) => {
process.exitCode = Constants.ERROR_EXIT_CODE;
AbstractCommandYargs.STOP_YARGS = true; // todo: figure out a better way
error = error || new Error(msg);
// Allocate a help generator from the factory
const failHelpGenerator = this.helpGeneratorFactory.getHelpGenerator({
commandDefinition: failedCommandDefinition,
fullCommandTree: failedCommandDefinition,
experimentalCommandsDescription: this.experimentalCommandDescription
});
// Create the command processor for the fail command
const failCommand = new CommandProcessor({
definition: failedCommandDefinition,
fullDefinition: failedCommandDefinition,
helpGenerator: failHelpGenerator,
profileManagerFactory: this.profileManagerFactory,
rootCommandName: this.rootCommandName,
commandLine: this.commandLine,
envVariablePrefix: this.envVariablePrefix,
promptPhrase: this.promptPhrase
});
const failureMessage = this.buildFailureMessage();
// Construct the fail command arguments
const argv: Arguments = {
failureMessage,
error,
_: [],
$0: Constants.PRIMARY_COMMAND
};
// Invoke the fail command
failCommand.invoke({ arguments: argv, silent: false, responseFormat: (jsonResponseFormat) ? "json" : "default" })
.then((failedCommandResponse) => {
logger.debug("Finished invoking the 'FailedCommand' handler");
}).catch((err) => {
logger.error("%s", err.msg);
});
});
process.on("uncaughtException", (error: Error) => {
process.exitCode = Constants.ERROR_EXIT_CODE;
// Allocate a help generator from the factory
const failHelpGenerator = this.helpGeneratorFactory.getHelpGenerator({
commandDefinition: failedCommandDefinition,
fullCommandTree: failedCommandDefinition,
experimentalCommandsDescription: this.experimentalCommandDescription
});
// Create the command processor for failure
let failureMessage = "Imperative encountered an unexpected exception";
const failCommand = new CommandProcessor({
definition: failedCommandDefinition,
fullDefinition: failedCommandDefinition,
helpGenerator: failHelpGenerator,
profileManagerFactory: this.profileManagerFactory,
rootCommandName: this.rootCommandName,
commandLine: this.commandLine,
envVariablePrefix: this.envVariablePrefix,
promptPhrase: this.promptPhrase
});
failureMessage += `\nCommand entered: "${this.rootCommandName} ${this.commandLine}"`;
const groupValues = this.commandLine.split(" ", 2);
failureMessage += `\nUse "${this.rootCommandName} ${groupValues[0]} ${groupValues[1]} --help" to view groups, commands, and options.`;
// Construct the arguments
const argv: Arguments = {
failureMessage,
error,
_: [],
$0: Constants.PRIMARY_COMMAND
};
// Invoke the fail command processor
failCommand.invoke({ arguments: argv, silent: false, responseFormat: (jsonResponseFormat) ? "json" : "default" })
.then((failedCommandResponse) => {
logger.debug("Finished invoking the 'FailedCommand' handler");
}).catch((err) => {
logger.error("%s", err.msg);
});
});
}
/**
* Builds the failure message that is passed to the failedCommand handler
* @return {string} - Returns the failure message
*/
private buildFailureMessage(closestCommand?: string) {
const three: number = 3;
let commands: string = "";
let groups: string = " "; // default to " " for proper spacing in message
let delimiter: string = ""; // used to delimit between possible 'command' values
let failureMessage = "Command failed due to improper syntax";
failureMessage += `\nCommand entered: "${this.rootCommandName} ${this.commandLine}"`;
// limit to three to include two levels of group and command value, if present
const groupValues = this.commandLine.split(" ", three);
// loop through the top level groups
for (const group of this.rootCommand.children) {
if ((group.name.trim() === groupValues[0]) || (group.aliases[0] === groupValues[0])) {
groups += groupValues[0] + " ";
// found the top level group so loop to see if second level group valid
for (const group2 of group.children) {
if ((group2.name.trim() === groupValues[1]) || (group2.aliases[0] === groupValues[1])) {
groups += groupValues[1] + " ";
// second level group valid so command provided is invalid, retrieve the valid command(s)
for (let i = 0; i < group2.children.length; i++) {
if (i > 0) {
delimiter = ", ";
}
commands += delimiter + group2.children[i].name;
}
break;
}
}
break;
}
}
if (!isNullOrUndefined(closestCommand)) {
failureMessage += format("\nUnknown group: %s\n", groupValues[0]);
failureMessage += format("Did you mean: %s?", closestCommand);
}
if (commands.length > 0) {
failureMessage += `\nAvailable commands are "${commands}".`;
}
failureMessage += `\nUse "${this.rootCommandName}${groups}--help" to view groups, commands, and options.`;
return failureMessage;
}
// /**
// * Constructs the response object for invoking help and error command handlers.
// * @param {boolean} silent - Enable silent mode
// * @param {boolean} printJsonResponse - Print a JSON response if requested.
// * @return {CommandResponse} - Returns the constructed command response object
// */
// private buildResponseObject(silent = false, printJsonResponse = false): CommandResponse {
// return new CommandResponse({
// log: this.commandRespParms.log,
// silent: silent,
// printJsonOnResponse: printJsonResponse,
// primaryTextColor: this.commandRespParms.primaryTextColor,
// progressBarSpinner: this.commandRespParms.progressBarSpinner,
// progressBarPollFrequency: this.commandRespParms.progressBarPollFrequency
// });
// }
}