@acot/cli
Version:
More accessible web, all over the world.
127 lines (126 loc) • 4.06 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const command_1 = require("../command");
const global_options_1 = require("../global-options");
const usage_1 = require("../utils/usage");
const buildOptionName = (name, opts) => {
if (opts.alias != null) {
const aliases = typeof opts.alias === 'string' ? [opts.alias] : opts.alias;
const alias = aliases.map((s) => `-${s}`).join(', ');
return `${alias}, --${name}`;
}
return ` --${name}`;
};
const globalOptionContent = {
header: 'GLOBAL OPTIONS',
content: Object.entries(global_options_1.globalOptions).map(([name, opts]) => ({
name: buildOptionName(name, opts),
summary: opts.description,
})),
};
exports.default = (0, command_1.createCommand)({
name: 'help',
summary: 'Show help.',
args: {
command: {
type: 'string',
array: true,
},
},
options: {},
})(async ({ logger, pkg, args, container }) => {
var _a;
const command = (_a = args.command) !== null && _a !== void 0 ? _a : [];
const name = command.join('.');
// command help
if (name !== '') {
const module = container.get(name);
if (module == null) {
logger.error(`"${name}" command does not exists`);
return 1;
}
let usage = `acot ${command.join(' ')}`;
if (module.commands != null) {
usage += ' <command>';
}
const args = Object.entries(module.args);
if (args.length > 0) {
const multiple = args.length > 1;
const overview = args
.map(([arg, opts]) => {
return opts.array === true ? `<${arg}..>` : `<${arg}>`;
})
.join(' | ');
usage += ` ${multiple ? '{' : ''}${overview}${multiple ? '}' : ''}`;
}
if (Object.keys(module.options).length > 0) {
usage += ' [flags]';
}
const availableCommands = module.commands != null
? module.commands.map((cmd) => ({
name: cmd.name,
summary: cmd.summary,
}))
: [];
const cmdOptions = Object.entries(module.options).map(([key, opts]) => ({
name: buildOptionName(key, opts),
summary: opts.description,
}));
logger.print((0, usage_1.buildUsage)([
{
content: module.summary,
raw: true,
},
{
header: 'USAGE',
content: usage,
},
...(availableCommands.length > 0
? [
{
header: 'AVAILABLE COMMANDS',
content: availableCommands,
},
]
: []),
...(cmdOptions.length > 0
? [
{
header: 'COMMAND OPTIONS',
content: cmdOptions,
},
]
: []),
globalOptionContent,
]));
return 0;
}
// global help
logger.print((0, usage_1.buildUsage)([
{
content: pkg.description,
raw: true,
},
{
header: 'USAGE',
content: `acot <command> [subcommand..] [flags]`,
},
{
header: 'AVAILABLE COMMANDS',
content: container.main().map((module) => ({
name: module.name,
summary: module.summary,
})),
},
globalOptionContent,
{
content: (0, chalk_1.default) `{gray Run {bold \`acot help [command..]\`} for help with a specific command.}`,
raw: true,
},
]));
return 0;
});