2015-10-04 21:21:10 +00:00
|
|
|
/// <reference path="typings/tsd.d.ts" />
|
2015-11-05 20:43:34 +00:00
|
|
|
/// <reference path="./smartcli.plugins.ts" />
|
|
|
|
/// <reference path="./smartcli.interfaces.ts" />
|
|
|
|
/// <reference path="./smartcli.checks.ts" />
|
|
|
|
/// <reference path="./smartcli.getters.ts" />
|
2015-10-04 21:21:10 +00:00
|
|
|
|
2015-11-05 20:43:34 +00:00
|
|
|
var plugins = smartcliPlugins.init(); //get all the required npm modules under plugins
|
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
|
2015-11-05 20:43:34 +00:00
|
|
|
smartcli.inquirer = plugins.inquirer; //inquirer is for asking questions
|
|
|
|
smartcli.cliff = plugins.cliff; // formats cli output
|
|
|
|
smartcli.argv = plugins.argv; //argv gets initial cli commands and options.
|
2015-10-05 21:33:57 +00:00
|
|
|
|
2015-11-05 20:43:34 +00:00
|
|
|
//init checks. Checks return boolean. That means they can be used as question with an answer of yes or no.
|
2015-10-06 23:07:38 +00:00
|
|
|
|
2015-11-05 20:43:34 +00:00
|
|
|
smartcliChecks.init(); // is defined in smartcli.checks.ts
|
2015-10-06 23:07:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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') {
|
2015-11-05 20:43:34 +00:00
|
|
|
plugins.beautylog.error('no question specified');
|
2015-10-05 21:33:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
//make inquirer compatible question object
|
|
|
|
var question = {
|
|
|
|
type: "input",
|
|
|
|
name: "userFeedback",
|
|
|
|
message: questionString,
|
|
|
|
validate: function( value ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-05 20:43:34 +00:00
|
|
|
plugins.inquirer.prompt([question],function(answers){
|
2015-10-05 21:33:57 +00:00
|
|
|
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(); }
|
|
|
|
};
|
|
|
|
|
2015-11-05 20:43:34 +00:00
|
|
|
plugins.inquirer.prompt(question,function(answers){
|
2015-10-05 21:33:57 +00:00
|
|
|
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;
|