Skip to content

Commit

Permalink
feat: mv getHelpClass, Help & HelpBase export to root (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo committed Sep 15, 2020
1 parent 70ea6e1 commit 3d272d8
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 129 deletions.
3 changes: 2 additions & 1 deletion src/help/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 7 additions & 10 deletions src/help/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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

/*
Expand Down Expand Up @@ -261,8 +263,3 @@ export default class Help extends HelpBase {
return this.formatCommand(command)
}
}

export {
Help,
getHelpClass,
}
3 changes: 2 additions & 1 deletion src/help/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
63 changes: 12 additions & 51 deletions src/help/util.ts
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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 {
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ 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 {
Command,
Config,
Errors,
flags,
getHelpClass,
Help,
HelpBase,
Parser,
run,
}
Expand Down
5 changes: 5 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
33 changes: 4 additions & 29 deletions test/command/command.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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}) => {
Expand All @@ -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}) => {
Expand All @@ -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}) => {
Expand Down
4 changes: 2 additions & 2 deletions test/command/helpers/test-help-in-src/src/test-help-plugin.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 2 additions & 27 deletions test/command/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -59,44 +58,20 @@ 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'))
.it('works with -h')

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'))
.it('works with --help')

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'))
Expand Down
2 changes: 1 addition & 1 deletion test/help/format-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion test/help/format-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/help/format-root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
2 changes: 1 addition & 1 deletion test/help/format-topic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/help/format-topics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test/help/show-help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/help/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 3d272d8

Please sign in to comment.