a2r
Version:
A2R Framework
62 lines (61 loc) • 2.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printCommandUsage = void 0;
const command_line_usage_1 = __importDefault(require("command-line-usage"));
const telemetry_1 = require("@a2r/telemetry");
const write_1 = __importDefault(require("../tools/write"));
// eslint-disable-next-line import/no-cycle
const commandLine_1 = require("./commandLine");
// eslint-disable-next-line import/no-cycle
const commands_1 = require("./commands");
const printCommandUsage = (commandName) => {
const command = commands_1.commandsMap.get(commandName);
const usage = [
{
header: `a2r ${command.name}`,
content: command.description,
},
];
if (command.args.length) {
usage.push({
header: 'Command Options',
optionList: command.args.map((arg) => (Object.assign(Object.assign({}, arg), { description: arg.required
? `[Required] ${arg.description}`
: arg.description }))),
});
}
usage.push({ header: 'Global Options', optionList: commandLine_1.globalArguments });
(0, write_1.default)(`${(0, command_line_usage_1.default)(usage)}\n\n`);
};
exports.printCommandUsage = printCommandUsage;
const help = async (info) => {
const { commandName, argv } = info;
if (!commandName) {
telemetry_1.out.warn(`No command given, printing global help...`);
(0, write_1.default)(`${(0, command_line_usage_1.default)(commandLine_1.globalHelp)}\n\n`);
return;
}
const command = commands_1.commandsMap.get(commandName);
if (!command) {
telemetry_1.out.warn(`No valid command: ${commandName}. Printing global help...`);
(0, write_1.default)(`${(0, command_line_usage_1.default)(commandLine_1.globalHelp)}\n\n`);
return;
}
const [commandToShow] = argv;
if (!commands_1.commandsMap.has(commandToShow)) {
telemetry_1.out.warn(`No valid command: ${commandToShow}. Printing global help...`);
(0, write_1.default)(`${(0, command_line_usage_1.default)(commandLine_1.globalHelp)}\n\n`);
return;
}
(0, exports.printCommandUsage)(commandToShow);
};
const command = {
name: 'help',
description: 'Prints this usage guide',
run: help,
args: [],
};
exports.default = command;