Skip to content

Commit

Permalink
fix: support src/commands/index cmd (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
RasPhilCo authored Sep 9, 2020
1 parent 36eb02f commit 2c14c3c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/command/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -16,19 +18,23 @@ 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)
}

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
Expand Down
4 changes: 4 additions & 0 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions src/help/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const {
bold,
} = Chalk

const ROOT_INDEX_CMD_ID = ''

export interface HelpOptions {
all?: boolean;
maxWidth: number;
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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('')
}
Expand Down

0 comments on commit 2c14c3c

Please sign in to comment.