smartcli/dist/smartcli.classes.smartcli.js

133 lines
8.0 KiB
JavaScript
Raw Normal View History

2016-06-09 12:03:18 +00:00
"use strict";
2016-10-14 22:56:02 +00:00
const q = require("q");
2016-12-18 19:53:50 +00:00
const rxjs_1 = require("rxjs");
2016-08-26 07:21:42 +00:00
const plugins = require("./smartcli.plugins");
2016-08-26 09:52:09 +00:00
// import classes
const lik_1 = require("lik");
2016-08-26 07:21:42 +00:00
class Smartcli {
constructor() {
2016-12-18 00:36:19 +00:00
/**
* map of all Command/Promise objects to keep track
*/
2016-12-18 19:53:50 +00:00
this.allCommandPromisesMap = new lik_1.Objectmap();
/**
* map of all Trigger/Observable objects to keep track
*/
this.allTriggerObservablesMap = new lik_1.Objectmap();
2016-09-04 15:23:29 +00:00
this.argv = plugins.yargs;
2016-10-14 22:56:02 +00:00
this.questionsDone = q.defer();
this.parseStarted = q.defer();
2016-06-09 12:03:18 +00:00
}
2016-08-26 07:21:42 +00:00
/**
2016-12-18 19:53:50 +00:00
* adds an alias, meaning one equals the other in terms of command execution.
2016-08-26 07:21:42 +00:00
*/
2016-12-18 19:53:50 +00:00
addCommandAlias(keyArg, aliasArg) {
this.argv = this.argv.alias(keyArg, aliasArg);
2016-09-04 15:50:10 +00:00
return;
2016-08-26 07:21:42 +00:00
}
/**
* 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.
*/
2016-12-18 19:53:50 +00:00
addCommand(commandNameArg) {
2016-10-14 22:56:02 +00:00
let done = q.defer();
2016-12-18 19:53:50 +00:00
this.allCommandPromisesMap.add({
commandName: commandNameArg,
promise: done.promise
2016-12-18 00:36:19 +00:00
});
2016-06-10 02:13:23 +00:00
this.parseStarted.promise
2016-08-26 07:21:42 +00:00
.then(() => {
2016-12-18 19:53:50 +00:00
if (this.argv._.indexOf(commandNameArg) === 0) {
2016-08-26 07:21:42 +00:00
done.resolve(this.argv);
}
else {
2016-08-26 07:21:42 +00:00
done.reject(this.argv);
}
});
2016-06-22 09:55:23 +00:00
return done.promise;
2016-08-26 07:21:42 +00:00
}
/**
* gets a Promise for a command word
*/
2016-08-26 09:52:09 +00:00
getCommandPromiseByName(commandNameArg) {
2016-12-18 19:53:50 +00:00
return this.allCommandPromisesMap.find(commandDeferredObjectArg => {
2016-12-18 00:36:19 +00:00
return commandDeferredObjectArg.commandName === commandNameArg;
2016-12-18 19:53:50 +00:00
}).promise;
2016-12-18 00:36:19 +00:00
}
/**
2016-12-18 19:53:50 +00:00
* 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
2016-12-18 00:36:19 +00:00
* @param commandNameArg - the name of the command to trigger
*/
2016-12-18 19:53:50 +00:00
trigger(triggerName) {
let triggerSubject = this.allTriggerObservablesMap.find(triggerObservableObjectArg => {
return triggerObservableObjectArg.triggerName === triggerName;
}).subject;
triggerSubject.next();
return triggerSubject;
2016-08-26 07:21:42 +00:00
}
2016-08-26 09:52:09 +00:00
/**
* allows to specify help text to be printed above the rest of the help text
*/
addHelp(optionsArg) {
2016-12-18 19:53:50 +00:00
this.addCommand('help').then(argvArg => {
2016-08-26 09:52:09 +00:00
plugins.beautylog.log(optionsArg.helpText);
});
2016-08-26 07:21:42 +00:00
}
2016-08-26 09:52:09 +00:00
/**
* specify version to be printed for -v --version
*/
2016-08-26 07:21:42 +00:00
addVersion(versionArg) {
this.version = versionArg;
2016-12-18 19:53:50 +00:00
this.addCommandAlias('v', 'version');
2016-06-10 02:13:23 +00:00
this.parseStarted.promise
2016-08-26 07:21:42 +00:00
.then(() => {
if (this.argv.v) {
console.log(this.version);
2016-06-10 02:13:23 +00:00
}
});
2016-08-26 07:21:42 +00:00
}
2016-08-26 09:52:09 +00:00
/**
* returns promise that is resolved when no commands are specified
*/
2016-08-26 07:21:42 +00:00
standardTask() {
2016-10-14 22:56:02 +00:00
let done = q.defer();
2016-12-18 19:53:50 +00:00
this.allCommandPromisesMap.add({
commandName: 'standard',
promise: done.promise
});
2016-06-10 02:13:23 +00:00
this.parseStarted.promise
2016-08-26 07:21:42 +00:00
.then(() => {
2016-10-14 22:56:02 +00:00
if (this.argv._.length === 0 && !this.argv.v) {
2016-08-26 07:21:42 +00:00
done.resolve(this.argv);
}
else {
2016-08-26 07:21:42 +00:00
done.reject(this.argv);
}
});
return done.promise;
2016-08-26 07:21:42 +00:00
}
2016-09-04 15:32:12 +00:00
/**
* start the process of evaluating commands
*/
2016-08-26 07:21:42 +00:00
startParse() {
2016-06-10 02:13:23 +00:00
this.argv = this.argv.argv;
this.parseStarted.resolve();
2016-09-04 15:50:10 +00:00
return;
2016-08-26 07:21:42 +00:00
}
}
exports.Smartcli = Smartcli;
2016-12-18 19:53:50 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHVCQUFzQjtBQUN0QiwrQkFBOEI7QUFFOUIsOENBQTZDO0FBRTdDLGlCQUFpQjtBQUNqQiw2QkFBK0I7QUFhL0I7SUFrQkk7UUFWQTs7V0FFRztRQUNILDBCQUFxQixHQUFHLElBQUksZUFBUyxFQUF5QixDQUFBO1FBRTlEOztXQUVHO1FBQ0gsNkJBQXdCLEdBQUcsSUFBSSxlQUFTLEVBQTRCLENBQUE7UUFHaEUsSUFBSSxDQUFDLElBQUksR0FBRyxPQUFPLENBQUMsS0FBSyxDQUFBO1FBQ3pCLElBQUksQ0FBQyxhQUFhLEdBQUcsQ0FBQyxDQUFDLEtBQUssRU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