introduce triggers

This commit is contained in:
Philipp Kunz 2016-12-18 20:53:50 +01:00
parent e257a38688
commit 857d31dcb2
8 changed files with 135 additions and 82 deletions

View File

@ -1,9 +1,14 @@
/// <reference types="q" />
import * as q from 'q';
import { Subject } from 'rxjs';
import { Objectmap } from 'lik';
export interface ICommandDeferredObject {
export interface ICommandPromiseObject {
commandName: string;
deferred: q.Deferred<any>;
promise: q.Promise<void>;
}
export interface ITriggerObservableObject {
triggerName: string;
subject: Subject<void>;
}
export declare class Smartcli {
argv: any;
@ -15,28 +20,34 @@ export declare class Smartcli {
/**
* map of all Command/Promise objects to keep track
*/
allCommandDeferredsMap: Objectmap<ICommandDeferredObject>;
allCommandPromisesMap: Objectmap<ICommandPromiseObject>;
/**
* map of all Trigger/Observable objects to keep track
*/
allTriggerObservablesMap: Objectmap<ITriggerObservableObject>;
constructor();
/**
* adds an alias, meaning one equals the other in terms of triggering associated commands
* adds an alias, meaning one equals the other in terms of command execution.
*/
addAlias(keyArg: any, aliasArg: any): void;
addCommandAlias(keyArg: any, aliasArg: any): void;
/**
* adds a Command by returning a Promise that reacts to the specific commandString given.
* Note: in e.g. "npm install something" the "install" is considered the command.
*/
addCommand(definitionArg: {
commandName: string;
}): q.Promise<any>;
addCommand(commandNameArg: string): q.Promise<any>;
/**
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg: string): q.Promise<void>;
/**
* triggers a command by name
* adds a Trigger. Like addCommand(), but returns an subscribable observable
*/
addTrigger(triggerNameArg: string): Subject<void>;
/**
* execute trigger by name
* @param commandNameArg - the name of the command to trigger
*/
triggerCommandByName(commandNameArg: string): q.Promise<any>;
trigger(triggerName: string): Subject<void>;
/**
* allows to specify help text to be printed above the rest of the help text
*/

View File

@ -1,5 +1,6 @@
"use strict";
const q = require("q");
const rxjs_1 = require("rxjs");
const plugins = require("./smartcli.plugins");
// import classes
const lik_1 = require("lik");
@ -8,15 +9,19 @@ class Smartcli {
/**
* map of all Command/Promise objects to keep track
*/
this.allCommandDeferredsMap = new lik_1.Objectmap();
this.allCommandPromisesMap = new lik_1.Objectmap();
/**
* map of all Trigger/Observable objects to keep track
*/
this.allTriggerObservablesMap = new lik_1.Objectmap();
this.argv = plugins.yargs;
this.questionsDone = q.defer();
this.parseStarted = q.defer();
}
/**
* adds an alias, meaning one equals the other in terms of triggering associated commands
* adds an alias, meaning one equals the other in terms of command execution.
*/
addAlias(keyArg, aliasArg) {
addCommandAlias(keyArg, aliasArg) {
this.argv = this.argv.alias(keyArg, aliasArg);
return;
}
@ -24,15 +29,15 @@ class Smartcli {
* adds a Command by returning a Promise that reacts to the specific commandString given.
* Note: in e.g. "npm install something" the "install" is considered the command.
*/
addCommand(definitionArg) {
addCommand(commandNameArg) {
let done = q.defer();
this.allCommandDeferredsMap.add({
commandName: definitionArg.commandName,
deferred: done
this.allCommandPromisesMap.add({
commandName: commandNameArg,
promise: done.promise
});
this.parseStarted.promise
.then(() => {
if (this.argv._.indexOf(definitionArg.commandName) === 0) {
if (this.argv._.indexOf(commandNameArg) === 0) {
done.resolve(this.argv);
}
else {
@ -45,28 +50,40 @@ class Smartcli {
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg) {
return this.allCommandDeferredsMap.find(commandDeferredObjectArg => {
return this.allCommandPromisesMap.find(commandDeferredObjectArg => {
return commandDeferredObjectArg.commandName === commandNameArg;
}).deferred.promise;
}).promise;
}
/**
* triggers a command by name
* adds a Trigger. Like addCommand(), but returns an subscribable observable
*/
addTrigger(triggerNameArg) {
let triggerSubject = new rxjs_1.Subject();
this.allTriggerObservablesMap.add({
triggerName: triggerNameArg,
subject: triggerSubject
});
this.addCommand(triggerNameArg).then(() => {
triggerSubject.next();
});
return triggerSubject;
}
/**
* execute trigger 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;
trigger(triggerName) {
let triggerSubject = this.allTriggerObservablesMap.find(triggerObservableObjectArg => {
return triggerObservableObjectArg.triggerName === triggerName;
}).subject;
triggerSubject.next();
return triggerSubject;
}
/**
* allows to specify help text to be printed above the rest of the help text
*/
addHelp(optionsArg) {
this.addCommand({
commandName: 'help'
}).then(argvArg => {
this.addCommand('help').then(argvArg => {
plugins.beautylog.log(optionsArg.helpText);
});
}
@ -75,7 +92,7 @@ class Smartcli {
*/
addVersion(versionArg) {
this.version = versionArg;
this.addAlias('v', 'version');
this.addCommandAlias('v', 'version');
this.parseStarted.promise
.then(() => {
if (this.argv.v) {
@ -88,6 +105,10 @@ class Smartcli {
*/
standardTask() {
let done = q.defer();
this.allCommandPromisesMap.add({
commandName: 'standard',
promise: done.promise
});
this.parseStarted.promise
.then(() => {
if (this.argv._.length === 0 && !this.argv.v) {
@ -109,4 +130,4 @@ class Smartcli {
}
}
exports.Smartcli = Smartcli;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHVCQUFzQjtBQUV0Qiw4Q0FBNkM7QUFFN0MsaUJBQWlCO0FBQ2pCLDZCQUE2QjtBQVE3QjtJQWFJO1FBTEE7O1dBRUc7UUFDSCwyQkFBc0IsR0FBRyxJQUFJLGVBQVMsRUFBMEIsQ0FBQTtRQUc1RCxJQUFJLENBQUMsSUFBSSxHQUFHLE9BQU8sQ0FBQyxLQUFLLENBQUE7UUFDekIsSUFBSSxDQUFDLGFBQWEsR0FBRyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7UUFDOUIsSUFBSSxDQUFDLFlBQVksR0FBRyxDQUFDLENBQUMsS0FBSyxFQUFFLEN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FPLENBQUE7UUFDekIsSUFBSSxDQUFDLHFCQUFxQixDQUFDLEdBQUcsQ0FBQztZQUMzQixXQUFXLEVBQUUsVUFBVTtZQUN2QixPQUFPLEVBQUUsSUFBSSxDQUFDLE9BQU87U0FDeEIsQ0FBQyxDQUFBO1FBQ0YsSUFBSSxDQUFDLFlBQVksQ0FBQyxPQUFPO2FBQ3BCLElBQUksQ0FBQztZQUNGLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLE1BQU0sS0FBSyxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUM7Z0JBQzNDLElBQUksQ0FBQyxPQUFPLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFBO1lBQzNCLENBQUM7WUFBQyxJQUFJLENBQUMsQ0FBQztnQkFDSixJQUFJLENBQUMsTUFBTSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQTtZQUMxQixDQUFDO1FBQ0wsQ0FBQyxDQUFDLENBQUE7UUFDTixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQTtJQUN2QixDQUFDO0lBRUQ7O09BRUc7SUFDSCxVQUFVO1FBQ04sSUFBSSxDQUFDLElBQUksR0FBRyxJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQTtRQUMxQixJQUFJLENBQUMsWUFBWSxDQUFDLE9BQU8sRUFBRSxDQUFBO1FBQzNCLE1BQU0sQ0FBQTtJQUNWLENBQUM7Q0FFSjtBQS9JRCw0QkErSUMifQ==

View File

@ -10,4 +10,4 @@ const path = require("path");
exports.path = path;
const smartparam = require("smartparam");
exports.smartparam = smartparam;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF3QjtBQUV4QiwrQkFBOEI7QUFPMUIsc0JBQUs7QUFOVCx1Q0FBc0M7QUFPbEMsOEJBQVM7QUFOYiwyQkFBMEI7QUFPdEIsa0JBQUc7QUFOUCw2QkFBNEI7QUFPeEIsb0JBQUk7QUFOUix5Q0FBd0M7QUFPcEMsZ0NBQVUifQ==
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLDBCQUF1QjtBQUV2QiwrQkFBOEI7QUFPMUIsc0JBQUs7QUFOVCx1Q0FBc0M7QUFPbEMsOEJBQVM7QUFOYiwyQkFBMEI7QUFPdEIsa0JBQUc7QUFOUCw2QkFBNEI7QUFPeEIsb0JBQUk7QUFOUix5Q0FBd0M7QUFPcEMsZ0NBQVUifQ==

View File

@ -34,6 +34,7 @@
"beautylog": "^6.0.0",
"lik": "^1.0.24",
"q": "^1.4.1",
"rxjs": "^5.0.1",
"smartparam": "0.1.1",
"typings-global": "^1.0.14",
"yargs": "^6.4.0"

View File

@ -12,9 +12,7 @@ describe('smartcli.Smartcli class', function () {
});
describe('.addCommand', function () {
it('should add an command', function () {
smartCliTestObject.addCommand({
commandName: 'awesome'
});
smartCliTestObject.addCommand('awesome');
});
});
describe('.standardTask', function () {
@ -26,11 +24,11 @@ describe('smartcli.Smartcli class', function () {
done();
});
});
describe('.triggerCommandByName', function () {
describe('.trigger', function () {
let hasExecuted = false;
it('should accept a command', function (done) {
smartCliTestObject.addCommand({ commandName: 'triggerme' })
.then(argvArg => {
smartCliTestObject.addTrigger('triggerme')
.subscribe(() => {
hasExecuted = true;
});
done();
@ -39,11 +37,9 @@ describe('smartcli.Smartcli class', function () {
should(hasExecuted).be.false();
});
it('should execute when triggered', function (done) {
smartCliTestObject.triggerCommandByName('triggerme')
.then(argvArg => {
smartCliTestObject.trigger('triggerme');
should(hasExecuted).be.true();
done();
}).catch(err => { done(err); });
});
});
describe('.startParse', function () {
@ -52,4 +48,4 @@ describe('smartcli.Smartcli class', function () {
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQiwwQ0FBMEM7QUFDMUMsaUNBQWdDO0FBRWhDLFFBQVEsQ0FBQyx5QkFBeUIsRUFBQztJQUMvQixJQUFJLGtCQUFxQyxDQUFBO0lBQ3pDLFFBQVEsQ0FBQyxnQkFBZ0IsRUFBQztRQUN0QixFQUFFLENBQUMsOEJBQThCLEVBQUM7WUFDOUIsa0JBQWtCLEdBQUcsSUFBSSxRQUFRLENBQUMsUUFBUSxFQUFFLENBQUE7WUFDNUMsTUFBTSxDQUFDLGtCQUFrQixDQUFDLENBQUMsRUFBRSxDQUFDLFVBQVUsQ0FBQyxRQUFRLENBQUMsUUFBUSxDQUFDLENBQUE7UUFDL0QsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQTtJQUNGLFFBQVEsQ0FBQyxhQUFhLEVBQUM7UUFDbkIsRUFBRSxDQUFDLHVCQUF1QixFQUFDO1lBQ3ZCLGtCQUFrQixDQUFDLFVBQVUsQ0FBQztnQkFDMUIsV0FBVyxFQUFFLFNBQVM7YUFDekIsQ0FBQyxDQUFBO1FBQ04sQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQTtJQUNGLFFBQVEsQ0FBQyxlQUFlLEVBQUM7UUFDckIsRUFBRSxDQUFDLHF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FBQVEsQ0FBQyxhQUFhLEVBQUM7UUFDbkIsRUFBRSxDQUFDLG9DQUFvQyxFQUFDO1lBQ3BDLGtCQUFrQixDQUFDLFVBQVUsRUFBRSxDQUFBO1FBQ25DLENBQUMsQ0FBQyxDQUFBO0lBQ04sQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9

View File

@ -13,9 +13,7 @@ describe('smartcli.Smartcli class',function(){
})
describe('.addCommand',function(){
it('should add an command',function(){
smartCliTestObject.addCommand({
commandName: 'awesome'
})
smartCliTestObject.addCommand('awesome')
})
})
describe('.standardTask',function(){
@ -27,11 +25,11 @@ describe('smartcli.Smartcli class',function(){
done()
})
})
describe('.triggerCommandByName', function() {
describe('.trigger', function() {
let hasExecuted = false
it('should accept a command', function(done) {
smartCliTestObject.addCommand({commandName: 'triggerme'})
.then(argvArg => {
smartCliTestObject.addTrigger('triggerme')
.subscribe(() => {
hasExecuted = true
})
done()
@ -40,11 +38,9 @@ describe('smartcli.Smartcli class',function(){
should(hasExecuted).be.false()
})
it('should execute when triggered', function(done) {
smartCliTestObject.triggerCommandByName('triggerme')
.then(argvArg => {
smartCliTestObject.trigger('triggerme')
should(hasExecuted).be.true()
done()
}).catch(err => { done(err) })
})
})
describe('.startParse',function(){

View File

@ -1,4 +1,5 @@
import * as q from 'q'
import { Subject } from 'rxjs'
import * as plugins from './smartcli.plugins'
@ -6,9 +7,14 @@ import * as plugins from './smartcli.plugins'
import { Objectmap } from 'lik'
// interfaces
export interface ICommandDeferredObject {
commandName: string
deferred: q.Deferred<any>
export interface ICommandPromiseObject {
commandName: string,
promise: q.Promise<void>
}
export interface ITriggerObservableObject {
triggerName: string
subject: Subject<void>
}
export class Smartcli {
@ -22,7 +28,12 @@ export class Smartcli {
/**
* map of all Command/Promise objects to keep track
*/
allCommandDeferredsMap = new Objectmap<ICommandDeferredObject>()
allCommandPromisesMap = new Objectmap<ICommandPromiseObject>()
/**
* map of all Trigger/Observable objects to keep track
*/
allTriggerObservablesMap = new Objectmap<ITriggerObservableObject>()
constructor() {
this.argv = plugins.yargs
@ -31,9 +42,9 @@ export class Smartcli {
}
/**
* adds an alias, meaning one equals the other in terms of triggering associated commands
* adds an alias, meaning one equals the other in terms of command execution.
*/
addAlias(keyArg,aliasArg): void {
addCommandAlias(keyArg,aliasArg): void {
this.argv = this.argv.alias(keyArg,aliasArg)
return
}
@ -42,15 +53,15 @@ export class Smartcli {
* adds a Command by returning a Promise that reacts to the specific commandString given.
* Note: in e.g. "npm install something" the "install" is considered the command.
*/
addCommand(definitionArg: {commandName: string}): q.Promise<any> {
addCommand(commandNameArg: string): q.Promise<any> {
let done = q.defer<any>()
this.allCommandDeferredsMap.add({
commandName: definitionArg.commandName,
deferred: done
this.allCommandPromisesMap.add({
commandName: commandNameArg,
promise: done.promise
})
this.parseStarted.promise
.then(() => {
if (this.argv._.indexOf(definitionArg.commandName) === 0) {
if (this.argv._.indexOf(commandNameArg) === 0) {
done.resolve(this.argv)
} else {
done.reject(this.argv)
@ -63,21 +74,36 @@ export class Smartcli {
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg: string): q.Promise<void> {
return this.allCommandDeferredsMap.find(commandDeferredObjectArg => {
return this.allCommandPromisesMap.find(commandDeferredObjectArg => {
return commandDeferredObjectArg.commandName === commandNameArg
}).deferred.promise
}).promise
}
/**
* triggers a command by name
* adds a Trigger. Like addCommand(), but returns an subscribable observable
*/
addTrigger(triggerNameArg: string) {
let triggerSubject = new Subject<void>()
this.allTriggerObservablesMap.add({
triggerName: triggerNameArg,
subject: triggerSubject
})
this.addCommand(triggerNameArg).then(() => {
triggerSubject.next()
})
return triggerSubject
}
/**
* execute trigger 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
trigger(triggerName: string) {
let triggerSubject = this.allTriggerObservablesMap.find(triggerObservableObjectArg => {
return triggerObservableObjectArg.triggerName === triggerName
}).subject
triggerSubject.next()
return triggerSubject
}
/**
@ -86,9 +112,7 @@ export class Smartcli {
addHelp(optionsArg: {
helpText: string
}) {
this.addCommand({
commandName: 'help'
}).then(argvArg => {
this.addCommand('help').then(argvArg => {
plugins.beautylog.log(optionsArg.helpText)
})
}
@ -98,7 +122,7 @@ export class Smartcli {
*/
addVersion(versionArg: string) {
this.version = versionArg
this.addAlias('v','version')
this.addCommandAlias('v','version')
this.parseStarted.promise
.then(() => {
if (this.argv.v) {
@ -112,6 +136,10 @@ export class Smartcli {
*/
standardTask(): q.Promise<any> {
let done = q.defer<any>()
this.allCommandPromisesMap.add({
commandName: 'standard',
promise: done.promise
})
this.parseStarted.promise
.then(() => {
if (this.argv._.length === 0 && !this.argv.v) {

View File

@ -1,4 +1,4 @@
import 'typings-global';
import 'typings-global'
import * as yargs from 'yargs'
import * as beautylog from 'beautylog'