smartcli/dist/smartcli.classes.smartcli.js

112 lines
6.8 KiB
JavaScript

"use strict";
const q = require("q");
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.allCommandDeferredsMap = 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.allCommandDeferredsMap.add({
commandName: definitionArg.commandName,
deferred: done
});
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.allCommandDeferredsMap.find(commandDeferredObjectArg => {
return commandDeferredObjectArg.commandName === commandNameArg;
}).deferred.promise;
}
/**
* triggers a command by name
* @param commandNameArg - the name of the command to trigger
*/
triggerCommandByName(commandNameArg) {
let commandDeferred = this.allCommandDeferredsMap.find(commandDeferredObjectArg => {
return commandDeferredObjectArg.commandName === commandNameArg;
}).deferred;
commandDeferred.resolve();
return commandDeferred.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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjbGkuY2xhc3Nlcy5zbWFydGNsaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y2xpLmNsYXNzZXMuc21hcnRjbGkudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHVCQUFzQjtBQUV0Qiw4Q0FBNkM7QUFFN0MsaUJBQWlCO0FBQ2pCLDZCQUE2QjtBQVE3QjtJQWFJO1FBTEE7O1dBRUc7UUFDSCwyQkFBc0IsR0FBRyxJQUFJLGVBQVMsRUFBMEIsQ0FBQTtRQUc1RCxJQUFJLENBQUMsSUFBSSxHQUFHLE9BQU8sQ0FBQyxLQUFLLENBQUE7UUFDekIsSUFBSSxDQUFDLGFBQWEsR0FBRyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7UUFDOUIsSUFBSSxDQUFDLFlBQVksR0FBRyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDakMsQ0FBQztJQUVEOztPQUVHO0lBQ0gsUUFBUSxDQUFDLE1BQU0sRUFBQyxRQUFRO1FBQ3BCLElBQUksQ0FBQyxJQUFJLEdBQUcsSUFBSSxDQUFDLElBQUksQ0FBQyxLQUFLLENBQUMsTUFBTSxFQUFDLFFBQVEsQ0FBQyxDQUFBO1FBQzVDLE1BQU0sQ0FBQTtJQUNWLENBQUM7SUFFRDs7O09BR0c7SUFDSCxVQUFVLENBQUMsYUFBb0M7UUFDM0MsSUFBSSxJQUFJLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBTyxDQUFBO1FBQ3pCLElBQUksQ0FBQyxzQkFBc0IsQ0FBQyxHQUFHLENBQUM7WUF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