Skip to content

Commit

Permalink
refactor: move help command to kernel itself
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 29, 2019
1 parent dae9a53 commit 786e1f1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
6 changes: 2 additions & 4 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

// import { printHelpFor } from '../src/utils/help'
import { BaseCommand } from '../src/BaseCommand'
import { Kernel } from '../src/Kernel'
import { args } from '../src/Decorators/args'
Expand All @@ -20,7 +19,7 @@ class Greet extends BaseCommand {
@args.string({ description: 'The name of the person you want to greet' })
public name: string

@args.number()
@args.string()
public age: number

@flags.string({ description: 'The environment to use to specialize certain commands' })
Expand Down Expand Up @@ -59,5 +58,4 @@ kernel.flag('env', (value) => {
}, { type: 'string' })

kernel.handle(process.argv.splice(2))

// printHelpFor(Greet)
kernel.printHelp(Greet)
1 change: 0 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ export { Kernel } from './src/Kernel'
export { BaseCommand } from './src/BaseCommand'
export { args } from './src/Decorators/args'
export { flags } from './src/Decorators/flags'
export { printHelp, printHelpFor } from './src/utils/help'
8 changes: 7 additions & 1 deletion src/Contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type GlobalFlagHandler = (
*/
export type CommandsGroup = {
group: string,
commands: CommandConstructorContract[],
commands: HelpCommand[],
}[]

/**
Expand Down Expand Up @@ -84,3 +84,9 @@ export type ManifestCommand = Pick<
export type ManifestNode = {
[command: string]: ManifestCommand,
}

/**
* Shape of command required for the help screen. The node is a
* neutral point between `CommandConstructor` and `ManifestCommand`.
*/
export type HelpCommand = Pick<ManifestCommand, Exclude<keyof ManifestCommand, 'commandPath'>>
14 changes: 14 additions & 0 deletions src/Kernel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as getopts from 'getopts'

import { Parser } from '../Parser'
import { validateCommand } from '../utils/validateCommand'
import { printHelp, printHelpFor } from '../utils/help'
import { CommandConstructorContract, CommandFlag, GlobalFlagHandler } from '../Contracts'

/**
Expand Down Expand Up @@ -185,4 +186,17 @@ export class Kernel {

return this.runCommand(argv.splice(1), command)
}

/**
* Print the help screen for a given command or all commands/flags
*/
public printHelp (command?: CommandConstructorContract) {
if (command) {
printHelpFor(command)
} else {
const commands = Object.keys(this.commands).map((name) => this.commands[name])
const flags = Object.keys(this.flags).map((name) => this.flags[name])
printHelp(commands, flags)
}
}
}
8 changes: 4 additions & 4 deletions src/utils/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import * as padRight from 'pad-right'
import { green, bold, yellow, dim } from 'kleur'
import { sortAndGroupCommands } from './sortAndGroupCommands'
import { CommandConstructorContract, CommandArg, CommandFlag } from '../Contracts'
import { CommandArg, CommandFlag, HelpCommand } from '../Contracts'

/**
* Wraps the command arg inside `<>` or `[]` brackets based upon if it's
Expand Down Expand Up @@ -53,7 +53,7 @@ function getArgsForDisplay (args: CommandArg[]) {
})
}

function getCommandsForDisplay (commands: CommandConstructorContract[]) {
function getCommandsForDisplay (commands: HelpCommand[]) {
return commands.map(({ commandName, description }) => {
return { displayName: commandName, description, width: commandName.length }
})
Expand All @@ -63,7 +63,7 @@ function getCommandsForDisplay (commands: CommandConstructorContract[]) {
* Prints help for all the commands by sorting them in alphabetical order
* and grouping them as per their namespace.
*/
export function printHelp (commands: CommandConstructorContract[], flags: CommandFlag[]): void {
export function printHelp (commands: HelpCommand[], flags: CommandFlag[]): void {
const flagsList = getFlagsForDisplay(flags)
const commandsList = getCommandsForDisplay(commands)

Expand Down Expand Up @@ -103,7 +103,7 @@ export function printHelp (commands: CommandConstructorContract[], flags: Comman
/**
* Prints help for a single command
*/
export function printHelpFor (command: CommandConstructorContract): void {
export function printHelpFor (command: HelpCommand): void {
if (command.description) {
console.log('')
console.log(command.description)
Expand Down
6 changes: 3 additions & 3 deletions src/utils/sortAndGroupCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* file that was distributed with this source code.
*/

import { CommandConstructorContract, CommandsGroup } from '../Contracts'
import { CommandsGroup, HelpCommand } from '../Contracts'

/**
* Loops over the commands and converts them to an array of sorted groups with
* nested commands inside them. The grouping is done using the command
* namespace seperated with `:`. Example: `make:controller`
*/
export function sortAndGroupCommands (commands: CommandConstructorContract[]): CommandsGroup {
export function sortAndGroupCommands (commands: HelpCommand[]): CommandsGroup {
/**
* Create a group of commands using it's namespace
*/
Expand All @@ -31,7 +31,7 @@ export function sortAndGroupCommands (commands: CommandConstructorContract[]): C
result[group].push(command)

return result
}, {} as { [key: string]: CommandConstructorContract[] })
}, {} as { [key: string]: HelpCommand[] })

/**
* Convert the object literal groups and it's command to an
Expand Down

0 comments on commit 786e1f1

Please sign in to comment.