2016-06-10 00:27:04 +00:00
|
|
|
import "typings-global";
|
|
|
|
|
2016-06-13 10:57:24 +00:00
|
|
|
import * as plugins from "./smartcli.plugins";
|
2016-08-26 09:52:09 +00:00
|
|
|
import * as interaction from "./smartcli.classes.interaction";
|
|
|
|
|
|
|
|
// import classes
|
|
|
|
import {Objectmap} from "lik";
|
|
|
|
|
|
|
|
// interfaces
|
|
|
|
export interface commandPromiseObject {
|
|
|
|
commandName:string;
|
|
|
|
promise: plugins.q.Promise<any>;
|
|
|
|
};
|
2016-06-10 01:10:21 +00:00
|
|
|
|
2016-06-10 00:27:04 +00:00
|
|
|
export class Smartcli {
|
2016-09-04 15:23:29 +00:00
|
|
|
argv:any;
|
2016-06-10 01:10:21 +00:00
|
|
|
questionsDone;
|
2016-06-10 01:48:01 +00:00
|
|
|
parseStarted;
|
|
|
|
commands;
|
2016-06-10 01:10:21 +00:00
|
|
|
questions;
|
2016-06-10 01:48:01 +00:00
|
|
|
version:string;
|
2016-08-26 09:52:09 +00:00
|
|
|
|
|
|
|
// maps
|
|
|
|
allCommandPromises = new Objectmap<commandPromiseObject>();
|
2016-06-10 01:10:21 +00:00
|
|
|
constructor(){
|
2016-09-04 15:23:29 +00:00
|
|
|
this.argv = plugins.yargs;
|
2016-06-10 01:10:21 +00:00
|
|
|
this.questionsDone = plugins.q.defer();
|
2016-06-10 01:48:01 +00:00
|
|
|
this.parseStarted = plugins.q.defer();
|
2016-08-26 07:21:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* adds an alias, meaning one equals the other in terms of triggering associated commands
|
|
|
|
*/
|
2016-06-10 01:48:01 +00:00
|
|
|
addAlias(keyArg,aliasArg){
|
|
|
|
this.argv = this.argv.alias(keyArg,aliasArg);
|
|
|
|
};
|
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-06-10 01:10:21 +00:00
|
|
|
addCommand(definitionArg:{commandName:string}){
|
|
|
|
let done = plugins.q.defer();
|
2016-06-10 02:13:23 +00:00
|
|
|
this.parseStarted.promise
|
2016-06-10 01:48:01 +00:00
|
|
|
.then(() => {
|
|
|
|
if (this.argv._.indexOf(definitionArg.commandName) == 0) {
|
2016-06-10 02:21:03 +00:00
|
|
|
done.resolve(this.argv);
|
2016-06-10 01:48:01 +00:00
|
|
|
} else {
|
2016-06-13 10:56:42 +00:00
|
|
|
done.reject(this.argv);
|
2016-06-10 01:48:01 +00:00
|
|
|
}
|
|
|
|
});
|
2016-06-22 09:54:12 +00:00
|
|
|
return done.promise;
|
2016-06-10 01:10:21 +00:00
|
|
|
};
|
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:string){
|
|
|
|
return this.allCommandPromises.find(commandPromiseObjectArg => {
|
|
|
|
return commandPromiseObjectArg.commandName === commandNameArg;
|
|
|
|
}).promise;
|
2016-06-10 01:10:21 +00:00
|
|
|
};
|
2016-06-13 10:57:24 +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:{
|
|
|
|
helpText:string
|
|
|
|
}){
|
|
|
|
this.addCommand({
|
|
|
|
commandName:"help"
|
|
|
|
}).then(argvArg => {
|
|
|
|
plugins.beautylog.log(optionsArg.helpText);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* specify version to be printed for -v --version
|
|
|
|
*/
|
2016-06-10 01:10:21 +00:00
|
|
|
addVersion(versionArg:string){
|
2016-06-10 01:48:01 +00:00
|
|
|
this.version = versionArg;
|
2016-06-10 02:13:23 +00:00
|
|
|
this.addAlias("v","version");
|
|
|
|
this.parseStarted.promise
|
2016-06-10 01:48:01 +00:00
|
|
|
.then(() => {
|
2016-06-10 02:13:23 +00:00
|
|
|
if(this.argv.v){
|
|
|
|
console.log(this.version);
|
|
|
|
}
|
2016-06-10 01:48:01 +00:00
|
|
|
})
|
2016-08-26 09:52:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns promise that is resolved when no commands are specified
|
|
|
|
*/
|
2016-06-10 01:48:01 +00:00
|
|
|
standardTask(){
|
|
|
|
let done = plugins.q.defer();
|
2016-06-10 02:13:23 +00:00
|
|
|
this.parseStarted.promise
|
2016-06-10 01:48:01 +00:00
|
|
|
.then(() => {
|
2016-06-10 02:32:11 +00:00
|
|
|
if(this.argv._.length == 0 && !this.argv.v){
|
2016-06-16 00:58:45 +00:00
|
|
|
done.resolve(this.argv);
|
2016-06-10 01:48:01 +00:00
|
|
|
} else {
|
2016-06-16 00:58:45 +00:00
|
|
|
done.reject(this.argv);
|
2016-06-10 01:48:01 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
return done.promise;
|
2016-06-10 01:10:21 +00:00
|
|
|
}
|
2016-09-04 15:32:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* start the process of evaluating commands
|
|
|
|
*/
|
2016-06-10 01:10:21 +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-06-10 01:10:21 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 12:01:06 +00:00
|
|
|
}
|