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

Commit

Permalink
Add loading for alternative help plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
chadian committed Apr 2, 2020
1 parent 6be030b commit e3267ed
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const pjson = require('../package.json')
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 {HelpBase} from '@oclif/plugin-help'
import {format, inspect} from 'util'

import * as flags from './flags'
import {sortBy, uniqBy} from './util'
import {sortBy, uniqBy, getHelpPluginPackage} from './util'

/**
* swallows stdout epipe errors
Expand Down Expand Up @@ -186,8 +186,9 @@ export default abstract class Command {
}

protected _help() {
const HHelp: typeof Help = require('@oclif/plugin-help').default
const help = new HHelp(this.config)
const pluginPackage = getHelpPluginPackage(pjson)
const HHelp = require(pluginPackage).default
const help: HelpBase = 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
Expand Down
8 changes: 5 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as Config from '@oclif/config'
import Help from '@oclif/plugin-help'
import {HelpBase} from '@oclif/plugin-help'

import {Command} from '.'
import {getHelpPluginPackage} from './util'

export class Main extends Command {
static run(argv = process.argv.slice(2), options?: Config.LoadOptions) {
Expand Down Expand Up @@ -36,8 +37,9 @@ export class Main extends Command {
}

protected _help() {
const HHelp: typeof Help = require('@oclif/plugin-help').default
const help = new HHelp(this.config)
const pluginPackage = getHelpPluginPackage(this.config.pjson)
const HHelp = require(pluginPackage).default
const help: HelpBase = new HHelp(this.config)
help.showHelp(this.argv)
return this.exit(0)
}
Expand Down
16 changes: 16 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Config} from '@oclif/config'

export function compact<T>(a: (T | undefined)[]): T[] {
return a.filter((a): a is T => Boolean(a))
}
Expand Down Expand Up @@ -31,3 +33,17 @@ export function sortBy<T>(arr: T[], fn: (i: T) => sort.Types | sort.Types[]): T[

return arr.sort((a, b) => compare(fn(a), fn(b)))
}

export function getHelpPluginPackage(pjson: Config['pjson']): string {
const configuredHelpPlugin = pjson && pjson.oclif && pjson.oclif.helpPlugin
const defaultHelpPlugin = '@oclif/plugin-help'

if (configuredHelpPlugin) {
try {
require(configuredHelpPlugin)
return configuredHelpPlugin
} catch {}
}

return defaultHelpPlugin
}

0 comments on commit e3267ed

Please sign in to comment.