smartcli/dist/smartcli.classes.smartcli.js
2016-10-15 00:56:02 +02:00

95 lines
5.7 KiB
JavaScript

"use strict";
const q = require("q");
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 = q.defer();
this.parseStarted = 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);
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(definitionArg) {
let done = 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 = 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();
return;
}
}
exports.Smartcli = Smartcli;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNv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