diff --git a/src/command.ts b/src/command.ts index 90103692..17cf34a2 100644 --- a/src/command.ts +++ b/src/command.ts @@ -6,7 +6,8 @@ import {HelpBase} from '@oclif/plugin-help' import {format, inspect} from 'util' import * as flags from './flags' -import {sortBy, uniqBy, getHelpPlugin} from './util' +import {sortBy, uniqBy} from './util' +import {getHelpPlugin} from '@oclif/plugin-help' /** * swallows stdout epipe errors diff --git a/src/main.ts b/src/main.ts index 7253cb62..c638a78e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import * as Config from '@oclif/config' import {HelpBase} from '@oclif/plugin-help' import {Command} from '.' -import {getHelpPlugin} from './util' +import {getHelpPlugin} from '@oclif/plugin-help' export class Main extends Command { static run(argv = process.argv.slice(2), options?: Config.LoadOptions) { diff --git a/src/util.ts b/src/util.ts index f527a1f5..9f10e0eb 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,7 +1,3 @@ -import {IConfig} from '@oclif/config' -import {tsPath} from '@oclif/config/lib/ts-node' -import {HelpBase} from '@oclif/plugin-help' - export function compact(a: (T | undefined)[]): T[] { return a.filter((a): a is T => Boolean(a)) } @@ -35,37 +31,3 @@ export function sortBy(arr: T[], fn: (i: T) => sort.Types | sort.Types[]): T[ return arr.sort((a, b) => compare(fn(a), fn(b))) } - -interface HelpBaseDerived { - new(config: IConfig): HelpBase; -} - -export function extractPlugin(config: IConfig, pluginPath: string): HelpBaseDerived { - const helpPlugin = tsPath(config.root, pluginPath) - return require(helpPlugin) as HelpBaseDerived -} - -export function extractExport(exported: any): HelpBaseDerived { - return exported && exported.default ? exported.default : exported -} - -export function getHelpPlugin(config: IConfig, defaultPlugin = '@oclif/plugin-help'): HelpBaseDerived { - const pjson = config.pjson - const configuredPlugin = pjson && pjson.oclif && pjson.oclif.helpPlugin - - if (configuredPlugin) { - try { - const exported = extractPlugin(config, configuredPlugin) - return extractExport(exported) as HelpBaseDerived - } catch (error) { - throw new Error(`Unable to load configured help plugin "${configuredPlugin}" from package.json, failed with message:\n${error.message}`) - } - } - - try { - const exported = require(defaultPlugin) - return extractExport(exported) as HelpBaseDerived - } catch (error) { - throw new Error(`Could not load a help plugin, consider installing the @oclif/plugin-help package, failed with message:\n${error.message}`) - } -} diff --git a/test/command.test.ts b/test/command.test.ts index 2eaa01c1..087c0afb 100644 --- a/test/command.test.ts +++ b/test/command.test.ts @@ -3,9 +3,9 @@ import {expect, fancy} from 'fancy-test' import Base, {flags} from '../src' import {TestHelpPluginConfig} from './helpers/test-help-in-src/src/test-help-plugin' -import * as Util from '../src/util' +import * as PluginHelp from '@oclif/plugin-help' -const originalGetHelpPlugin = Util.getHelpPlugin +const originalGetHelpPlugin = PluginHelp.getHelpPlugin // const pjson = require('../package.json') @@ -290,7 +290,7 @@ USAGE fancy .stdout() - .stub(Util, 'getHelpPlugin', function (config: any) { + .stub(PluginHelp, 'getHelpPlugin', function (config: any) { return originalGetHelpPlugin(config, '') }) .add('config', async () => { @@ -310,7 +310,7 @@ USAGE describe('from a help plugin', () => { fancy .stdout() - .stub(Util, 'getHelpPlugin', function (config: Config.IConfig) { + .stub(PluginHelp, 'getHelpPlugin', function (config: Config.IConfig) { const patchedConfig = { ...config, root: `${__dirname}/helpers/test-help-in-lib/`, @@ -343,7 +343,7 @@ USAGE fancy .stdout() - .stub(Util, 'getHelpPlugin', function (config: Config.IConfig) { + .stub(PluginHelp, 'getHelpPlugin', function (config: Config.IConfig) { const patchedConfig = { ...config, root: `${__dirname}/helpers/test-help-in-src/`, @@ -376,7 +376,7 @@ USAGE fancy .stdout() - .stub(Util, 'getHelpPlugin', function (config: Config.IConfig) { + .stub(PluginHelp, 'getHelpPlugin', function (config: Config.IConfig) { const patchedConfig = { ...config, root: `${__dirname}/helpers/test-help-in-src/`, diff --git a/test/main.test.ts b/test/main.test.ts index ed092e66..dd2a673b 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -1,13 +1,13 @@ import {expect, fancy} from 'fancy-test' import {Main} from '../src/main' -import * as Util from '../src/util' +import * as PluginHelp from '@oclif/plugin-help' import * as Config from '@oclif/config' import {TestHelpPluginConfig} from './helpers/test-help-in-src/src/test-help-plugin' const pjson = require('../package.json') const version = `@oclif/command/${pjson.version} ${process.platform}-${process.arch} node-${process.version}` -const originalGetHelpPlugin = Util.getHelpPlugin +const originalGetHelpPlugin = PluginHelp.getHelpPlugin describe('main', () => { fancy @@ -56,7 +56,7 @@ COMMANDS fancy .stdout() - .stub(Util, 'getHelpPlugin', function (config: Config.IConfig) { + .stub(PluginHelp, 'getHelpPlugin', function (config: Config.IConfig) { const patchedConfig = { ...config, root: `${__dirname}/helpers/test-help-in-src/`, @@ -71,7 +71,7 @@ COMMANDS fancy .stdout() - .stub(Util, 'getHelpPlugin', function (config: Config.IConfig) { + .stub(PluginHelp, 'getHelpPlugin', function (config: Config.IConfig) { const patchedConfig = { ...config, root: `${__dirname}/helpers/test-help-in-src/`, @@ -86,7 +86,7 @@ COMMANDS fancy .stdout() - .stub(Util, 'getHelpPlugin', function (config: Config.IConfig) { + .stub(PluginHelp, 'getHelpPlugin', function (config: Config.IConfig) { const patchedConfig = { ...config, root: `${__dirname}/helpers/test-help-in-src/`,