smartcli/ts/index.ts

140 lines
3.2 KiB
TypeScript
Raw Normal View History

2015-10-04 21:21:10 +00:00
/// <reference path="typings/tsd.d.ts" />
2015-10-14 18:59:01 +00:00
/// <reference path="./interfaces.ts" />
2015-10-04 21:21:10 +00:00
2015-10-05 21:33:57 +00:00
var path = require("path");
var beautylog = require("beautylog");
var cliff = require("cliff");
var inquirer = require("inquirer");
var argv = require('yargs').argv;
2015-10-04 21:21:10 +00:00
2015-10-05 21:33:57 +00:00
//define the smartcli object
var smartcli:any = {};
2015-10-04 21:21:10 +00:00
2015-10-05 21:33:57 +00:00
//add plugins from above for direct use
smartcli.inquirer = inquirer;
smartcli.cliff = cliff;
smartcli.argv = argv;
2015-10-06 23:07:38 +00:00
/* ------------------------------------------------------------------------------
*----------------------- initial call CLI args -----------------------------
*----------------------------------------------------------------------------- */
// commands
smartcli.checkCommand = function(commandString:string):boolean {
if (argv._.indexOf(commandString) != -1) {
return true
}
return false;
};
smartcli.getCommands = function ():string[] {
return argv._;
};
// options
2015-10-14 18:59:01 +00:00
smartcli.getOption = function(optionName:string):CliOption {
if (argv.hasOwnProperty(optionName)) {
return {
name:optionName,
specified: true,
value: argv[optionName] //we already know from the "if" above that the value is available.
};
2015-10-06 23:16:15 +00:00
}
2015-10-14 18:59:01 +00:00
return {
name:optionName,
specified: false,
value: false
}
2015-10-06 23:16:15 +00:00
};
2015-10-06 23:07:38 +00:00
2015-10-14 18:59:01 +00:00
2015-10-06 23:07:38 +00:00
smartcli.getOptions = function() {
var options = {};
for (var key in argv) {
if (key != "_") {
options['key'] = argv['key'];
}
}
return options;
};
2015-10-05 21:33:57 +00:00
/**
2015-10-14 18:59:01 +00:00
* returns Directory of cwd
* @returns {{path: string}}
2015-10-05 21:33:57 +00:00
*/
2015-10-14 18:59:01 +00:00
smartcli.getCwd = function():Directory {
return {
path: process.cwd()
}
2015-10-05 21:33:57 +00:00
};
2015-10-06 23:07:38 +00:00
/* ------------------------------------------------------------------------------
*----------------------- in program CLI interaction -----------------------------
*----------------------------------------------------------------------------- */
2015-10-05 21:33:57 +00:00
/**
* executes callback with answer to question as argument
* @param questionString the question you want to ask the user
* @param cb the function to execute with answer as param
* @returns {null}
*/
smartcli.getAnswer = function(questionString:string, cb) {
if (typeof questionString != 'string') {
beautylog.error('no question specified');
return null;
}
//make inquirer compatible question object
var question = {
type: "input",
name: "userFeedback",
message: questionString,
validate: function( value ) {
return true;
}
};
inquirer.prompt([question],function(answers){
var answer = answers.userFeedback;
cb(answer);
});
};
/**
*
* @param questionString
* @param choiceOptions
* @param cb
* @returns {null}
*/
smartcli.getChoice = function(questionString:string, choiceOptions:string[], cb) {
if(!Array.isArray(choiceOptions)) {
return null;
}
//make inquirer compatible question object
var question = {
type: "list",
name: "userFeedback",
message: questionString,
choices: choiceOptions,
filter: function( val ) { return val.toLowerCase(); }
};
inquirer.prompt(question,function(answers){
var answer = answers.userFeedback;
cb(answer);
2015-10-04 21:21:10 +00:00
});
2015-10-05 21:33:57 +00:00
2015-10-04 21:21:10 +00:00
};
2015-10-05 21:33:57 +00:00
module.exports = smartcli;