From b1b989c5cb81d81b20238cb0ade54920d80fd115 Mon Sep 17 00:00:00 2001 From: Philipe Navarro Date: Thu, 11 Apr 2019 17:28:23 -0700 Subject: [PATCH 1/3] fix: use showCommandHelp to delegate topic help --- src/command.ts | 6 +++--- test/command.test.ts | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/command.ts b/src/command.ts index f7c5e042..1c20d2f0 100644 --- a/src/command.ts +++ b/src/command.ts @@ -147,9 +147,9 @@ 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 = '' + help.showCommandHelp(cmd, this.config.topics) return this.exit(0) } 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 + `) }) }) From b1c3122a804c950bace71360ec0a27679768cd68 Mon Sep 17 00:00:00 2001 From: Philipe Navarro Date: Thu, 11 Apr 2019 17:35:08 -0700 Subject: [PATCH 2/3] update --- src/command.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/command.ts b/src/command.ts index 1c20d2f0..a514da5f 100644 --- a/src/command.ts +++ b/src/command.ts @@ -5,6 +5,7 @@ import * as Config from '@oclif/config' import * as Errors from '@oclif/errors' import * as Parser from '@oclif/parser' import Help from '@oclif/plugin-help' +import {sortBy, uniqBy} from '@oclif/plugin-help/lib/util' import {format, inspect} from 'util' import * as flags from './flags' @@ -149,6 +150,10 @@ export default abstract class Command { const help = new HHelp(this.config) 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 => !t.hidden) + topics = sortBy(topics, t => t.name) + topics = uniqBy(topics, t => t.name) help.showCommandHelp(cmd, this.config.topics) return this.exit(0) } From 69b5a2c0960f0ef11b49f0d3b6d2fca588ddfbdd Mon Sep 17 00:00:00 2001 From: Philipe Navarro Date: Fri, 12 Apr 2019 08:55:21 -0700 Subject: [PATCH 3/3] add utils --- src/command.ts | 8 ++++---- src/util.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/command.ts b/src/command.ts index a514da5f..f4c30078 100644 --- a/src/command.ts +++ b/src/command.ts @@ -5,10 +5,10 @@ import * as Config from '@oclif/config' import * as Errors from '@oclif/errors' import * as Parser from '@oclif/parser' import Help from '@oclif/plugin-help' -import {sortBy, uniqBy} from '@oclif/plugin-help/lib/util' 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 @@ -151,9 +151,9 @@ export default abstract class Command { 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 => !t.hidden) - topics = sortBy(topics, t => t.name) - topics = uniqBy(topics, t => t.name) + 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))) +}