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

Commit

Permalink
Extract getHelpPlugin to @oclif/plugin-help
Browse files Browse the repository at this point in the history
  • Loading branch information
chadian committed Apr 13, 2020
1 parent 98f160b commit a4b686c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
38 changes: 0 additions & 38 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -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<T>(a: (T | undefined)[]): T[] {
return a.filter((a): a is T => Boolean(a))
}
Expand Down Expand Up @@ -35,37 +31,3 @@ export function sortBy<T>(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}`)
}
}
12 changes: 6 additions & 6 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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 () => {
Expand All @@ -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/`,
Expand Down Expand Up @@ -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/`,
Expand Down Expand Up @@ -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/`,
Expand Down
10 changes: 5 additions & 5 deletions test/main.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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/`,
Expand All @@ -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/`,
Expand All @@ -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/`,
Expand Down

0 comments on commit a4b686c

Please sign in to comment.