add Tests and improve TypeScript organization
This commit is contained in:
parent
5611ad03aa
commit
5444f1d3c0
356
index.js
356
index.js
@ -1,113 +1,253 @@
|
||||
/// <reference path="./index.ts" />
|
||||
var smartcliPlugins;
|
||||
(function (smartcliPlugins) {
|
||||
var plugins = {};
|
||||
function init() {
|
||||
plugins.path = require("path");
|
||||
plugins.beautylog = require("beautylog")("os");
|
||||
plugins.cliff = require("cliff");
|
||||
plugins.inquirer = require("inquirer");
|
||||
plugins.smartparam = require("smartparam");
|
||||
plugins.argv = require('yargs').argv;
|
||||
return plugins;
|
||||
}
|
||||
smartcliPlugins.init = init;
|
||||
})(smartcliPlugins || (smartcliPlugins = {}));
|
||||
/// <reference path="index.ts" />
|
||||
/// <reference path="./index.ts" />
|
||||
var SmartcliChecks;
|
||||
(function (SmartcliChecks) {
|
||||
function init() {
|
||||
/**
|
||||
* all functions in smartcli.check return a boolean
|
||||
* @type {{}}
|
||||
*/
|
||||
smartcli.check = {};
|
||||
/**
|
||||
* checks for a special command string and returns true if found.
|
||||
* @param commandString
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.command = function (commandString) {
|
||||
if (plugins.argv._.indexOf(commandString) == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
/**
|
||||
* checks if a command is present, returns true if yes, false if no.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.commandPresence = function () {
|
||||
if (plugins.argv._.length > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
/**
|
||||
* checks for an special command argument at a certain position, returns true if matches, returns false if not
|
||||
* @param level
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.commandArgument = function (commandArgumentString, level) {
|
||||
if (level === void 0) { level = 1; }
|
||||
if (smartcli.check.commandArgumentPresence(level) && (plugins.argv._[level] == commandArgumentString)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
smartcli.check.commandArgumentPresence = function (level) {
|
||||
if (level === void 0) { level = 1; }
|
||||
if (plugins.argv._.length >= (level + 1)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
/**
|
||||
* checks for a specific option string, returns true if yes, returns false if no
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.option = function (optionString) {
|
||||
if (plugins.smartparam.exists(plugins.argv, optionString)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
smartcli.check.optionPresence = function () {
|
||||
if (plugins.argv.indexOf() != -1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
SmartcliChecks.init = init;
|
||||
})(SmartcliChecks || (SmartcliChecks = {}));
|
||||
/// <reference path="./index.ts" />
|
||||
var SmartcliGetters;
|
||||
(function (SmartcliGetters) {
|
||||
function init() {
|
||||
smartcli.get = {};
|
||||
/**
|
||||
*
|
||||
* @param commandString
|
||||
* @returns {{specified: boolean, name: any, arguments: CliCommandArgument}}
|
||||
*/
|
||||
smartcli.get.command = function () {
|
||||
var cliCommand = {
|
||||
specified: smartcli.check.commandPresence(),
|
||||
name: plugins.argv._[1],
|
||||
arguments: smartcli.get.commandArguments(1)
|
||||
};
|
||||
return cliCommand;
|
||||
};
|
||||
/**
|
||||
* gets the second or higher value of plugins.argv._ if specified and returns it as commandArgument
|
||||
* @param argumentLevel
|
||||
* @returns {{specified: (boolean|boolean), name}}
|
||||
*/
|
||||
smartcli.get.commandArgument = function (argumentLevel) {
|
||||
var commandArgument = {
|
||||
specified: false,
|
||||
name: "undefined",
|
||||
level: argumentLevel
|
||||
};
|
||||
if (argumentLevel < 1) {
|
||||
plugins.beautylog.error("smartcli.get.argument cannot be invoked with an argumentLevel smaller than 1");
|
||||
return commandArgument;
|
||||
}
|
||||
if (smartcli.check.commandArgumentPresence(argumentLevel)) {
|
||||
commandArgument.specified = true;
|
||||
commandArgument.name = plugins.argv._[argumentLevel];
|
||||
}
|
||||
return commandArgument;
|
||||
};
|
||||
/**
|
||||
*
|
||||
* @returns {CliCommandArgument[]}
|
||||
*/
|
||||
smartcli.get.commandArgs = function () {
|
||||
var commandArgs = [];
|
||||
for (var command in plugins.argv._)
|
||||
return commandArgs;
|
||||
};
|
||||
/**
|
||||
* returns complete command array
|
||||
* @returns {any}
|
||||
*/
|
||||
smartcli.get.commandArray = function () {
|
||||
return plugins.argv._;
|
||||
};
|
||||
/**
|
||||
* returns a cli option
|
||||
* @param optionName
|
||||
* @returns {CliOption}
|
||||
*/
|
||||
smartcli.get.option = function (optionName) {
|
||||
var cliOption = {
|
||||
name: optionName,
|
||||
specified: false,
|
||||
value: false
|
||||
};
|
||||
if (plugins.argv.hasOwnProperty(optionName)) {
|
||||
cliOption.name = optionName;
|
||||
cliOption.specified = true;
|
||||
cliOption.value = plugins.argv[optionName]; //we already know from the "if" above that the value is available.
|
||||
}
|
||||
return cliOption;
|
||||
};
|
||||
smartcli.get.options = function () {
|
||||
var options = {};
|
||||
for (var key in plugins.argv) {
|
||||
if (key != "_") {
|
||||
options['key'] = plugins.argv['key'];
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
/**
|
||||
* returns Directory of cwd
|
||||
* @returns {{path: string}}
|
||||
*/
|
||||
smartcli.get.cwd = function () {
|
||||
return {
|
||||
path: process.cwd()
|
||||
};
|
||||
};
|
||||
}
|
||||
SmartcliGetters.init = init;
|
||||
})(SmartcliGetters || (SmartcliGetters = {}));
|
||||
/// <reference path="./index.ts" />
|
||||
var SmartcliInteraction;
|
||||
(function (SmartcliInteraction) {
|
||||
function init() {
|
||||
smartcli.interaction = {};
|
||||
/**
|
||||
* 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.interaction.getAnswer = function (questionString, cb) {
|
||||
if (typeof questionString != 'string') {
|
||||
plugins.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;
|
||||
}
|
||||
};
|
||||
plugins.inquirer.prompt([question], function (answers) {
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
};
|
||||
/**
|
||||
*
|
||||
* @param questionString
|
||||
* @param choiceOptions
|
||||
* @param cb
|
||||
* @returns {null}
|
||||
*/
|
||||
smartcli.interaction.getChoice = function (questionString, choiceOptions, 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(); }
|
||||
};
|
||||
plugins.inquirer.prompt(question, function (answers) {
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
};
|
||||
}
|
||||
SmartcliInteraction.init = init;
|
||||
})(SmartcliInteraction || (SmartcliInteraction = {}));
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
/// <reference path="./interfaces.ts" />
|
||||
var path = require("path");
|
||||
var beautylog = require("beautylog");
|
||||
var cliff = require("cliff");
|
||||
var inquirer = require("inquirer");
|
||||
var argv = require('yargs').argv;
|
||||
/// <reference path="./smartcli.plugins.ts" />
|
||||
/// <reference path="./smartcli.interfaces.ts" />
|
||||
/// <reference path="./smartcli.checks.ts" />
|
||||
/// <reference path="./smartcli.getters.ts" />
|
||||
/// <reference path="./smartcli.interaction.ts" />
|
||||
var plugins = smartcliPlugins.init(); //get all the required npm modules under plugins
|
||||
//define the smartcli object
|
||||
var smartcli = {};
|
||||
//add plugins from above for direct use
|
||||
smartcli.inquirer = inquirer;
|
||||
smartcli.cliff = cliff;
|
||||
smartcli.argv = argv;
|
||||
/* ------------------------------------------------------------------------------
|
||||
*----------------------- initial call CLI args -----------------------------
|
||||
*----------------------------------------------------------------------------- */
|
||||
// commands
|
||||
smartcli.checkCommand = function (commandString) {
|
||||
if (argv._.indexOf(commandString) != -1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
smartcli.getCommands = function () {
|
||||
return argv._;
|
||||
};
|
||||
// options
|
||||
smartcli.getOption = function (optionName) {
|
||||
if (argv.hasOwnProperty(optionName)) {
|
||||
return {
|
||||
name: optionName,
|
||||
specified: true,
|
||||
value: argv[optionName] //we already know from the "if" above that the value is available.
|
||||
};
|
||||
}
|
||||
return {
|
||||
name: optionName,
|
||||
specified: false,
|
||||
value: false
|
||||
};
|
||||
};
|
||||
smartcli.getOptions = function () {
|
||||
var options = {};
|
||||
for (var key in argv) {
|
||||
if (key != "_") {
|
||||
options['key'] = argv['key'];
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
/**
|
||||
* returns Directory of cwd
|
||||
* @returns {{path: string}}
|
||||
*/
|
||||
smartcli.getCwd = function () {
|
||||
return {
|
||||
path: process.cwd()
|
||||
};
|
||||
};
|
||||
/* ------------------------------------------------------------------------------
|
||||
*----------------------- in program CLI interaction -----------------------------
|
||||
*----------------------------------------------------------------------------- */
|
||||
/**
|
||||
* 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, 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, choiceOptions, 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);
|
||||
});
|
||||
};
|
||||
module.exports = smartcli;
|
||||
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.
|
||||
//init checks. Checks return boolean. That means they can be used as question with an answer of yes or no.
|
||||
SmartcliChecks.init(); // is defined in smartcli.checks.ts
|
||||
SmartcliGetters.init(); // is defined in smartcli.getters.ts
|
||||
SmartcliInteraction.init(); // is defined in smartcli.interaction.ts
|
||||
module.exports = smartcli; // expose smartcli to outside world
|
||||
|
@ -4,7 +4,8 @@
|
||||
"description": "nodejs wrapper for CLI related tasks",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "(cd ts/compile && gulp)",
|
||||
"test": "(cd ts/compile && gulp) && (node test.js jazz jam --awesome --silent)",
|
||||
"mtest": "(cd ts/compile && gulp) && (node test.js jazz jam --awesome)",
|
||||
"devTest": "(npm test) && (node test.js --test true)",
|
||||
"reinstall": "(rm -r node_modules && npm install)",
|
||||
"release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)",
|
||||
@ -29,7 +30,7 @@
|
||||
"beautylog": "1.0.2",
|
||||
"cliff": "^0.1.10",
|
||||
"inquirer": "^0.11.0",
|
||||
"smartparam": "0.0.5",
|
||||
"smartparam": "0.0.7",
|
||||
"yargs": "^3.29.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
127
test.js
127
test.js
@ -1,45 +1,112 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var smartcli = require("./index.js");
|
||||
var bl = require("beautylog");
|
||||
var bl = require("beautylog")("os");
|
||||
bl.log('now starting Test');
|
||||
bl.log('starting with initial CLI commands and options');
|
||||
var commandsString = 'You specified the following commands:';
|
||||
var commands = smartcli.getCommands();
|
||||
var commands = smartcli.get.commandArray();
|
||||
for (var key in commands) {
|
||||
commandsString = commandsString + ' ' + commands[key];
|
||||
}
|
||||
bl.log(commandsString);
|
||||
var getCwdTest = function () {
|
||||
console.log('The current directory is: ' + smartcli.getCwd().path);
|
||||
};
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- CHECKS TESTS --------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
/**
|
||||
*
|
||||
*/
|
||||
var checkCommandTest = function () {
|
||||
if (smartcli.checkCommand('jazz')) {
|
||||
bl.log('One of your commands is jazz');
|
||||
if (smartcli.check.command('jazz')) {
|
||||
bl.success('One of your commands is jazz. It is supposed to be there. Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.log('None of your commands is jazz');
|
||||
bl.error('None of your commands is jazz. You need to check this');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.command('punk')) {
|
||||
bl.success('None of your commands is punk. It is not supposed to be there. Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('One of your commands seems to be punk. Something is wrong here');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
var getOptionTest = function () {
|
||||
console.log('We now test for option --test');
|
||||
console.log(smartcli.getOption('test'));
|
||||
};
|
||||
var checkOptionTest = function () {
|
||||
};
|
||||
var getAnswerTest = function () {
|
||||
smartcli.getAnswer('How do you feel?', function (answer) {
|
||||
console.log('The answer is: ' + answer);
|
||||
getChoiceTest();
|
||||
});
|
||||
};
|
||||
var getChoiceTest = function () {
|
||||
smartcli.getChoice('What music do you like to hear?', ['Jazz', 'Blues', 'Classical'], function (answer) {
|
||||
console.log('The answer is: ' + answer);
|
||||
});
|
||||
};
|
||||
//starting command tests
|
||||
getCwdTest();
|
||||
checkCommandTest();
|
||||
getOptionTest();
|
||||
//starting first interaction test (the other tests are then started via callbacks)
|
||||
getAnswerTest();
|
||||
var checkCommandPresenceTest = function () {
|
||||
if (smartcli.check.commandPresence()) {
|
||||
bl.success('There are commands present, like supposed to. Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There do not seem to be any commands present... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandPresenceTest();
|
||||
var checkCommandArgumentTest = function () {
|
||||
if (smartcli.check.commandArgument("jam", 1)) {
|
||||
bl.success('There is a level 1 argument! Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There seems to be no level 1 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.commandArgument("session", 2)) {
|
||||
bl.success('There is no level 2 argument with the name of "session"! Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There seems to be a level 2 argument with the name of "session"! This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandArgumentTest();
|
||||
var checkCommandArgumentPresenceTest = function () {
|
||||
if (smartcli.check.commandArgumentPresence(1)) {
|
||||
bl.success('There is a level 1 argument! Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There seems to be no level 1 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.commandArgumentPresence(2)) {
|
||||
bl.success('There is no level 2 argument! Perfect!');
|
||||
}
|
||||
else {
|
||||
bl.error('There seems to be a level 2 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandArgumentPresenceTest();
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- GETTERS TESTS -------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
var getCwdTest = function () {
|
||||
bl.info('The current directory is: ' + smartcli.get.cwd().path);
|
||||
};
|
||||
getCwdTest();
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- INTERACTION TESTS ---------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
var interactionGetAnswerTest = function () {
|
||||
smartcli.interaction.getAnswer('How do you feel?', function (answer) {
|
||||
console.log('The answer is: ' + answer);
|
||||
interactionGetChoiceTest();
|
||||
});
|
||||
};
|
||||
var interactionGetChoiceTest = function () {
|
||||
smartcli.interaction.getChoice('What music do you like to hear?', ['Jazz', 'Blues', 'Classical'], function (answer) {
|
||||
console.log('The answer is: ' + answer);
|
||||
endTests();
|
||||
});
|
||||
};
|
||||
var endTests = function () {
|
||||
bl.ok("No more tests!");
|
||||
bl.success("Tests completed successfully!");
|
||||
};
|
||||
if (!smartcli.check.option("silent")) {
|
||||
interactionGetAnswerTest();
|
||||
}
|
||||
else {
|
||||
bl.info("--silent option is specified, thus we are not running interaction tests.");
|
||||
endTests();
|
||||
}
|
||||
;
|
||||
|
77
ts/index.ts
77
ts/index.ts
@ -3,6 +3,7 @@
|
||||
/// <reference path="./smartcli.interfaces.ts" />
|
||||
/// <reference path="./smartcli.checks.ts" />
|
||||
/// <reference path="./smartcli.getters.ts" />
|
||||
/// <reference path="./smartcli.interaction.ts" />
|
||||
|
||||
var plugins = smartcliPlugins.init(); //get all the required npm modules under plugins
|
||||
|
||||
@ -16,77 +17,9 @@ smartcli.argv = plugins.argv; //argv gets initial cli commands and options.
|
||||
|
||||
//init checks. Checks return boolean. That means they can be used as question with an answer of yes or no.
|
||||
|
||||
smartcliChecks.init(); // is defined in smartcli.checks.ts
|
||||
SmartcliChecks.init(); // is defined in smartcli.checks.ts
|
||||
SmartcliGetters.init(); // is defined in smartcli.getters.ts
|
||||
SmartcliInteraction.init(); // is defined in smartcli.interaction.ts
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------
|
||||
*----------------------- in program CLI interaction -----------------------------
|
||||
*----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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') {
|
||||
plugins.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;
|
||||
}
|
||||
};
|
||||
|
||||
plugins.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(); }
|
||||
};
|
||||
|
||||
plugins.inquirer.prompt(question,function(answers){
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
module.exports = smartcli;
|
||||
module.exports = smartcli; // expose smartcli to outside world
|
||||
|
@ -1,48 +1,66 @@
|
||||
/// <reference path="./index.ts" />
|
||||
module smartcliChecks {
|
||||
module SmartcliChecks {
|
||||
export function init() {
|
||||
/**
|
||||
* all functions in smartcli.check return a boolean
|
||||
* @type {{}}
|
||||
*/
|
||||
smartcli.check = {};
|
||||
|
||||
/**
|
||||
* checks for a special command string and returns true if found.
|
||||
* @param commandString
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.checkCommand = function(commandString:string):boolean {
|
||||
smartcli.check.command = function(commandString:string):boolean {
|
||||
if (plugins.argv._.indexOf(commandString) == 0) {
|
||||
return true
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
smartcli.checkCommandArgument = function(level:number):boolean {
|
||||
if(plugins.argv._.length == (level + 1)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* checks if a command is present, returns true if yes, false if no.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.checkCommandPresence = function():boolean {
|
||||
if(plugins.argv._.length < 0){
|
||||
smartcli.check.commandPresence = function():boolean {
|
||||
if(plugins.argv._.length > 0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* checks for an special command argument at a certain position, returns true if matches, returns false if not
|
||||
* @param level
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.check.commandArgument = function(commandArgumentString:string,level:number = 1):boolean {
|
||||
if(smartcli.check.commandArgumentPresence(level) && (plugins.argv._[level] == commandArgumentString )) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
smartcli.check.commandArgumentPresence = function(level:number = 1) {
|
||||
if(plugins.argv._.length >= (level + 1)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* checks for a specific option string, returns true if yes, returns false if no
|
||||
* @returns {boolean}
|
||||
*/
|
||||
smartcli.checkOption = function(optionString):boolean {
|
||||
smartcli.check.option = function(optionString):boolean {
|
||||
if(plugins.smartparam.exists(plugins.argv, optionString)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
smartcli.checkOptionsPresence = function():boolean {
|
||||
smartcli.check.optionPresence = function():boolean {
|
||||
if (plugins.argv.indexOf() != -1) {
|
||||
return true
|
||||
}
|
||||
|
@ -1,50 +1,83 @@
|
||||
/// <reference path="./index.ts" />
|
||||
module smartcliGetters {
|
||||
module SmartcliGetters {
|
||||
export function init() {
|
||||
smartcli.getCommand = function(commandString):CliCommand {
|
||||
smartcli.get = {};
|
||||
/**
|
||||
*
|
||||
* @param commandString
|
||||
* @returns {{specified: boolean, name: any, arguments: CliCommandArgument}}
|
||||
*/
|
||||
smartcli.get.command = function():CliCommand {
|
||||
var cliCommand = {
|
||||
specified: smartcli.checkCommand(commandString),
|
||||
name: commandString,
|
||||
arguments: smartcli.getCommandArgument(1)
|
||||
specified: smartcli.check.commandPresence(),
|
||||
name: plugins.argv._[1],
|
||||
arguments: smartcli.get.commandArguments(1)
|
||||
}
|
||||
return cliCommand;
|
||||
};
|
||||
|
||||
smartcli.getCommandArgument = function(argumentLevel):CommandArgument {
|
||||
var commandArgument = {
|
||||
specified
|
||||
/**
|
||||
* gets the second or higher value of plugins.argv._ if specified and returns it as commandArgument
|
||||
* @param argumentLevel
|
||||
* @returns {{specified: (boolean|boolean), name}}
|
||||
*/
|
||||
smartcli.get.commandArgument = function(argumentLevel):CliCommandArgument {
|
||||
var commandArgument:CliCommandArgument = {
|
||||
specified: false,
|
||||
name: "undefined",
|
||||
level:argumentLevel
|
||||
};
|
||||
if(argumentLevel < 1) {
|
||||
plugins.beautylog.error("smartcli.get.argument cannot be invoked with an argumentLevel smaller than 1");
|
||||
return commandArgument;
|
||||
}
|
||||
if(smartcli.check.commandArgumentPresence(argumentLevel)) {
|
||||
commandArgument.specified = true;
|
||||
commandArgument.name = plugins.argv._[argumentLevel];
|
||||
}
|
||||
return commandArgument;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns complete command object
|
||||
*
|
||||
* @returns {CliCommandArgument[]}
|
||||
*/
|
||||
smartcli.get.commandArgs = function():CliCommandArgument[] {
|
||||
var commandArgs:CliCommandArgument[] = [];
|
||||
for (var command in plugins.argv._)
|
||||
return commandArgs;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns complete command array
|
||||
* @returns {any}
|
||||
*/
|
||||
smartcli.getCommands = function ():string[] {
|
||||
smartcli.get.commandArray = function ():string[] {
|
||||
return plugins.argv._;
|
||||
};
|
||||
|
||||
|
||||
// options
|
||||
smartcli.getOption = function(optionName:string):CliOption {
|
||||
if (plugins.argv.hasOwnProperty(optionName)) {
|
||||
return {
|
||||
name:optionName,
|
||||
specified: true,
|
||||
value: plugins.argv[optionName] //we already know from the "if" above that the value is available.
|
||||
};
|
||||
|
||||
}
|
||||
return {
|
||||
/**
|
||||
* returns a cli option
|
||||
* @param optionName
|
||||
* @returns {CliOption}
|
||||
*/
|
||||
smartcli.get.option = function(optionName:string):CliOption {
|
||||
var cliOption:CliOption = {
|
||||
name:optionName,
|
||||
specified: false,
|
||||
value: false
|
||||
};
|
||||
if (plugins.argv.hasOwnProperty(optionName)) {
|
||||
cliOption.name = optionName;
|
||||
cliOption.specified = true;
|
||||
cliOption.value = plugins.argv[optionName] //we already know from the "if" above that the value is available.
|
||||
}
|
||||
return cliOption;
|
||||
};
|
||||
|
||||
|
||||
smartcli.getOptions = function() {
|
||||
smartcli.get.options = function() {
|
||||
var options = {};
|
||||
for (var key in plugins.argv) {
|
||||
if (key != "_") {
|
||||
@ -58,7 +91,7 @@ module smartcliGetters {
|
||||
* returns Directory of cwd
|
||||
* @returns {{path: string}}
|
||||
*/
|
||||
smartcli.getCwd = function():Directory {
|
||||
smartcli.get.cwd = function():Directory {
|
||||
return {
|
||||
path: process.cwd()
|
||||
}
|
||||
|
61
ts/smartcli.interaction.ts
Normal file
61
ts/smartcli.interaction.ts
Normal file
@ -0,0 +1,61 @@
|
||||
/// <reference path="./index.ts" />
|
||||
module SmartcliInteraction {
|
||||
export function init(){
|
||||
|
||||
smartcli.interaction = {};
|
||||
/**
|
||||
* 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.interaction.getAnswer = function(questionString:string, cb) {
|
||||
if (typeof questionString != 'string') {
|
||||
plugins.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;
|
||||
}
|
||||
};
|
||||
|
||||
plugins.inquirer.prompt([question],function(answers){
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param questionString
|
||||
* @param choiceOptions
|
||||
* @param cb
|
||||
* @returns {null}
|
||||
*/
|
||||
smartcli.interaction.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(); }
|
||||
};
|
||||
|
||||
plugins.inquirer.prompt(question,function(answers){
|
||||
var answer = answers.userFeedback;
|
||||
cb(answer);
|
||||
});
|
||||
|
||||
};
|
||||
}
|
||||
}
|
@ -12,13 +12,12 @@ interface Directory {
|
||||
interface CliCommand {
|
||||
specified: boolean;
|
||||
name: string;
|
||||
second: string;
|
||||
third: string;
|
||||
fourth: string;
|
||||
arguments:string[];
|
||||
}
|
||||
|
||||
interface CliCommandArgument {
|
||||
specified:boolean;
|
||||
name:string;
|
||||
level:number;
|
||||
}
|
||||
|
||||
|
145
ts/test.ts
145
ts/test.ts
@ -1,58 +1,127 @@
|
||||
/// <reference path="typings/tsd.d.ts" />
|
||||
var smartcli = require("./index.js");
|
||||
var bl = require("beautylog");
|
||||
var bl = require("beautylog")("os");
|
||||
|
||||
bl.log('now starting Test');
|
||||
bl.log('starting with initial CLI commands and options');
|
||||
|
||||
var commandsString:string = 'You specified the following commands:';
|
||||
var commands = smartcli.getCommands();
|
||||
var commands = smartcli.get.commandArray();
|
||||
for (var key in commands) {
|
||||
commandsString = commandsString + ' ' + commands[key];
|
||||
}
|
||||
bl.log(commandsString);
|
||||
|
||||
|
||||
var getCwdTest = function(){
|
||||
console.log('The current directory is: ' + smartcli.getCwd().path);
|
||||
};
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- CHECKS TESTS --------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
var checkCommandTest = function() {
|
||||
if (smartcli.checkCommand('jazz')) {
|
||||
bl.log('One of your commands is jazz');
|
||||
if (smartcli.check.command('jazz')) {
|
||||
bl.success('One of your commands is jazz. It is supposed to be there. Perfect!');
|
||||
} else {
|
||||
bl.log('None of your commands is jazz');
|
||||
bl.error('None of your commands is jazz. You need to check this');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.command('punk')) {
|
||||
bl.success('None of your commands is punk. It is not supposed to be there. Perfect!');
|
||||
} else {
|
||||
bl.error('One of your commands seems to be punk. Something is wrong here');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
var getOptionTest = function() {
|
||||
console.log('We now test for option --test')
|
||||
console.log(smartcli.getOption('test'));
|
||||
}
|
||||
|
||||
var checkOptionTest = function() {
|
||||
|
||||
};
|
||||
|
||||
var getAnswerTest = function() {
|
||||
smartcli.getAnswer('How do you feel?',function(answer){
|
||||
console.log('The answer is: ' + answer);
|
||||
getChoiceTest();
|
||||
});
|
||||
};
|
||||
|
||||
var getChoiceTest = function() {
|
||||
smartcli.getChoice('What music do you like to hear?',['Jazz','Blues','Classical'],function(answer){
|
||||
console.log('The answer is: ' + answer);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
//starting command tests
|
||||
getCwdTest();
|
||||
checkCommandTest();
|
||||
getOptionTest();
|
||||
|
||||
//starting first interaction test (the other tests are then started via callbacks)
|
||||
getAnswerTest();
|
||||
|
||||
var checkCommandPresenceTest = function() {
|
||||
if (smartcli.check.commandPresence()) {
|
||||
bl.success('There are commands present, like supposed to. Perfect!');
|
||||
} else {
|
||||
bl.error('There do not seem to be any commands present... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandPresenceTest();
|
||||
|
||||
|
||||
var checkCommandArgumentTest = function() {
|
||||
if (smartcli.check.commandArgument("jam",1)) {
|
||||
bl.success('There is a level 1 argument! Perfect!');
|
||||
} else {
|
||||
bl.error('There seems to be no level 1 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.commandArgument("session",2)) {
|
||||
bl.success('There is no level 2 argument with the name of "session"! Perfect!');
|
||||
} else {
|
||||
bl.error('There seems to be a level 2 argument with the name of "session"! This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandArgumentTest();
|
||||
|
||||
var checkCommandArgumentPresenceTest = function() {
|
||||
if (smartcli.check.commandArgumentPresence(1)) {
|
||||
bl.success('There is a level 1 argument! Perfect!');
|
||||
} else {
|
||||
bl.error('There seems to be no level 1 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
if (!smartcli.check.commandArgumentPresence(2)) {
|
||||
bl.success('There is no level 2 argument! Perfect!');
|
||||
} else {
|
||||
bl.error('There seems to be a level 2 argument... This is wrong');
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
checkCommandArgumentPresenceTest();
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- GETTERS TESTS -------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
var getCwdTest = function(){
|
||||
bl.info('The current directory is: ' + smartcli.get.cwd().path);
|
||||
};
|
||||
getCwdTest();
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- INTERACTION TESTS ---------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
|
||||
var interactionGetAnswerTest = function() {
|
||||
smartcli.interaction.getAnswer('How do you feel?',function(answer){
|
||||
console.log('The answer is: ' + answer);
|
||||
interactionGetChoiceTest();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
||||
var interactionGetChoiceTest = function() {
|
||||
smartcli.interaction.getChoice('What music do you like to hear?',['Jazz','Blues','Classical'],function(answer){
|
||||
console.log('The answer is: ' + answer);
|
||||
endTests();
|
||||
});
|
||||
};
|
||||
|
||||
var endTests = function() {
|
||||
bl.ok("No more tests!");
|
||||
bl.success("Tests completed successfully!");
|
||||
};
|
||||
|
||||
if(!smartcli.check.option("silent")){
|
||||
interactionGetAnswerTest();
|
||||
} else {
|
||||
bl.info("--silent option is specified, thus we are not running interaction tests.");
|
||||
endTests();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user