Skip to content

Commit

Permalink
Merge pull request #184 from mattallty/fix-command-import
Browse files Browse the repository at this point in the history
fix(command): Fix command discovery
  • Loading branch information
mattallty authored Jun 13, 2020
2 parents 7dba9a1 + 47cdb9c commit f345ed8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/command/import.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
/* eslint-disable @typescript-eslint/ban-ts-ignore */
/* eslint-disable @typescript-eslint/camelcase */
/**
* @packageDocumentation
* @internal
*/
import { CommandCreator } from "../types"
import path from "path"

const requireFunc =
// @ts-ignore
typeof __webpack_require__ === "function" ? __non_webpack_require__ : require

export async function importCommand(file: string): Promise<CommandCreator> {
const { dir, base } = path.parse(file)
const filename = path.join(dir, base)
const mod = await import(filename)
return mod.default
const { dir, name } = path.parse(file)
const filename = path.join(dir, name)
const mod = requireFunc(filename)
return mod.default ?? mod
}
2 changes: 1 addition & 1 deletion src/command/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function scanCommands(
const data = zipObject(files, imp)
return map(data, (cmdBuilder, filename) => {
const { dir, name } = path.parse(filename)
const cmd = dir.split("/").concat(name).join(" ")
const cmd = dir ? [...dir.split("/"), name].join(" ") : name
const options = {
createCommand: createCommand.bind(null, program, cmd),
program,
Expand Down
2 changes: 1 addition & 1 deletion src/help/templates/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getOptionsTable, getCommandsTable } from "../utils"

export const program: Template = async (ctx: TemplateContext) => {
const { prog, globalOptions, eol, eol3, colorize, tpl } = ctx
const commands = prog.getCommands()
const commands = await prog.getAllCommands()
const options = Array.from(globalOptions.keys())
const help =
(await prog.getSynopsis()) +
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
* @module @caporal/core
*/
import { Program } from "./program"
export { Command } from "./command"
export * from "./types"

/**
* @ignore
Expand Down
11 changes: 7 additions & 4 deletions src/program/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class Program extends EventEmitter {
private _progCommand?: Command
private _bin: string
private _discoveryPath?: string
private _discoveredCommands?: Command[]

/**
* Number validator. Check that the value looks like a numeric one
Expand Down Expand Up @@ -371,7 +372,7 @@ export class Program extends EventEmitter {
*/
async getAllCommands(): Promise<Command[]> {
const discoveredCommands = await this.scanCommands()
return this.commands.concat(discoveredCommands)
return [...this.commands, ...discoveredCommands]
}

/**
Expand Down Expand Up @@ -501,9 +502,11 @@ export class Program extends EventEmitter {
if (this._discoveryPath === undefined) {
return []
}
const commands = await scanCommands(this, this._discoveryPath)
this.commands.push(...commands)
return commands
if (this._discoveredCommands) {
return this._discoveredCommands
}
this._discoveredCommands = await scanCommands(this, this._discoveryPath)
return this._discoveredCommands
}

/* istanbul ignore next */
Expand Down

0 comments on commit f345ed8

Please sign in to comment.