smartcli/dist/smartcli.classes.smartcli.js

133 lines
8.0 KiB
JavaScript

"use strict";
const q = require("q");
const rxjs_1 = require("rxjs");
const plugins = require("./smartcli.plugins");
// import classes
const lik_1 = require("lik");
class Smartcli {
constructor() {
/**
* map of all Command/Promise objects to keep track
*/
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 command execution.
*/
addCommandAlias(keyArg, aliasArg) {
this.argv = this.argv.alias(keyArg, aliasArg);
return;
}
/**
* 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(commandNameArg) {
let done = q.defer();
this.allCommandPromisesMap.add({
commandName: commandNameArg,
promise: done.promise
});
this.parseStarted.promise
.then(() => {
if (this.argv._.indexOf(commandNameArg) === 0) {
done.resolve(this.argv);
}
else {
done.reject(this.argv);
}
});
return done.promise;
}
/**
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg) {
return this.allCommandPromisesMap.find(commandDeferredObjectArg => {
return commandDeferredObjectArg.commandName === commandNameArg;
}).promise;
}
/**
* 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(this.argv);
});
return triggerSubject;
}
/**
* execute trigger by name
* @param commandNameArg - the name of the command to trigger
*/
trigger(triggerName) {
let triggerSubject = this.allTriggerObservablesMap.find(triggerObservableObjectArg => {
return triggerObservableObjectArg.triggerName === triggerName;
}).subject;
triggerSubject.next(this.argv);
return triggerSubject;
}
/**
* allows to specify help text to be printed above the rest of the help text
*/
addHelp(optionsArg) {
this.addCommand('help').then(argvArg => {
plugins.beautylog.log(optionsArg.helpText);
});
}
/**
* specify version to be printed for -v --version
*/
addVersion(versionArg) {
this.version = versionArg;
this.addCommandAlias('v', 'version');
this.parseStarted.promise
.then(() => {
if (this.argv.v) {
console.log(this.version);
}
});
}
/**
* returns promise that is resolved when no commands are specified
*/
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) {
done.resolve(this.argv);
}
else {
done.reject(this.argv);
}
});
return done.promise;
}
/**
* start the process of evaluating commands
*/
startParse() {
this.argv = this.argv.argv;
this.parseStarted.resolve();
return;
}
}
exports.Smartcli = Smartcli;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHVCQUFzQjtBQUN0QiwrQkFBOEI7QUFFOUIsOENBQTZDO0FBRTdDLGlCQUFpQjtBQUNqQiw2QkFBK0I7QUFhL0I7SUFrQkk7UUFWQTs7V0FFRztRQUNILDBCQUFxQixHQUFHLElBQUksZUFBUyxFQUF5QixDQUFBO1FBRTlEOztXQUVHO1FBQ0gsNkJBQXdCLEdBQUcsSUFBSSxlQUFTLEVBQTRCLENBQUE7UUFHaEUsSUFBSSxDQUFDLElBQUksR0FBRyxPQUFPLENBQUMsS0FBSyxDQUFBO1FBQ3pCLElBQUksQ0FBQyxhQUFhLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFBO1FBQzlCLElBQUksQ0FBQyxZQUFZLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFBO0lBQ2pDLENBQUM7SUFFRDs7T0FFRztJQUNILGVBQWUsQ0FBQyx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