diff --git a/src/command.ts b/src/command.ts index f7c5e042..f4c30078 100644 --- a/src/command.ts +++ b/src/command.ts @@ -8,6 +8,7 @@ import Help from '@oclif/plugin-help' import {format, inspect} from 'util' import * as flags from './flags' +import {sortBy, uniqBy} from './util' /** * An abstract class which acts as the base for each command @@ -147,9 +148,13 @@ export default abstract class Command { protected _help() { const HHelp: typeof Help = require('@oclif/plugin-help').default const help = new HHelp(this.config) - let title = this.ctor.description && help.render(this.ctor.description.trim()).split('\n')[0] - if (title) this.log(title + '\n') - this.log(help.command(Config.Command.toCached(this.ctor as any as Config.Command.Class))) + const cmd = Config.Command.toCached(this.ctor as any as Config.Command.Class) + if (!cmd.id) cmd.id = '' + let topics = this.config.topics + topics = topics.filter((t: any) => !t.hidden) + topics = sortBy(topics, (t: any) => t.name) + topics = uniqBy(topics, (t: any) => t.name) + help.showCommandHelp(cmd, this.config.topics) return this.exit(0) } diff --git a/src/util.ts b/src/util.ts index 45f7b2bf..f63ca6d3 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,3 +1,33 @@ export function compact(a: (T | undefined)[]): T[] { return a.filter((a): a is T => !!a) } + +export function uniqBy(arr: T[], fn: (cur: T) => any): T[] { + return arr.filter((a, i) => { + let aVal = fn(a) + return !arr.find((b, j) => j > i && fn(b) === aVal) + }) +} +export namespace sort { + export type Types = string | number | undefined | boolean +} + +export function sortBy(arr: T[], fn: (i: T) => sort.Types | sort.Types[]): T[] { + function compare(a: sort.Types | sort.Types[], b: sort.Types | sort.Types[]): number { + a = a === undefined ? 0 : a + b = b === undefined ? 0 : b + + if (Array.isArray(a) && Array.isArray(b)) { + if (a.length === 0 && b.length === 0) return 0 + let diff = compare(a[0], b[0]) + if (diff !== 0) return diff + return compare(a.slice(1), b.slice(1)) + } + + if (a < b) return -1 + if (a > b) return 1 + return 0 + } + + return arr.sort((a, b) => compare(fn(a), fn(b))) +} diff --git a/test/command.test.ts b/test/command.test.ts index 984e1727..d22ed68b 100644 --- a/test/command.test.ts +++ b/test/command.test.ts @@ -241,6 +241,7 @@ USAGE OPTIONS --help show CLI help + `) }) @@ -257,6 +258,7 @@ OPTIONS USAGE $ @oclif/command + `) }) })