Skip to content

Commit

Permalink
feat: add default command (rm root cmd) (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo committed Jan 27, 2021
1 parent 364d6dd commit fbf1a0f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
4 changes: 0 additions & 4 deletions src/config/plugin.ts
Expand Up @@ -13,8 +13,6 @@ import {Topic} from '../interfaces/topic'
import {tsPath} from './ts-node'
import {compact, exists, flatMap, loadJSON, mapValues} from './util'

const ROOT_INDEX_CMD_ID = ''

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

const hasManifest = function (p: string): boolean {
Expand Down Expand Up @@ -174,8 +172,6 @@ export class Plugin implements IPlugin {
const p = path.parse(file)
const topics = p.dir.split('/')
const command = p.name !== 'index' && p.name
// support src/commands/index as a "root" command
if (!command && this.type === 'core' && p.dir.length === 0 && p.name === 'index') return ROOT_INDEX_CMD_ID
return [...topics, command].filter(f => f).join(':')
})
this._debug('found commands', ids)
Expand Down
8 changes: 4 additions & 4 deletions src/help/index.ts
Expand Up @@ -18,8 +18,6 @@ const {
bold,
} = Chalk

const ROOT_INDEX_CMD_ID = ''

function getHelpSubject(args: string[]): string | undefined {
for (const arg of args) {
if (arg === '--') return
Expand Down Expand Up @@ -97,8 +95,10 @@ export class Help extends HelpBase {
public showHelp(argv: string[]) {
const subject = getHelpSubject(argv)
if (!subject) {
const rootCmd = this.config.findCommand(ROOT_INDEX_CMD_ID)
if (rootCmd) this.showCommandHelp(rootCmd)
if (this.config.pjson.oclif.default) {
const rootCmd = this.config.findCommand(this.config.pjson.oclif.default)
if (rootCmd) this.showCommandHelp(rootCmd)
}
this.showRootHelp()
return
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/pjson.ts
Expand Up @@ -16,6 +16,7 @@ export namespace PJSON {
description?: string;
hooks?: { [name: string]: (string | string[]) };
commands?: string;
default?: string;
plugins?: string[];
devPlugins?: string[];
helpClass?: string;
Expand Down
11 changes: 4 additions & 7 deletions src/main.ts
Expand Up @@ -4,16 +4,14 @@ import * as Interfaces from './interfaces'
import {Config} from './config'
import {getHelpClass} from './help'

const ROOT_INDEX_CMD_ID = ''

const log = (message = '', ...args: any[]) => {
// tslint:disable-next-line strict-type-predicates
message = typeof message === 'string' ? message : inspect(message)
process.stdout.write(format(message, ...args) + '\n')
}

const helpOverride = (argv: string[], config: Interfaces.Config): boolean => {
if (argv.length === 0 && !config.findCommand(ROOT_INDEX_CMD_ID)) return true
if (argv.length === 0 && !config.pjson.oclif.default) return true
for (const arg of argv) {
if (arg === '--help') return true
if (arg === '--') return false
Expand Down Expand Up @@ -48,17 +46,16 @@ export async function run(argv = process.argv.slice(2), options?: Interfaces.Loa
})
const Help = getHelpClass(config)
const help = new Help(config)
const helpArgv = config.findCommand(ROOT_INDEX_CMD_ID) ? ['', ...argv] : argv
help.showHelp(helpArgv)
help.showHelp(argv)
return
}

// find & run command
if (!config.findCommand(id)) {
const topic = config.findTopic(id)
if (topic) return config.runCommand('help', [id])
if (config.findCommand(ROOT_INDEX_CMD_ID)) {
id = ROOT_INDEX_CMD_ID
if (config.pjson.oclif.default) {
id = config.pjson.oclif.default
argvSlice = argv
}
}
Expand Down

0 comments on commit fbf1a0f

Please sign in to comment.