added .triggerCommandByName

This commit is contained in:
2016-12-18 01:36:19 +01:00
parent 00f5539e6b
commit 19a5082381
5 changed files with 103 additions and 19 deletions

View File

@ -6,9 +6,9 @@ import * as plugins from './smartcli.plugins'
import {Objectmap} from 'lik'
// interfaces
export interface ICommandPromiseObject {
export interface ICommandDeferredObject {
commandName: string
promise: q.Promise<any>
deferred: q.Deferred<any>
}
export class Smartcli {
@ -19,8 +19,11 @@ export class Smartcli {
questions
version: string
// maps
allCommandPromises = new Objectmap<ICommandPromiseObject>()
/**
* map of all Command/Promise objects to keep track
*/
allCommandDeferredsMap = new Objectmap<ICommandDeferredObject>()
constructor() {
this.argv = plugins.yargs
this.questionsDone = q.defer()
@ -41,6 +44,10 @@ export class Smartcli {
*/
addCommand(definitionArg: {commandName: string}): q.Promise<any> {
let done = q.defer<any>()
this.allCommandDeferredsMap.add({
commandName: definitionArg.commandName,
deferred: done
})
this.parseStarted.promise
.then(() => {
if (this.argv._.indexOf(definitionArg.commandName) === 0) {
@ -55,10 +62,22 @@ export class Smartcli {
/**
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg: string) {
return this.allCommandPromises.find(commandPromiseObjectArg => {
return commandPromiseObjectArg.commandName === commandNameArg
}).promise
getCommandPromiseByName(commandNameArg: string): q.Promise<void> {
return this.allCommandDeferredsMap.find(commandDeferredObjectArg => {
return commandDeferredObjectArg.commandName === commandNameArg
}).deferred.promise
}
/**
* triggers a command by name
* @param commandNameArg - the name of the command to trigger
*/
triggerCommandByName(commandNameArg: string) {
let commandDeferred = this.allCommandDeferredsMap.find(commandDeferredObjectArg => {
return commandDeferredObjectArg.commandName === commandNameArg
}).deferred
commandDeferred.resolve()
return commandDeferred.promise
}
/**