vtex
Version:
The platform for e-commerce apps
116 lines (115 loc) • 5.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderCommands = exports.buildCommandGroups = exports.collectCommands = exports.isHelpInvocation = exports.getHelpSubject = void 0;
const tslib_1 = require("tslib");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const indent_string_1 = tslib_1.__importDefault(require("indent-string"));
const Colors_1 = require("../../api/constants/Colors");
const list_1 = require("@oclif/plugin-help/lib/list");
const root_1 = tslib_1.__importDefault(require("@oclif/plugin-help/lib/root"));
const constants_1 = require("./constants");
function getHelpSubject(args) {
for (const arg of args) {
if (arg === '--' || arg.startsWith('-'))
return;
if (['help', '--help', '-h'].includes(arg))
continue;
return arg;
}
}
exports.getHelpSubject = getHelpSubject;
/**
* Detects whether the current invocation is a help request, covering every
* help form: `vtex help`, `vtex help <command>`, `vtex --help` / `-h` (oclif
* passes the bare flag as the command id itself) and `vtex <command> --help` /
* `-h` (`-h` is globally reserved for help in CustomCommand).
*
* Note on the `--` separator: everything after `--` is forwarded verbatim to
* the underlying command as positional values (consistent with
* `getHelpSubject` above), so `vtex cmd -- --help` must NOT be treated as a
* help invocation. Only flags appearing before `--` are considered.
*/
function isHelpInvocation(commandId, argv) {
if (commandId != null && ['help', '--help', '-h'].includes(commandId))
return true;
for (const arg of argv) {
if (arg === '--')
break;
if (arg === '--help' || arg === '-h')
return true;
}
return false;
}
exports.isHelpInvocation = isHelpInvocation;
/**
* Flattens the oclif config's commands and topics into the flat `CommandI`
* list used by the help output, dropping namespaced entries (those containing
* a `:`) so only top-level commands/topics are shown.
*/
function collectCommands(commands, topics) {
const mappedCommands = commands
.filter(c => !c.id.includes(':'))
.map(c => ({ name: c.id, description: c.description }));
const mappedTopics = topics
.filter(t => !t.name.includes(':'))
.map(t => ({ name: t.name, description: t.description }));
return mappedCommands.concat(mappedTopics);
}
exports.collectCommands = collectCommands;
/**
* Buckets the CLI's commands/topics into display groups for the help output.
*
* The group metadata (`commandsGroup` maps command name -> group id, and
* `commandsId` maps group id -> group label) comes from a feature-flag store
* that is populated by a non-blocking child process. On a fresh install those
* values may still be undefined, so we fall back to rendering every command
* under a single default ("Other") group so that help always works.
*
* Commands with no known group id (or a falsy one) are placed in the last
* group. Duplicate command names are rendered only once.
*/
function buildCommandGroups(commandsGroup, commandsId, allCommands) {
const resolvedCommandsGroup = commandsGroup !== null && commandsGroup !== void 0 ? commandsGroup : {};
const resolvedCommandsId = commandsId !== null && commandsId !== void 0 ? commandsId : { [constants_1.OTHER_GROUP_ID]: 'Other' };
const commandsGroupLength = Object.keys(resolvedCommandsId).length;
const groups = Object.keys(resolvedCommandsId).map(() => []);
const seen = new Set();
allCommands.forEach((command) => {
if (seen.has(command.name))
return;
seen.add(command.name);
const commandGroupId = resolvedCommandsGroup[command.name];
if (commandGroupId) {
groups[commandGroupId].push(command);
}
else {
groups[commandsGroupLength - 1].push(command);
}
});
return { commandsId: resolvedCommandsId, groups };
}
exports.buildCommandGroups = buildCommandGroups;
function renderCommand(commands, ctx) {
return (0, list_1.renderList)(commands.map(c => [chalk_1.default.hex(Colors_1.COLORS.PINK)(c.name), c.description && ctx.render(c.description.split('\n')[0])]), {
spacer: '\n',
stripAnsi: ctx.opts.stripAnsi,
maxWidth: ctx.opts.maxWidth - 2,
});
}
function renderCommands(commandsId, groups, ctx) {
const body = [];
const commandsGroupLength = Object.keys(commandsId).length;
const help = new root_1.default(ctx.config, ctx.opts);
body.push(help.root());
body.push(' ');
for (let [key, value] of Object.entries(commandsId)) {
key = key !== constants_1.OTHER_GROUP_ID ? key : (commandsGroupLength - 1).toString();
if (groups[key].length > 0) {
body.push(chalk_1.default.bold(value));
body.push((0, indent_string_1.default)(renderCommand(groups[key], ctx), 2));
body.push(' ');
}
}
return body.join('\n');
}
exports.renderCommands = renderCommands;