UNPKG

@jovotech/cli

Version:

Jovo Command Line Interface (Beta)

147 lines 5.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cli_core_1 = require("@jovotech/cli-core"); const help_1 = require("@oclif/help"); const CommandHelp_1 = require("./CommandHelp"); class HelpOutput extends help_1.HelpBase { constructor() { super(...arguments); this.globalFlags = cli_core_1.PluginCommand.flags; } showHelp(argv) { try { const commands = this.config.commands.filter((command) => !command.hidden && command.id !== 'help'); const subject = this.getHelpSubject(argv); if (!subject) { // Show root help this.printDescription(); this.printVersion(); this.printUsage(); this.printCommands(commands); this.printGlobalFlags(); this.printHint(); return; } else { const command = this.config.findCommand(subject); if (command) { return this.showCommandHelp(command); } const topic = this.config.findTopic(subject); if (topic) { return this.showTopicHelp(topic); } } throw new cli_core_1.JovoCliError({ message: `jovo ${subject} is not a valid command`, hint: 'Run jovo --help to get a list of available commands', }); } catch (error) { if (!cli_core_1.isJovoCliError(error)) { error = new cli_core_1.JovoCliError({ message: error.message }); } cli_core_1.JovoCliError.print(error); process.exit(1); } } showCommandHelp(command) { if (command.description) { cli_core_1.Log.info(command.description || ''); cli_core_1.Log.spacer(); } const help = new CommandHelp_1.CommandHelp(command, this.config, this.opts); cli_core_1.Log.info(help.generate()); } showTopicHelp(topic) { this.printTopic(topic); // Collect all corresponding commands and print them const topicCommands = this.config.commands.filter((cmd) => cmd.id.startsWith(topic.name + ':')); if (topicCommands.length) { this.printCommands(topicCommands, false); } } printDescription() { const description = this.config.pjson.oclif.description || this.config.pjson.description || ''; cli_core_1.Log.info(description); cli_core_1.Log.info(cli_core_1.printSubHeadline('Read the docs: https://www.jovo.tech/docs/cli')); cli_core_1.Log.spacer(); } printVersion() { cli_core_1.Log.info('VERSION'); cli_core_1.Log.info(this.config.userAgent, { indent: 2 }); cli_core_1.Log.spacer(); } printUsage() { cli_core_1.Log.info('USAGE'); cli_core_1.Log.info('$ jovo [COMMAND]', { indent: 2 }); cli_core_1.Log.spacer(); } printCommands(commands, showTopic = true) { cli_core_1.Log.info('COMMANDS'); const commandTopics = commands .filter((cmd) => cmd.id.includes(':')) .map((cmd) => { return cmd.id.split(':')[0]; }) .filter((command, index, self) => self.indexOf(command) === index); const commandsWithoutTopics = commands.filter((cmd) => !cmd.id.includes(':') && !commandTopics.includes(cmd.id)); for (const command of commandsWithoutTopics) { cli_core_1.Log.info(`${command.id} - ${command.description}`, { indent: 2 }); cli_core_1.Log.spacer(); } for (const topic of commandTopics) { const topicCommands = commands.filter((cmd) => cmd.id.includes(topic)); if (!topicCommands.length) { continue; } if (showTopic) { cli_core_1.Log.info(cli_core_1.chalk.dim(topic.toUpperCase()), { indent: 2 }); } for (const command of topicCommands) { cli_core_1.Log.info(`${command.id} - ${command.description}`, { indent: showTopic ? 4 : 2 }); } cli_core_1.Log.spacer(); } } printGlobalFlags() { const validFlags = Object.entries(this.globalFlags) .map(([flagKey, flag]) => { flag.name = flagKey; return flag; }) .filter((flag) => !flag.hidden); cli_core_1.Log.info(CommandHelp_1.CommandHelp.prototype.flags.call(this, validFlags)); } printHint() { cli_core_1.Log.spacer(); cli_core_1.Log.info(`${cli_core_1.BULB} To get started, run the following command:`); cli_core_1.Log.info(`$ ${this.config.bin} new`, { indent: 3 }); cli_core_1.Log.spacer(); } printTopic(topic) { if (topic.description) { cli_core_1.Log.info(topic.description); cli_core_1.Log.spacer(); } cli_core_1.Log.info('USAGE'); cli_core_1.Log.info(`$ ${this.config.bin} ${topic.name}:COMMAND`, { indent: 2 }); cli_core_1.Log.spacer(); } /** * Returns the current subject, i.e. "build" for "jovo help build" * or "jovo build --help" * @param argv - Current command line arguments */ getHelpSubject(argv) { for (const arg of argv) { if (arg === 'help' || arg === '--help' || arg === '-h') continue; if (arg.startsWith('-')) return; return arg; } } } exports.default = HelpOutput; //# sourceMappingURL=RootHelp.js.map