Repository: oclif/core
Pull request #39 (2020-09-14 21:41:35)

feat: mv getHelpClass, Help & HelpBase export to root

codecov[bot]: # [Codecov](https://codecov.io/gh/oclif/core/pull/39?src=pr&el=h1) Report
> Merging [#39](https://codecov.io/gh/oclif/core/pull/39?src=pr&el=desc) into [master](https://codecov.io/gh/oclif/core/commit/70ea6e16ada0ce18ebbeb52ed9802d3a1536276d?el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/oclif/core/pull/39/graphs/tree.svg?width=650&height=150&src=pr&token=K0kGk9tefA)](https://codecov.io/gh/oclif/core/pull/39?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master     #39   +/-   ##
======================================
  Coverage    0.00%   0.00%           
======================================
  Files           4       4           
  Lines          25      25           
  Branches        5       5           
======================================
  Misses         25      25           
```


| [Impacted Files](https://codecov.io/gh/oclif/core/pull/39?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/index.ts](https://codecov.io/gh/oclif/core/pull/39/diff?src=pr&el=tree#diff-c3JjL2luZGV4LnRz) | `0.00% <ø> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/oclif/core/pull/39?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/oclif/core/pull/39?src=pr&el=footer). Last update [70ea6e1...5405460](https://codecov.io/gh/oclif/core/pull/39?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/help/command.ts b/src/help/command.ts
index 62dbad6..5e41fd2 100644
--- a/src/help/command.ts
+++ b/src/help/command.ts
@@ -5,7 +5,8 @@ import stripAnsi = require('strip-ansi')
 
 import {HelpOptions} from '.'
 import {renderList} from './list'
-import {castArray, compact, sortBy, template} from './util'
+import {castArray, compact, sortBy} from '../util'
+import {template} from './util'
 
 const {
   underline,
diff --git a/src/help/index.ts b/src/help/index.ts
index bd3ac62..cb3e59a 100644
--- a/src/help/index.ts
+++ b/src/help/index.ts
@@ -1,15 +1,17 @@
-import * as Config from '../config'
-import {error} from '../errors'
 import * as Chalk from 'chalk'
 import indent = require('indent-string')
 import stripAnsi = require('strip-ansi')
 
+import * as Config from '../config'
+import {error} from '../errors'
 import CommandHelp from './command'
 import {renderList} from './list'
 import RootHelp from './root'
 import {stdtermwidth} from './screen'
-import {compact, sortBy, template, uniqBy} from './util'
-import {getHelpClass} from './util'
+import {compact, sortBy, uniqBy} from '../util'
+import {template} from './util'
+
+export {getHelpClass} from './util'
 
 const wrap = require('wrap-ansi')
 const {
@@ -57,7 +59,7 @@ export abstract class HelpBase {
   public abstract showCommandHelp(command: Config.Command, topics: Config.Topic[]): void;
 }
 
-export default class Help extends HelpBase {
+export class Help extends HelpBase {
   render: (input: string) => string
 
   /*
@@ -261,8 +263,3 @@ export default class Help extends HelpBase {
     return this.formatCommand(command)
   }
 }
-
-export {
-  Help,
-  getHelpClass,
-}
diff --git a/src/help/root.ts b/src/help/root.ts
index 502d8db..ba47361 100644
--- a/src/help/root.ts
+++ b/src/help/root.ts
@@ -4,7 +4,8 @@ import indent = require('indent-string')
 import stripAnsi = require('strip-ansi')
 
 import {HelpOptions} from '.'
-import {compact, template} from './util'
+import {compact} from '../util'
+import {template} from './util'
 
 const wrap = require('wrap-ansi')
 const {
diff --git a/src/help/util.ts b/src/help/util.ts
index 2014d15..65a6cda 100644
--- a/src/help/util.ts
+++ b/src/help/util.ts
@@ -1,61 +1,15 @@
-import {tsPath} from '../config'
 import lodashTemplate = require('lodash.template')
-import {IConfig} from '../config'
-import Help, {HelpBase, HelpOptions} from '.'
 
-export function uniqBy<T>(arr: T[], fn: (cur: T) => any): T[] {
-  return arr.filter((a, i) => {
-    const aVal = fn(a)
-    return !arr.find((b, j) => j > i && fn(b) === aVal)
-  })
-}
-
-export function compact<T>(a: (T | undefined)[]): T[] {
-  return a.filter((a): a is T => Boolean(a))
-}
-
-export function castArray<T>(input?: T | T[]): T[] {
-  if (input === undefined) return []
-  return Array.isArray(input) ? input : [input]
-}
-
-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
-      const 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)))
-}
-
-export namespace sort {
-  export type Types = string | number | undefined | boolean
-}
-
-export function template(context: any): (t: string) => string {
-  function render(t: string): string {
-    return lodashTemplate(t)(context)
-  }
-  return render
-}
+import {IConfig} from '../config/config'
+import {Help, HelpBase, HelpOptions} from '.'
+import * as Config from '../config'
 
 interface HelpBaseDerived {
   new(config: IConfig, opts?: Partial<HelpOptions>): HelpBase;
 }
 
 function extractExport(config: IConfig, classPath: string): HelpBaseDerived {
-  const helpClassPath = tsPath(config.root, classPath)
+  const helpClassPath = Config.tsPath(config.root, classPath)
   return require(helpClassPath) as HelpBaseDerived
 }
 
@@ -65,7 +19,7 @@ function extractClass(exported: any): HelpBaseDerived {
 
 export function getHelpClass(config: IConfig): HelpBaseDerived {
   const pjson = config.pjson
-  const configuredClass = pjson && pjson.oclif &&  pjson.oclif.helpClass
+  const configuredClass = pjson && pjson.oclif && pjson.oclif.helpClass
 
   if (configuredClass) {
     try {
@@ -78,3 +32,10 @@ export function getHelpClass(config: IConfig): HelpBaseDerived {
 
   return Help
 }
+
+export function template(context: any): (t: string) => string {
+  function render(t: string): string {
+    return lodashTemplate(t)(context)
+  }
+  return render
+}
diff --git a/src/index.ts b/src/index.ts
index 7c48699..6910dce 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -6,7 +6,7 @@ import {run} from './main'
 import * as Config from './config'
 import * as Errors from './errors'
 import * as flags from './flags'
-import * as Help from './help'
+import {HelpBase, Help, getHelpClass} from './help'
 import * as Parser from './parser'
 
 export {
@@ -14,7 +14,9 @@ export {
   Config,
   Errors,
   flags,
+  getHelpClass,
   Help,
+  HelpBase,
   Parser,
   run,
 }
diff --git a/src/util.ts b/src/util.ts
index 9f10e0e..93c5f13 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -31,3 +31,8 @@ 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 castArray<T>(input?: T | T[]): T[] {
+  if (input === undefined) return []
+  return Array.isArray(input) ? input : [input]
+}


Test patch:
diff --git a/test/command/command.test.ts b/test/command/command.test.ts
index 5a920e4..4826fbf 100644
--- a/test/command/command.test.ts
+++ b/test/command/command.test.ts
@@ -1,11 +1,10 @@
 import {expect, fancy} from 'fancy-test'
 import path = require('path')
 
-import {Config, Command as Base, flags, Help as PluginHelp} from '../../src'
+import {Config, Command as Base, flags} from '../../src'
 import {TestHelpClassConfig} from './helpers/test-help-in-src/src/test-help-plugin'
 
 // const pjson = require('../package.json')
-const originalgetHelpClass = PluginHelp.getHelpClass
 const root = path.resolve(__dirname, '../../package.json')
 
 class Command extends Base {
@@ -290,17 +289,9 @@ USAGE
     describe('from a help class', () => {
       fancy
       .stdout()
-      .stub(PluginHelp, 'getHelpClass', ((config: Config.IConfig) => {
-        const patchedConfig = {
-          ...config,
-          root: `${__dirname}/helpers/test-help-in-lib/`,
-        }
-
-        return originalgetHelpClass(patchedConfig)
-      }) as unknown as () => void)
       .add('config', async () => {
         const config: TestHelpClassConfig = await Config.load()
-        config.pjson.oclif.helpClass = './lib/test-help-plugin'
+        config.pjson.oclif.helpClass = './test/command/helpers/test-help-in-lib/lib/test-help-plugin'
         return config
       })
       .do(async ({config}) => {
@@ -323,17 +314,9 @@ USAGE
 
       fancy
       .stdout()
-      .stub(PluginHelp, 'getHelpClass', ((config: Config.IConfig) => {
-        const patchedConfig = {
-          ...config,
-          root: `${__dirname}/helpers/test-help-in-src/`,
-        }
-
-        return originalgetHelpClass(patchedConfig)
-      }) as unknown as () => void)
       .add('config', async () => {
         const config: TestHelpClassConfig = await Config.load()
-        config.pjson.oclif.helpClass = './src/test-help-plugin'
+        config.pjson.oclif.helpClass = './test/command/helpers/test-help-in-src/src/test-help-plugin'
         return config
       })
       .do(async ({config}) => {
@@ -356,17 +339,9 @@ USAGE
 
       fancy
       .stdout()
-      .stub(PluginHelp, 'getHelpClass', ((config: Config.IConfig) => {
-        const patchedConfig = {
-          ...config,
-          root: `${__dirname}/helpers/test-help-in-src/`,
-        }
-
-        return originalgetHelpClass(patchedConfig)
-      }) as unknown as () => void)
       .add('config', async () => {
         const config: TestHelpClassConfig = await Config.load()
-        config.pjson.oclif.helpClass = './src/test-help-plugin'
+        config.pjson.oclif.helpClass = './test/command/helpers/test-help-in-src/src/test-help-plugin'
         return config
       })
       .do(async ({config}) => {
diff --git a/test/command/helpers/test-help-in-src/src/test-help-plugin.ts b/test/command/helpers/test-help-in-src/src/test-help-plugin.ts
index 838da11..d62a748 100644
--- a/test/command/helpers/test-help-in-src/src/test-help-plugin.ts
+++ b/test/command/helpers/test-help-in-src/src/test-help-plugin.ts
@@ -1,9 +1,9 @@
 import {spy, SinonSpy} from 'sinon'
-import {Config, Help} from '../../../../../src'
+import {Config, HelpBase} from '../../../../../src'
 
 export type TestHelpClassConfig = Config.IConfig & { showCommandHelpSpy?: SinonSpy; showHelpSpy?: SinonSpy }
 
-export default class extends Help.HelpBase {
+export default class extends HelpBase {
   constructor(config: any, opts: any) {
     super(config, opts)
     config.showCommandHelpSpy = this.showCommandHelp
diff --git a/test/command/main.test.ts b/test/command/main.test.ts
index 8a975b3..8de34f1 100644
--- a/test/command/main.test.ts
+++ b/test/command/main.test.ts
@@ -2,13 +2,12 @@ import {expect, fancy} from 'fancy-test'
 import path = require('path')
 
 import {Main} from '../../src/main'
-import {Help as PluginHelp, Config} from '../../src'
+import {Config} from '../../src'
 import {TestHelpClassConfig} from './helpers/test-help-in-src/src/test-help-plugin'
 
 const root = path.resolve(__dirname, '../../package.json')
 const pjson = require(root)
 const version = `@oclif/core/${pjson.version} ${process.platform}-${process.arch} node-${process.version}`
-const originalgetHelpClass = PluginHelp.getHelpClass
 
 describe('main', () => {
   fancy
@@ -48,7 +47,7 @@ COMMANDS
   describe('with an alternative help class', async () => {
     const getMainWithHelpClass = async () => {
       const config: TestHelpClassConfig = await Config.load(root)
-      config.pjson.oclif.helpClass = './src/test-help-plugin'
+      config.pjson.oclif.helpClass = './test/command/helpers/test-help-in-src/src/test-help-plugin'
 
       class MainWithHelpClass extends Main {
         config = config
@@ -59,14 +58,6 @@ COMMANDS
 
     fancy
     .stdout()
-    .stub(PluginHelp, 'getHelpClass', ((config: Config.IConfig) => {
-      const patchedConfig = {
-        ...config,
-        root: `${__dirname}/helpers/test-help-in-src/`,
-      }
-
-      return originalgetHelpClass(patchedConfig)
-    }) as unknown as () => void)
     .do(async () => (await getMainWithHelpClass()).run(['-h']))
     .catch('EEXIT: 0')
     .do((output: any) => expect(output.stdout).to.equal('hello showHelp\n'))
@@ -74,14 +65,6 @@ COMMANDS
 
     fancy
     .stdout()
-    .stub(PluginHelp, 'getHelpClass', ((config: Config.IConfig) =>  {
-      const patchedConfig = {
-        ...config,
-        root: `${__dirname}/helpers/test-help-in-src/`,
-      }
-
-      return originalgetHelpClass(patchedConfig)
-    }) as unknown as () => void)
     .do(async () => (await getMainWithHelpClass()).run(['--help']))
     .catch('EEXIT: 0')
     .do((output: any) => expect(output.stdout).to.equal('hello showHelp\n'))
@@ -89,14 +72,6 @@ COMMANDS
 
     fancy
     .stdout()
-    .stub(PluginHelp, 'getHelpClass', ((config: Config.IConfig) => {
-      const patchedConfig = {
-        ...config,
-        root: `${__dirname}/helpers/test-help-in-src/`,
-      }
-
-      return originalgetHelpClass(patchedConfig)
-    }) as unknown as () => void)
     .do(async () => (await getMainWithHelpClass()).run(['help']))
     .catch('EEXIT: 0')
     .do((output: any) => expect(output.stdout).to.equal('hello showHelp\n'))
diff --git a/test/help/format-command.test.ts b/test/help/format-command.test.ts
index 6a73338..a94b164 100644
--- a/test/help/format-command.test.ts
+++ b/test/help/format-command.test.ts
@@ -4,7 +4,7 @@ import stripAnsi = require('strip-ansi')
 
 const g: any = global
 g.columns = 80
-import Help from '../../src/help'
+import {Help} from '../../src/help'
 
 class Command extends Base {
   async run() {
diff --git a/test/help/format-commands.test.ts b/test/help/format-commands.test.ts
index c2ac761..5fba831 100644
--- a/test/help/format-commands.test.ts
+++ b/test/help/format-commands.test.ts
@@ -5,7 +5,7 @@ import stripAnsi = require('strip-ansi')
 
 const g: any = global
 g.columns = 80
-import Help from '../../src/help'
+import {Help} from '../../src/help'
 import {AppsDestroy, AppsCreate} from './fixtures/fixtures'
 
 // extensions to expose method as public for testing
diff --git a/test/help/format-root.test.ts b/test/help/format-root.test.ts
index 66381f6..16c6296 100644
--- a/test/help/format-root.test.ts
+++ b/test/help/format-root.test.ts
@@ -3,7 +3,7 @@ import stripAnsi = require('strip-ansi')
 
 const g: any = global
 g.columns = 80
-import Help from '../../src/help'
+import {Help} from '../../src/help'
 
 const VERSION = require('../../package.json').version
 const UA = `@oclif/core/${VERSION} ${process.platform}-${process.arch} node-${process.version}`
diff --git a/test/help/format-topic.test.ts b/test/help/format-topic.test.ts
index df762a2..4e4a903 100644
--- a/test/help/format-topic.test.ts
+++ b/test/help/format-topic.test.ts
@@ -4,7 +4,7 @@ import stripAnsi = require('strip-ansi')
 
 const g: any = global
 g.columns = 80
-import Help from '../../src/help'
+import {Help} from '../../src/help'
 
 // extensions to expose method as public for testing
 class TestHelp extends Help {
diff --git a/test/help/format-topics.test.ts b/test/help/format-topics.test.ts
index 3798bd2..3ff4c9d 100644
--- a/test/help/format-topics.test.ts
+++ b/test/help/format-topics.test.ts
@@ -4,7 +4,7 @@ import stripAnsi = require('strip-ansi')
 
 const g: any = global
 g.columns = 80
-import Help from '../../src/help'
+import {Help} from '../../src/help'
 
 // extensions to expose method as public for testing
 class TestHelp extends Help {
diff --git a/test/help/show-help.test.ts b/test/help/show-help.test.ts
index 79c9065..ba303d1 100644
--- a/test/help/show-help.test.ts
+++ b/test/help/show-help.test.ts
@@ -5,7 +5,7 @@ import * as path from 'path'
 
 const g: any = global
 g.columns = 80
-import Help from '../../src/help'
+import {Help} from '../../src/help'
 import {AppsIndex, AppsDestroy, AppsCreate, AppsTopic, AppsAdminTopic, AppsAdminAdd, AppsAdminIndex, DbCreate, DbTopic} from './fixtures/fixtures'
 
 // extension makes previously protected methods public
diff --git a/test/help/util.test.ts b/test/help/util.test.ts
index b004818..b576901 100644
--- a/test/help/util.test.ts
+++ b/test/help/util.test.ts
@@ -2,7 +2,7 @@
 import {resolve} from 'path'
 import * as Config from '../../src/config'
 import {expect, test} from '@oclif/test'
-import {getHelpClass} from '../../src/help/util'
+import {getHelpClass} from '../../src/help'
 import configuredHelpClass from  '../../src/help/_test-help-class'
 
 describe('util', () => {