Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
fix: use showCommandHelp to delegate topic help (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo authored Apr 15, 2019
1 parent fa7de62 commit 3c0bafd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
30 changes: 30 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
export function compact<T>(a: (T | undefined)[]): T[] {
return a.filter((a): a is T => !!a)
}

export function uniqBy<T>(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<T>(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)))
}
2 changes: 2 additions & 0 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ USAGE
OPTIONS
--help show CLI help
`)
})

Expand All @@ -257,6 +258,7 @@ OPTIONS
USAGE
$ @oclif/command
`)
})
})
Expand Down

0 comments on commit 3c0bafd

Please sign in to comment.