smartcli/dist/smartcli.classes.smartcli.js

102 lines
5.9 KiB
JavaScript

"use strict";
require("typings-global");
const plugins = require("./smartcli.plugins");
// import classes
const lik_1 = require("lik");
;
class Smartcli {
constructor() {
// maps
this.allCommandPromises = new lik_1.Objectmap();
this.argv = plugins.yargs;
this.questionsDone = plugins.q.defer();
this.parseStarted = plugins.q.defer();
}
;
/**
* adds an alias, meaning one equals the other in terms of triggering associated commands
*/
addAlias(keyArg, aliasArg) {
this.argv = this.argv.alias(keyArg, aliasArg);
}
;
/**
* 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) {
let done = plugins.q.defer();
this.parseStarted.promise
.then(() => {
if (this.argv._.indexOf(definitionArg.commandName) == 0) {
done.resolve(this.argv);
}
else {
done.reject(this.argv);
}
});
return done.promise;
}
;
/**
* gets a Promise for a command word
*/
getCommandPromiseByName(commandNameArg) {
return this.allCommandPromises.find(commandPromiseObjectArg => {
return commandPromiseObjectArg.commandName === commandNameArg;
}).promise;
}
;
/**
* allows to specify help text to be printed above the rest of the help text
*/
addHelp(optionsArg) {
this.addCommand({
commandName: "help"
}).then(argvArg => {
plugins.beautylog.log(optionsArg.helpText);
});
}
;
/**
* specify version to be printed for -v --version
*/
addVersion(versionArg) {
this.version = versionArg;
this.addAlias("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 = plugins.q.defer();
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();
}
}
exports.Smartcli = Smartcli;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5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