2016-06-09 12:03:18 +00:00
|
|
|
"use strict";
|
2017-04-22 19:03:28 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const smartq = require("smartq");
|
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;
|
2017-04-22 19:03:28 +00:00
|
|
|
this.questionsDone = smartq.defer();
|
|
|
|
this.parseStarted = smartq.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) {
|
2016-06-10 01:48:01 +00:00
|
|
|
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) {
|
2017-04-22 19:03:28 +00:00
|
|
|
let done = smartq.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);
|
2016-06-10 01:48:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-08-26 07:21:42 +00:00
|
|
|
done.reject(this.argv);
|
2016-06-10 01:48:01 +00:00
|
|
|
}
|
|
|
|
});
|
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(() => {
|
2016-12-18 19:58:37 +00:00
|
|
|
triggerSubject.next(this.argv);
|
2016-12-18 19:53:50 +00:00
|
|
|
});
|
|
|
|
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;
|
2016-12-18 19:58:37 +00:00
|
|
|
triggerSubject.next(this.argv);
|
2016-12-18 19:53:50 +00:00
|
|
|
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) {
|
2016-06-10 01:48:01 +00:00
|
|
|
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-06-10 01:48:01 +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() {
|
2017-04-22 19:03:28 +00:00
|
|
|
let done = smartq.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);
|
2016-06-10 01:48:01 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-08-26 07:21:42 +00:00
|
|
|
done.reject(this.argv);
|
2016-06-10 01:48:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
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;
|
2016-06-10 01:48:01 +00:00
|
|
|
this.parseStarted.resolve();
|
2016-09-04 15:50:10 +00:00
|
|
|
return;
|
2016-08-26 07:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-10 01:10:21 +00:00
|
|
|
exports.Smartcli = Smartcli;
|
2017-04-22 19:03:28 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpQ0FBZ0M7QUFDaEMsK0JBQThCO0FBRTlCLDhDQUE2QztBQUU3QyxpQkF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