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

Commit

Permalink
Add helpful error messages when a plugin cannot be found
Browse files Browse the repository at this point in the history
  • Loading branch information
chadian committed Apr 3, 2020
1 parent 939bedc commit 4a7dabb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,13 @@ export default abstract class Command {
}
}

protected _helpPlugin() {
return getHelpPluginPackage(this.config.pjson)
}

protected _help() {
const pluginPackage = getHelpPluginPackage(this.config.pjson)
const HHelp = require(pluginPackage).default
const helpPlugin = this._helpPlugin()
const HHelp = require(helpPlugin).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 = ''
Expand Down
5 changes: 2 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as Config from '@oclif/config'
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 @@ -37,8 +36,8 @@ export class Main extends Command {
}

protected _help() {
const pluginPackage = getHelpPluginPackage(this.config.pjson)
const HHelp = require(pluginPackage).default
const helpPlugin = this._helpPlugin()
const HHelp = require(helpPlugin).default
const help: HelpBase = new HHelp(this.config)
help.showHelp(this.argv)
return this.exit(0)
Expand Down
19 changes: 12 additions & 7 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ 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'
export function getHelpPluginPackage(pjson: Config['pjson'], defaultPlugin = '@oclif/plugin-help'): string {
const configuredPlugin = pjson.oclif.helpPlugin

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

return defaultHelpPlugin
try {
require(defaultPlugin)
return defaultPlugin
} catch {}

if (configuredPlugin) throw new Error(`Unable to load configured help plugin "${configuredPlugin}" from package.json`)
throw new Error('Could not load a help plugin, consider installing the @oclif/plugin-help package')
}
43 changes: 43 additions & 0 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {expect, fancy} from 'fancy-test'

import Base, {flags} from '../src'
import {TestHelpPluginConfig} from './helpers/test-help-plugin'
import {getHelpPluginPackage} from '../src/util'

// const pjson = require('../package.json')

Expand Down Expand Up @@ -269,6 +270,48 @@ USAGE
`)
})

fancy
.stdout()
.add('config', async () => {
const config: TestHelpPluginConfig = await Config.load()
config.pjson.oclif.helpPlugin = 'plugin-does-not-exist'
return config
})
.do(async ({config}) => {
class CMD extends Command {
config = config

_helpPlugin() {
return getHelpPluginPackage(this.config.pjson, '')
}
}

await CMD.run(['-h'])
})
.catch((error: Error) => expect(error.message).to.equal('Unable to load configured help plugin "plugin-does-not-exist" from package.json'))
.it('shows useful error message when configured help plugin cannot be loaded')

fancy
.stdout()
.add('config', async () => {
const config: TestHelpPluginConfig = await Config.load()
config.pjson.oclif.helpPlugin = undefined
return config
})
.do(async ({config}) => {
class CMD extends Command {
config = config

_helpPlugin() {
return getHelpPluginPackage(this.config.pjson, '')
}
}

await CMD.run(['-h'])
})
.catch((error: Error) => expect(error.message).to.equal('Could not load a help plugin, consider installing the @oclif/plugin-help package'))
.it('shows useful error message when no help plugin has been configured and the default cannot be loaded')

describe('from a help plugin', () => {
fancy
.stdout()
Expand Down

0 comments on commit 4a7dabb

Please sign in to comment.