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
/
DefaultRootCommandHandler.ts
55 lines (53 loc) · 2.68 KB
/
DefaultRootCommandHandler.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
/*
* 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 { Imperative } from "../../../imperative/src/Imperative";
import { ICommandHandler, IHandlerParameters, ICommandTreeEntry, CommandUtils } from "../../../cmd";
import { ImperativeConfig, TextUtils } from "../../../utilities";
import { WebHelpManager } from "../../../cmd/src/help/WebHelpManager";
/**
* The default command handler for the top level/root command
* Allows the user to check the version of the package.
* If they haven't specified --version, the help prints
*/
export default class DefaultRootCommandHandler implements ICommandHandler {
public async process(params: IHandlerParameters) {
// if --version is specified
if (params.arguments.version) {
// load the user's package.json to check the version of their package
const packageJson: any = ImperativeConfig.instance.callerPackageJson;
const versionString =
params.response.console.log(packageJson.version);
params.response.data.setObj({ version: versionString });
params.response.data.setMessage("Version displayed");
} else if(params.arguments.availableCommands) {
// Gather and display the full set of commands available to the CLI with descriptions
const cmdList: ICommandTreeEntry[] = CommandUtils.flattenCommandTree(Imperative.fullCommandTree);
cmdList.forEach((cmd: ICommandTreeEntry) => {
if (cmd.command.type === "command") {
params.response.console.log(Imperative.highlightWithPrimaryColor(`${Imperative.rootCommandName.trim()} ${cmd.fullName.trim()}`));
params.response.console.log("");
params.response.console.log(TextUtils.wordWrap(cmd.command.description,
TextUtils.DEFAULT_WRAP_WIDTH,
"\t"));
params.response.console.log("");
}
});
} else if (params.arguments.helpWeb) {
WebHelpManager.instance.openRootHelp(params.response);
} else {
params.response.console.log(Buffer.from(Imperative.getHelpGenerator({
commandDefinition: params.definition,
fullCommandTree: params.fullDefinition,
experimentalCommandsDescription: ImperativeConfig.instance.loadedConfig.experimentalCommandDescription
}).buildHelp()));
}
}
}