-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,63 @@ | ||
import {format, inspect} from 'util' | ||
|
||
import * as Interfaces from './interfaces' | ||
import {HelpBase, getHelpClass} from './help' | ||
import Command from './command' | ||
import {Config} from './config' | ||
import {getHelpClass} from './help' | ||
|
||
const ROOT_INDEX_CMD_ID = '' | ||
|
||
export class Main extends Command { | ||
static run(argv = process.argv.slice(2), options?: Interfaces.LoadOptions) { | ||
return super.run(argv, options || (module.parent && module.parent.parent && module.parent.parent.filename) || __dirname) | ||
} | ||
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') | ||
} | ||
|
||
async init() { | ||
const [id, ...argv] = this.argv | ||
await this.config.runHook('init', {id, argv}) | ||
return super.init() | ||
const helpOverride = (argv: string[], config: Interfaces.Config): boolean => { | ||
if (['-h', 'help'].includes(argv[0])) return true | ||
if (argv.length === 0 && !config.findCommand(ROOT_INDEX_CMD_ID)) return true | ||
for (const arg of argv) { | ||
if (arg === '--help') return true | ||
if (arg === '--') return false | ||
} | ||
return false | ||
} | ||
|
||
async run() { | ||
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) | ||
} | ||
const versionOverride = (argv: string[]): boolean => { | ||
if (['-v', '--version', 'version'].includes(argv[0])) return true | ||
return false | ||
} | ||
|
||
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 && !this.config.findCommand(ROOT_INDEX_CMD_ID)) return true | ||
for (const arg of this.argv) { | ||
if (arg === '--help') return true | ||
if (arg === '--') return false | ||
} | ||
return false | ||
export async function run(argv = process.argv.slice(2), options?: Interfaces.LoadOptions) { | ||
// return Main.run(argv, options) | ||
const config = await Config.load(options || (module.parent && module.parent.parent && module.parent.parent.filename) || __dirname) as Config | ||
|
||
// run init hook | ||
let [id, ...argvSlice] = argv | ||
await config.runHook('init', {id, argv: argvSlice}) | ||
|
||
// display version if applicable | ||
if (versionOverride(argv)) { | ||
log(config.userAgent) | ||
return | ||
} | ||
|
||
protected _help() { | ||
const HelpClass = getHelpClass(this.config) | ||
const help: HelpBase = new HelpClass(this.config) | ||
help.showHelp(this.argv) | ||
return this.exit(0) | ||
// display help version if applicable | ||
if (helpOverride(argv, config)) { | ||
const Help = getHelpClass(config) | ||
const help = new Help(config) | ||
const helpArgv = config.findCommand(ROOT_INDEX_CMD_ID) ? ['', ...argv] : argv | ||
help.showHelp(helpArgv) | ||
return | ||
} | ||
} | ||
|
||
export function run(argv = process.argv.slice(2), options?: Interfaces.LoadOptions) { | ||
return Main.run(argv, options) | ||
// 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 | ||
argvSlice = argv | ||
} | ||
} | ||
await config.runCommand(id, argvSlice) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters