From 2c14c3c0987e9cf97ff1d34648cf4a0a90e595d2 Mon Sep 17 00:00:00 2001 From: RasPhilCo Date: Wed, 9 Sep 2020 11:35:17 -0500 Subject: [PATCH] fix: support src/commands/index cmd (#35) --- src/command/main.ts | 10 ++++++++-- src/config/plugin.ts | 4 ++++ src/help/index.ts | 9 +++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/command/main.ts b/src/command/main.ts index 0b94cc0a3..6c681b22b 100644 --- a/src/command/main.ts +++ b/src/command/main.ts @@ -4,6 +4,8 @@ import {HelpBase} from '../help' import {Command} from '.' import {getHelpClass} from '../help' +const ROOT_INDEX_CMD_ID = '' + export class Main extends Command { static run(argv = process.argv.slice(2), options?: Config.LoadOptions) { return super.run(argv, options || (module.parent && module.parent.parent && module.parent.parent.filename) || __dirname) @@ -16,11 +18,15 @@ export class Main extends Command { } async run() { - const [id, ...argv] = this.argv + let [id, ...argv] = this.argv this.parse({strict: false, '--': false, ...this.ctor as any}) if (!this.config.findCommand(id)) { const topic = this.config.findTopic(id) if (topic) return this._help() + if (this.config.findCommand(ROOT_INDEX_CMD_ID)) { + id = ROOT_INDEX_CMD_ID + argv = this.argv + } } await this.config.runCommand(id, argv) } @@ -28,7 +34,7 @@ export class Main extends Command { protected _helpOverride(): boolean { if (['-v', '--version', 'version'].includes(this.argv[0])) return this._version() as any if (['-h', 'help'].includes(this.argv[0])) return true - if (this.argv.length === 0) return true + if (this.argv.length === 0 && !this.config.findCommand(ROOT_INDEX_CMD_ID)) return true for (const arg of this.argv) { if (arg === '--help') return true if (arg === '--') return false diff --git a/src/config/plugin.ts b/src/config/plugin.ts index 69bd76f14..f8847b28e 100644 --- a/src/config/plugin.ts +++ b/src/config/plugin.ts @@ -11,6 +11,8 @@ import {Topic} from './topic' import {tsPath} from './ts-node' import {compact, exists, flatMap, loadJSON, mapValues} from './util' +const ROOT_INDEX_CMD_ID = '' + export interface Options { root: string; name?: string; @@ -232,6 +234,8 @@ 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) diff --git a/src/help/index.ts b/src/help/index.ts index cf062dd5c..bd3ac627a 100644 --- a/src/help/index.ts +++ b/src/help/index.ts @@ -16,6 +16,8 @@ const { bold, } = Chalk +const ROOT_INDEX_CMD_ID = '' + export interface HelpOptions { all?: boolean; maxWidth: number; @@ -25,8 +27,8 @@ export interface HelpOptions { function getHelpSubject(args: string[]): string | undefined { for (const arg of args) { if (arg === '--') return - if (arg.startsWith('-')) continue - if (arg === 'help') continue + if (arg === 'help' || arg === '--help' || arg === '-h') continue + if (arg.startsWith('-')) return return arg } } @@ -99,6 +101,8 @@ export default 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) this.showRootHelp() return } @@ -159,6 +163,7 @@ export default class Help extends HelpBase { } if (rootCommands.length > 0) { + rootCommands = rootCommands.filter(c => c.id) console.log(this.formatCommands(rootCommands)) console.log('') }