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");
|
2018-05-03 10:10:39 +00:00
|
|
|
/**
|
|
|
|
* class to create a new instance of Smartcli. Handles parsing of command line arguments.
|
|
|
|
*/
|
2016-08-26 07:21:42 +00:00
|
|
|
class Smartcli {
|
2018-05-03 10:10:39 +00:00
|
|
|
/**
|
|
|
|
* The constructor of Smartcli
|
|
|
|
*/
|
2016-08-26 07:21:42 +00:00
|
|
|
constructor() {
|
2017-10-12 20:44:34 +00:00
|
|
|
this.onlyOnProcessEnvCliCall = false;
|
2016-12-18 19:53:50 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
2018-05-03 10:10:39 +00:00
|
|
|
/**
|
|
|
|
* halts any execution of commands if (process.env.CLI_CALL === false)
|
|
|
|
*/
|
2017-10-12 20:44:34 +00:00
|
|
|
onlyTriggerOnProcessEnvCliCall() {
|
|
|
|
this.onlyOnProcessEnvCliCall = true;
|
|
|
|
}
|
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) {
|
2018-05-03 10:10:39 +00:00
|
|
|
let triggerSubject = this.addTrigger(commandNameArg);
|
|
|
|
this.parseStarted.promise.then(() => {
|
2016-12-18 19:53:50 +00:00
|
|
|
if (this.argv._.indexOf(commandNameArg) === 0) {
|
2018-05-03 10:10:39 +00:00
|
|
|
this.trigger(commandNameArg);
|
2016-06-10 01:48:01 +00:00
|
|
|
}
|
|
|
|
});
|
2018-05-03 10:10:39 +00:00
|
|
|
return triggerSubject;
|
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();
|
2018-05-03 10:10:39 +00:00
|
|
|
if (!this.getTriggerSubject(triggerNameArg)) {
|
|
|
|
this.allTriggerObservablesMap.add({
|
|
|
|
triggerName: triggerNameArg,
|
|
|
|
subject: triggerSubject
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Error(`you can't add a trigger twice`);
|
|
|
|
}
|
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) {
|
2018-05-03 10:10:39 +00:00
|
|
|
let triggerSubject = this.getTriggerSubject(triggerName);
|
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
|
|
|
}
|
2018-05-03 10:10:39 +00:00
|
|
|
getTriggerSubject(triggerName) {
|
|
|
|
const triggerObservableObject = this.allTriggerObservablesMap.find(triggerObservableObjectArg => {
|
|
|
|
return triggerObservableObjectArg.triggerName === triggerName;
|
|
|
|
});
|
|
|
|
if (triggerObservableObject) {
|
|
|
|
return triggerObservableObject.subject;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2018-05-03 10:10:39 +00:00
|
|
|
this.addCommand('help').subscribe(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');
|
2018-05-03 10:10:39 +00:00
|
|
|
this.parseStarted.promise.then(() => {
|
2016-08-26 07:21:42 +00:00
|
|
|
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
|
|
|
/**
|
2018-05-03 10:10:39 +00:00
|
|
|
* adds a trigger that is called when no command is specified
|
2016-08-26 09:52:09 +00:00
|
|
|
*/
|
2016-08-26 07:21:42 +00:00
|
|
|
standardTask() {
|
2018-05-03 10:10:39 +00:00
|
|
|
let standardSubject = this.addTrigger('standardTask');
|
|
|
|
this.parseStarted.promise.then(() => {
|
2016-10-14 22:56:02 +00:00
|
|
|
if (this.argv._.length === 0 && !this.argv.v) {
|
2017-10-12 20:44:34 +00:00
|
|
|
if (this.onlyOnProcessEnvCliCall) {
|
|
|
|
if (process.env.CLI_CALL === 'true') {
|
2018-05-03 10:10:39 +00:00
|
|
|
this.trigger('standardTask');
|
2017-10-12 20:44:34 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-05-03 10:10:39 +00:00
|
|
|
this.trigger('standardTask');
|
2017-10-12 20:44:34 +00:00
|
|
|
}
|
2016-06-10 01:48:01 +00:00
|
|
|
}
|
|
|
|
});
|
2018-05-03 10:10:39 +00:00
|
|
|
return standardSubject;
|
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;
|
2018-05-03 10:10:39 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZ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