|
| 1 | +import { Command, Help, HelpBase } from '@oclif/core'; |
| 2 | +import { colorize } from '@oclif/core/ux'; |
| 3 | +import chalk from 'chalk'; |
| 4 | +import stripAnsi from 'strip-ansi'; |
| 5 | + |
| 6 | +enum HelpSection { |
| 7 | + GET_STARTED = 'GETTING STARTED', |
| 8 | + CORE = 'CORE', |
| 9 | + IMPORT = 'IMPORT', |
| 10 | + CLOUD = 'CLOUD', |
| 11 | +} |
| 12 | + |
| 13 | +const HelpOrganization = { |
| 14 | + [HelpSection.GET_STARTED]: [ |
| 15 | + 'init', |
| 16 | + ], |
| 17 | + [HelpSection.CORE]: [ |
| 18 | + 'plan', |
| 19 | + 'apply', |
| 20 | + 'destroy', |
| 21 | + 'validate' |
| 22 | + ], |
| 23 | + [HelpSection.IMPORT]: [ |
| 24 | + 'import', |
| 25 | + 'refresh', |
| 26 | + ], |
| 27 | + [HelpSection.CLOUD]: [ |
| 28 | + 'login', |
| 29 | + 'logout', |
| 30 | + 'edit', |
| 31 | + 'connect', |
| 32 | + ] |
| 33 | +} |
| 34 | + |
| 35 | +export default class CustomHelp extends Help { |
| 36 | + formatCommands(commands: Command.Loadable[]): string { |
| 37 | + if (commands.length === 0) return '' |
| 38 | + |
| 39 | + const gettingStarted = this.formatSection(HelpSection.GET_STARTED, commands) |
| 40 | + const core = this.formatSection(HelpSection.CORE, commands) |
| 41 | + const importSection = this.formatSection(HelpSection.IMPORT, commands) |
| 42 | + const cloud = this.formatSection(HelpSection.CLOUD, commands) |
| 43 | + |
| 44 | + return this.section('COMMANDS', `${gettingStarted}\n\n${core}\n\n${importSection}\n\n${cloud}`) |
| 45 | + } |
| 46 | + |
| 47 | + formatSection(section: HelpSection, commands: Command.Loadable[]): string { |
| 48 | + const body = this.renderList( |
| 49 | + commands |
| 50 | + .filter((c) => HelpOrganization[section].includes(c.id)) |
| 51 | + .filter((c) => (this.opts.hideAliasesFromRoot ? !c.aliases?.includes(c.id) : true)) |
| 52 | + .map((c) => { |
| 53 | + if (this.config.topicSeparator !== ':') c.id = c.id.replaceAll(':', this.config.topicSeparator) |
| 54 | + const summary = this.summary(c) |
| 55 | + return [ |
| 56 | + colorize(this.config?.theme?.command, c.id), |
| 57 | + summary && colorize(this.config?.theme?.sectionDescription, stripAnsi(summary)), |
| 58 | + ] |
| 59 | + }), |
| 60 | + { |
| 61 | + indentation: 2, |
| 62 | + spacer: '\n', |
| 63 | + stripAnsi: this.opts.stripAnsi, |
| 64 | + }, |
| 65 | + ) |
| 66 | + |
| 67 | + return `${chalk.underline(section)}\n${body}` |
| 68 | + } |
| 69 | +} |
0 commit comments