added .triggerCommandByName

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

View File

@ -1,9 +1,9 @@
/// <reference types="q" />
import * as q from 'q';
import { Objectmap } from 'lik';
export interface ICommandPromiseObject {
export interface ICommandDeferredObject {
commandName: string;
promise: q.Promise<any>;
deferred: q.Deferred<any>;
}
export declare class Smartcli {
argv: any;
@ -12,7 +12,10 @@ export declare class Smartcli {
commands: any;
questions: any;
version: string;
allCommandPromises: Objectmap<ICommandPromiseObject>;
/**
* map of all Command/Promise objects to keep track
*/
allCommandDeferredsMap: Objectmap<ICommandDeferredObject>;
constructor();
/**
* adds an alias, meaning one equals the other in terms of triggering associated commands
@ -28,7 +31,12 @@ export declare class Smartcli {
/**
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg: string): q.Promise<any>;
getCommandPromiseByName(commandNameArg: string): q.Promise<void>;
/**
* triggers a command by name
* @param commandNameArg - the name of the command to trigger
*/
triggerCommandByName(commandNameArg: string): q.Promise<any>;
/**
* allows to specify help text to be printed above the rest of the help text
*/

View File

@ -5,8 +5,10 @@ const plugins = require("./smartcli.plugins");
const lik_1 = require("lik");
class Smartcli {
constructor() {
// maps
this.allCommandPromises = new lik_1.Objectmap();
/**
* map of all Command/Promise objects to keep track
*/
this.allCommandDeferredsMap = new lik_1.Objectmap();
this.argv = plugins.yargs;
this.questionsDone = q.defer();
this.parseStarted = q.defer();
@ -24,6 +26,10 @@ class Smartcli {
*/
addCommand(definitionArg) {
let done = q.defer();
this.allCommandDeferredsMap.add({
commandName: definitionArg.commandName,
deferred: done
});
this.parseStarted.promise
.then(() => {
if (this.argv._.indexOf(definitionArg.commandName) === 0) {
@ -39,9 +45,20 @@ class Smartcli {
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg) {
return this.allCommandPromises.find(commandPromiseObjectArg => {
return commandPromiseObjectArg.commandName === commandNameArg;
}).promise;
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) {
let commandDeferred = this.allCommandDeferredsMap.find(commandDeferredObjectArg => {
return commandDeferredObjectArg.commandName === commandNameArg;
}).deferred;
commandDeferred.resolve();
return commandDeferred.promise;
}
/**
* allows to specify help text to be printed above the rest of the help text
@ -92,4 +109,4 @@ class Smartcli {
}
}
exports.Smartcli = Smartcli;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHVCQUFzQjtBQUV0Qiw4Q0FBNkM7QUFFN0MsaUJBQWlCO0FBQ2pCLDZCQUE2QjtBQVE3QjtJQVVJO1FBRkEsT0FBTztRQUNQLHVCQUFrQixHQUFHLElBQUksZUFBUyxFQUF5QixDQUFBO1FBRXZELElBQUksQ0FBQyxJQUFJLEdBQUcsT0FBTyxDQUFDLEtBQUssQ0FBQTtRQUN6QixJQUFJLENBQUMsYUFBYSxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtRQUM5QixJQUFJLENBQUMsWUFBWSxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtJQUNqQyxDQUFDO0lBRUQ7O09BRUc7SUFDSCxRQUFRLENBQUMsTUFBTSxFQUFDLFFBQVE7UUFDcEIsSUFBSSxDQUFDLElBQUksR0FBRyxJQUFJLENBQUMsSUFBSSxDQUFDLEtBQUssQ0FBQyxNQUFNLEVBQUMsUUFBUSxDQUFDLENBQUE7UUFDNUMsTUFBTSxDQUFBO0lBQ1YsQ0FBQztJQUVE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0FBQztZQUNGLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLE1BQU0sS0FBSyxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUM7Z0JBQzNDLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFBO1lBQzNCLENBQUM7WUFBQyxJQUFJLENBQUMsQ0FBQztnQkFDSixJQUFJLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQTtZQUMxQixDQUFDO1FBQ0wsQ0FBQyxDQUFDLENBQUE7UUFDTixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQTtJQUN2QixDQUFDO0lBRUQ7O09BRUc7SUFDSCxVQUFVO1FBQ04sSUFBSSxDQUFDLElBQUksR0FBRyxJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQTtRQUMxQixJQUFJLENBQUMsWUFBWSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQzNCLE1BQU0sQ0FBQTtJQUNWLENBQUM7Q0FFSjtBQXpIRCw0QkF5SEMifQ==

View File

@ -26,10 +26,30 @@ describe('smartcli.Smartcli class', function () {
done();
});
});
describe('.triggerCommandByName', function () {
let hasExecuted = false;
it('should accept a command', function (done) {
smartCliTestObject.addCommand({ commandName: 'triggerme' })
.then(argvArg => {
hasExecuted = true;
});
done();
});
it('should not have executed yet', function () {
should(hasExecuted).be.false();
});
it('should execute when triggered', function (done) {
smartCliTestObject.triggerCommandByName('triggerme')
.then(argvArg => {
should(hasExecuted).be.true();
done();
}).catch(err => { done(err); });
});
});
describe('.startParse', function () {
it('should start parsing the CLI input', function () {
smartCliTestObject.startParse();
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiI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0FBQTtRQUN0QyxDQUFDLENBQUMsQ0FBQTtJQUNOLENBQUMsQ0FBQyxDQUFBO0lBQ0YsUUFBUSxDQUFDLGFBQWEsRUFBQztRQUNuQixFQUFFLENBQUMsb0NBQW9DLEVBQUM7WUFDcEMsa0JBQWtCLENBQUMsVUFBVSxFQUFFLENBQUE7UUFDbkMsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=

View File

@ -27,6 +27,26 @@ describe('smartcli.Smartcli class',function(){
done()
})
})
describe('.triggerCommandByName', function() {
let hasExecuted = false
it('should accept a command', function(done) {
smartCliTestObject.addCommand({commandName: 'triggerme'})
.then(argvArg => {
hasExecuted = true
})
done()
})
it('should not have executed yet', function() {
should(hasExecuted).be.false()
})
it('should execute when triggered', function(done) {
smartCliTestObject.triggerCommandByName('triggerme')
.then(argvArg => {
should(hasExecuted).be.true()
done()
}).catch(err => { done(err) })
})
})
describe('.startParse',function(){
it('should start parsing the CLI input',function(){
smartCliTestObject.startParse()

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
}
/**