Skip to content

Commit

Permalink
feat: mv Command & flag export to root (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo authored Sep 14, 2020
1 parent be0d001 commit 70ea6e1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 83 deletions.
16 changes: 8 additions & 8 deletions src/command/command.ts → src/command.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const pjson = require('../../package.json')
import * as Config from '../config'
import * as Errors from '../errors'
import * as Parser from '../parser'
import {HelpBase} from '../help'
import {format, inspect} from 'util'

import * as Config from './config'
import * as Errors from './errors'
import {PrettyPrintableError} from './errors'
import * as Parser from './parser'
import {HelpBase, getHelpClass} from './help'
import * as flags from './flags'
import {sortBy, uniqBy} from './util'
import {getHelpClass} from '../help'
import {PrettyPrintableError} from '../errors'

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

/**
* swallows stdout epipe errors
* this occurs when stdout closes such as when piping to head
*/
process.stdout.on('error', err => {
process.stdout.on('error', (err: any) => {
if (err && err.code === 'EPIPE')
return
throw err
Expand Down
31 changes: 0 additions & 31 deletions src/command/index.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/command/flags.ts → src/flags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {IConfig} from '../config'
import * as Parser from '../parser'

import {Command} from '.'
import {IConfig} from './config'
import * as Parser from './parser'
import Command from './command'

export type ICompletionContext = {
args?: { [name: string]: string };
Expand Down Expand Up @@ -56,7 +55,7 @@ export {_enum as enum}

const stringFlag = build({})
export {stringFlag as string}
export {boolean, integer} from '../parser'
export {boolean, integer} from './parser'

export const version = (opts: Partial<Parser.flags.IBooleanFlag<boolean>> = {}) => {
return Parser.flags.boolean({
Expand Down
28 changes: 26 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
import * as Command from './command'
import {run} from './command'
import * as path from 'path'
import * as semver from 'semver'

import Command from './command'
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 * as Parser from './parser'

export {
Command,
Config,
Errors,
flags,
Help,
Parser,
run,
}

function checkCWD() {
try {
process.cwd()
} catch (error) {
if (error.code === 'ENOENT') {
process.stderr.write('WARNING: current directory does not exist\n')
}
}
}
function checkNodeVersion() {
const root = path.join(__dirname, '..')
const pjson = require(path.join(root, 'package.json'))
if (!semver.satisfies(process.versions.node, pjson.engines.node)) {
process.stderr.write(`WARNING\nWARNING Node version must be ${pjson.engines.node} to use this CLI\nWARNING Current node version: ${process.versions.node}\nWARNING\n`)
}
}
checkCWD()
checkNodeVersion()
8 changes: 3 additions & 5 deletions src/command/main.ts → src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as Config from '../config'
import {HelpBase} from '../help'

import {Command} from '.'
import {getHelpClass} from '../help'
import * as Config from './config'
import {HelpBase, getHelpClass} from './help'
import Command from './command'

const ROOT_INDEX_CMD_ID = ''

Expand Down
File renamed without changes.
41 changes: 20 additions & 21 deletions test/command/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as Config from '../../src/config'
import {expect, fancy} from 'fancy-test'
import path = require('path')

import Base, {flags} from '../../src/command'
import {Config, Command as Base, flags, Help as PluginHelp} from '../../src'
import {TestHelpClassConfig} from './helpers/test-help-in-src/src/test-help-plugin'
import * as PluginHelp from '../../src/help'

const originalgetHelpClass = PluginHelp.getHelpClass

// const pjson = require('../package.json')
const originalgetHelpClass = PluginHelp.getHelpClass
const root = path.resolve(__dirname, '../../package.json')

class Command extends Base {
static description = 'test command'
Expand All @@ -34,11 +33,11 @@ describe('command', () => {
fancy
.do(async () => {
class Command extends Base {
static description = 'test command'
static description = 'test command'

async run() {
return 101
}
async run() {
return 101
}
}

expect(await Command.run([])).to.equal(101)
Expand Down Expand Up @@ -205,14 +204,14 @@ describe('command', () => {
.stdout()
.it('has a flag', async ctx => {
class CMD extends Base {
static flags = {
foo: flags.string(),
}

async run() {
const {flags} = this.parse(CMD)
this.log(flags.foo)
}
static flags = {
foo: flags.string(),
}

async run() {
const {flags} = this.parse(CMD)
this.log(flags.foo)
}
}

await CMD.run(['--foo=bar'])
Expand All @@ -225,7 +224,7 @@ describe('command', () => {
.stdout()
.add('config', () => Config.load())
.do(async () => {
await Command.run(['--version'])
await Command.run(['--version'], root)
})
.catch(/EEXIT: 0/)
.it('shows version', ctx => {
Expand All @@ -238,9 +237,9 @@ describe('command', () => {
.stdout()
.do(() => {
class CMD extends Command {
static flags = {help: flags.help()}
static flags = {help: flags.help()}
}
return CMD.run(['--help'])
return CMD.run(['--help'], root)
})
.catch(/EEXIT: 0/)
.it('--help', ctx => {
Expand All @@ -259,7 +258,7 @@ OPTIONS
.stdout()
.do(async () => {
class CMD extends Command {}
await CMD.run(['-h'])
await CMD.run(['-h'], root)
})
.catch(/EEXIT: 0/)
.it('-h', ctx => {
Expand Down
18 changes: 9 additions & 9 deletions test/command/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import {expect, fancy} from 'fancy-test'
import path = require('path')

import {Main} from '../../src/command/main'
import * as PluginHelp from '../../src/help'
import * as Config from '../../src/config'
import {Main} from '../../src/main'
import {Help as PluginHelp, Config} from '../../src'
import {TestHelpClassConfig} from './helpers/test-help-in-src/src/test-help-plugin'
import path = require('path')

const pjson = require(path.resolve(__dirname, '../../package.json'))
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
.stdout()
.do(() => Main.run(['plugins']))
.do(() => Main.run(['plugins'], root))
.do((output: any) => expect(output.stdout).to.equal('no plugins installed\n'))
.it('runs plugins')

fancy
.stdout()
.do(() => Main.run(['-v']))
.do(() => Main.run(['-v'], root))
.catch('EEXIT: 0')
.do((output: any) => expect(output.stdout).to.equal(version + '\n'))
.it('runs -v')

fancy
.stdout()
.do(() => Main.run(['-h']))
.do(() => Main.run(['-h'], root))
.catch('EEXIT: 0')
.do((output: any) => expect(output.stdout).to.equal(`base library for oclif CLIs
Expand All @@ -47,7 +47,7 @@ COMMANDS

describe('with an alternative help class', async () => {
const getMainWithHelpClass = async () => {
const config: TestHelpClassConfig = await Config.load()
const config: TestHelpClassConfig = await Config.load(root)
config.pjson.oclif.helpClass = './src/test-help-plugin'

class MainWithHelpClass extends Main {
Expand Down
3 changes: 1 addition & 2 deletions test/help/format-command.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Command as Base, flags} from '../../src/command'
import * as Config from '../../src/config'
import {Command as Base, flags, Config} from '../../src'
import {expect, test as base} from '@oclif/test'
import stripAnsi = require('strip-ansi')

Expand Down

0 comments on commit 70ea6e1

Please sign in to comment.