add Tests and improve TypeScript organization
This commit is contained in:
parent
5611ad03aa
commit
5444f1d3c0
224
index.js
224
index.js
@ -1,50 +1,165 @@
|
|||||||
|
/// <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" />
|
||||||
/// <reference path="typings/tsd.d.ts" />
|
/// <reference path="./index.ts" />
|
||||||
/// <reference path="./interfaces.ts" />
|
var SmartcliChecks;
|
||||||
var path = require("path");
|
(function (SmartcliChecks) {
|
||||||
var beautylog = require("beautylog");
|
function init() {
|
||||||
var cliff = require("cliff");
|
/**
|
||||||
var inquirer = require("inquirer");
|
* all functions in smartcli.check return a boolean
|
||||||
var argv = require('yargs').argv;
|
* @type {{}}
|
||||||
//define the smartcli object
|
*/
|
||||||
var smartcli = {};
|
smartcli.check = {};
|
||||||
//add plugins from above for direct use
|
/**
|
||||||
smartcli.inquirer = inquirer;
|
* checks for a special command string and returns true if found.
|
||||||
smartcli.cliff = cliff;
|
* @param commandString
|
||||||
smartcli.argv = argv;
|
* @returns {boolean}
|
||||||
/* ------------------------------------------------------------------------------
|
*/
|
||||||
*----------------------- initial call CLI args -----------------------------
|
smartcli.check.command = function (commandString) {
|
||||||
*----------------------------------------------------------------------------- */
|
if (plugins.argv._.indexOf(commandString) == 0) {
|
||||||
// commands
|
|
||||||
smartcli.checkCommand = function (commandString) {
|
|
||||||
if (argv._.indexOf(commandString) != -1) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
smartcli.getCommands = function () {
|
/**
|
||||||
return argv._;
|
* 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;
|
||||||
};
|
};
|
||||||
// options
|
/**
|
||||||
smartcli.getOption = function (optionName) {
|
* checks for an special command argument at a certain position, returns true if matches, returns false if not
|
||||||
if (argv.hasOwnProperty(optionName)) {
|
* @param level
|
||||||
return {
|
* @returns {boolean}
|
||||||
name: optionName,
|
*/
|
||||||
specified: true,
|
smartcli.check.commandArgument = function (commandArgumentString, level) {
|
||||||
value: argv[optionName] //we already know from the "if" above that the value is available.
|
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;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {
|
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,
|
name: optionName,
|
||||||
specified: false,
|
specified: false,
|
||||||
value: 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 = {};
|
var options = {};
|
||||||
for (var key in argv) {
|
for (var key in plugins.argv) {
|
||||||
if (key != "_") {
|
if (key != "_") {
|
||||||
options['key'] = argv['key'];
|
options['key'] = plugins.argv['key'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return options;
|
return options;
|
||||||
@ -53,23 +168,28 @@ smartcli.getOptions = function () {
|
|||||||
* returns Directory of cwd
|
* returns Directory of cwd
|
||||||
* @returns {{path: string}}
|
* @returns {{path: string}}
|
||||||
*/
|
*/
|
||||||
smartcli.getCwd = function () {
|
smartcli.get.cwd = function () {
|
||||||
return {
|
return {
|
||||||
path: process.cwd()
|
path: process.cwd()
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
/* ------------------------------------------------------------------------------
|
}
|
||||||
*----------------------- in program CLI interaction -----------------------------
|
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
|
* executes callback with answer to question as argument
|
||||||
* @param questionString the question you want to ask the user
|
* @param questionString the question you want to ask the user
|
||||||
* @param cb the function to execute with answer as param
|
* @param cb the function to execute with answer as param
|
||||||
* @returns {null}
|
* @returns {null}
|
||||||
*/
|
*/
|
||||||
smartcli.getAnswer = function (questionString, cb) {
|
smartcli.interaction.getAnswer = function (questionString, cb) {
|
||||||
if (typeof questionString != 'string') {
|
if (typeof questionString != 'string') {
|
||||||
beautylog.error('no question specified');
|
plugins.beautylog.error('no question specified');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
//make inquirer compatible question object
|
//make inquirer compatible question object
|
||||||
@ -81,7 +201,7 @@ smartcli.getAnswer = function (questionString, cb) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
inquirer.prompt([question], function (answers) {
|
plugins.inquirer.prompt([question], function (answers) {
|
||||||
var answer = answers.userFeedback;
|
var answer = answers.userFeedback;
|
||||||
cb(answer);
|
cb(answer);
|
||||||
});
|
});
|
||||||
@ -93,7 +213,7 @@ smartcli.getAnswer = function (questionString, cb) {
|
|||||||
* @param cb
|
* @param cb
|
||||||
* @returns {null}
|
* @returns {null}
|
||||||
*/
|
*/
|
||||||
smartcli.getChoice = function (questionString, choiceOptions, cb) {
|
smartcli.interaction.getChoice = function (questionString, choiceOptions, cb) {
|
||||||
if (!Array.isArray(choiceOptions)) {
|
if (!Array.isArray(choiceOptions)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -105,9 +225,29 @@ smartcli.getChoice = function (questionString, choiceOptions, cb) {
|
|||||||
choices: choiceOptions,
|
choices: choiceOptions,
|
||||||
filter: function (val) { return val.toLowerCase(); }
|
filter: function (val) { return val.toLowerCase(); }
|
||||||
};
|
};
|
||||||
inquirer.prompt(question, function (answers) {
|
plugins.inquirer.prompt(question, function (answers) {
|
||||||
var answer = answers.userFeedback;
|
var answer = answers.userFeedback;
|
||||||
cb(answer);
|
cb(answer);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
module.exports = smartcli;
|
}
|
||||||
|
SmartcliInteraction.init = init;
|
||||||
|
})(SmartcliInteraction || (SmartcliInteraction = {}));
|
||||||
|
/// <reference path="typings/tsd.d.ts" />
|
||||||
|
/// <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 = 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",
|
"description": "nodejs wrapper for CLI related tasks",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"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)",
|
"devTest": "(npm test) && (node test.js --test true)",
|
||||||
"reinstall": "(rm -r node_modules && npm install)",
|
"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)",
|
"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",
|
"beautylog": "1.0.2",
|
||||||
"cliff": "^0.1.10",
|
"cliff": "^0.1.10",
|
||||||
"inquirer": "^0.11.0",
|
"inquirer": "^0.11.0",
|
||||||
"smartparam": "0.0.5",
|
"smartparam": "0.0.7",
|
||||||
"yargs": "^3.29.0"
|
"yargs": "^3.29.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
127
test.js
127
test.js
@ -1,45 +1,112 @@
|
|||||||
/// <reference path="typings/tsd.d.ts" />
|
/// <reference path="typings/tsd.d.ts" />
|
||||||
var smartcli = require("./index.js");
|
var smartcli = require("./index.js");
|
||||||
var bl = require("beautylog");
|
var bl = require("beautylog")("os");
|
||||||
bl.log('now starting Test');
|
bl.log('now starting Test');
|
||||||
bl.log('starting with initial CLI commands and options');
|
bl.log('starting with initial CLI commands and options');
|
||||||
var commandsString = 'You specified the following commands:';
|
var commandsString = 'You specified the following commands:';
|
||||||
var commands = smartcli.getCommands();
|
var commands = smartcli.get.commandArray();
|
||||||
for (var key in commands) {
|
for (var key in commands) {
|
||||||
commandsString = commandsString + ' ' + commands[key];
|
commandsString = commandsString + ' ' + commands[key];
|
||||||
}
|
}
|
||||||
bl.log(commandsString);
|
bl.log(commandsString);
|
||||||
var getCwdTest = function () {
|
/* ------------------------------------------------------------------ *
|
||||||
console.log('The current directory is: ' + smartcli.getCwd().path);
|
* ------------------- CHECKS TESTS --------------------------------- *
|
||||||
};
|
* ------------------------------------------------------------------ */
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
var checkCommandTest = function () {
|
var checkCommandTest = function () {
|
||||||
if (smartcli.checkCommand('jazz')) {
|
if (smartcli.check.command('jazz')) {
|
||||||
bl.log('One of your commands is jazz');
|
bl.success('One of your commands is jazz. It is supposed to be there. Perfect!');
|
||||||
}
|
}
|
||||||
else {
|
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();
|
checkCommandTest();
|
||||||
getOptionTest();
|
var checkCommandPresenceTest = function () {
|
||||||
//starting first interaction test (the other tests are then started via callbacks)
|
if (smartcli.check.commandPresence()) {
|
||||||
getAnswerTest();
|
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.interfaces.ts" />
|
||||||
/// <reference path="./smartcli.checks.ts" />
|
/// <reference path="./smartcli.checks.ts" />
|
||||||
/// <reference path="./smartcli.getters.ts" />
|
/// <reference path="./smartcli.getters.ts" />
|
||||||
|
/// <reference path="./smartcli.interaction.ts" />
|
||||||
|
|
||||||
var plugins = smartcliPlugins.init(); //get all the required npm modules under plugins
|
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.
|
//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
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = smartcli; // expose smartcli to outside world
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------------------------
|
|
||||||
*----------------------- 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;
|
|
||||||
|
@ -1,48 +1,66 @@
|
|||||||
/// <reference path="./index.ts" />
|
/// <reference path="./index.ts" />
|
||||||
module smartcliChecks {
|
module SmartcliChecks {
|
||||||
export function init() {
|
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.
|
* checks for a special command string and returns true if found.
|
||||||
* @param commandString
|
* @param commandString
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
smartcli.checkCommand = function(commandString:string):boolean {
|
smartcli.check.command = function(commandString:string):boolean {
|
||||||
if (plugins.argv._.indexOf(commandString) == 0) {
|
if (plugins.argv._.indexOf(commandString) == 0) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false;
|
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.
|
* checks if a command is present, returns true if yes, false if no.
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
smartcli.checkCommandPresence = function():boolean {
|
smartcli.check.commandPresence = function():boolean {
|
||||||
if(plugins.argv._.length < 0){
|
if(plugins.argv._.length > 0){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
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
|
* checks for a specific option string, returns true if yes, returns false if no
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
smartcli.checkOption = function(optionString):boolean {
|
smartcli.check.option = function(optionString):boolean {
|
||||||
if(plugins.smartparam.exists(plugins.argv, optionString)) {
|
if(plugins.smartparam.exists(plugins.argv, optionString)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
smartcli.checkOptionsPresence = function():boolean {
|
smartcli.check.optionPresence = function():boolean {
|
||||||
if (plugins.argv.indexOf() != -1) {
|
if (plugins.argv.indexOf() != -1) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -1,50 +1,83 @@
|
|||||||
/// <reference path="./index.ts" />
|
/// <reference path="./index.ts" />
|
||||||
module smartcliGetters {
|
module SmartcliGetters {
|
||||||
export function init() {
|
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 = {
|
var cliCommand = {
|
||||||
specified: smartcli.checkCommand(commandString),
|
specified: smartcli.check.commandPresence(),
|
||||||
name: commandString,
|
name: plugins.argv._[1],
|
||||||
arguments: smartcli.getCommandArgument(1)
|
arguments: smartcli.get.commandArguments(1)
|
||||||
}
|
}
|
||||||
return cliCommand;
|
return cliCommand;
|
||||||
};
|
};
|
||||||
|
|
||||||
smartcli.getCommandArgument = function(argumentLevel):CommandArgument {
|
/**
|
||||||
var commandArgument = {
|
* gets the second or higher value of plugins.argv._ if specified and returns it as commandArgument
|
||||||
specified
|
* @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;
|
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}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
smartcli.getCommands = function ():string[] {
|
smartcli.get.commandArray = function ():string[] {
|
||||||
return plugins.argv._;
|
return plugins.argv._;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// options
|
/**
|
||||||
smartcli.getOption = function(optionName:string):CliOption {
|
* returns a cli option
|
||||||
if (plugins.argv.hasOwnProperty(optionName)) {
|
* @param optionName
|
||||||
return {
|
* @returns {CliOption}
|
||||||
name:optionName,
|
*/
|
||||||
specified: true,
|
smartcli.get.option = function(optionName:string):CliOption {
|
||||||
value: plugins.argv[optionName] //we already know from the "if" above that the value is available.
|
var cliOption:CliOption = {
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
name:optionName,
|
name:optionName,
|
||||||
specified: false,
|
specified: false,
|
||||||
value: 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 = {};
|
var options = {};
|
||||||
for (var key in plugins.argv) {
|
for (var key in plugins.argv) {
|
||||||
if (key != "_") {
|
if (key != "_") {
|
||||||
@ -58,7 +91,7 @@ module smartcliGetters {
|
|||||||
* returns Directory of cwd
|
* returns Directory of cwd
|
||||||
* @returns {{path: string}}
|
* @returns {{path: string}}
|
||||||
*/
|
*/
|
||||||
smartcli.getCwd = function():Directory {
|
smartcli.get.cwd = function():Directory {
|
||||||
return {
|
return {
|
||||||
path: process.cwd()
|
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 {
|
interface CliCommand {
|
||||||
specified: boolean;
|
specified: boolean;
|
||||||
name: string;
|
name: string;
|
||||||
second: string;
|
arguments:string[];
|
||||||
third: string;
|
|
||||||
fourth: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CliCommandArgument {
|
interface CliCommandArgument {
|
||||||
specified:boolean;
|
specified:boolean;
|
||||||
name:string;
|
name:string;
|
||||||
|
level:number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
145
ts/test.ts
145
ts/test.ts
@ -1,58 +1,127 @@
|
|||||||
/// <reference path="typings/tsd.d.ts" />
|
/// <reference path="typings/tsd.d.ts" />
|
||||||
var smartcli = require("./index.js");
|
var smartcli = require("./index.js");
|
||||||
var bl = require("beautylog");
|
var bl = require("beautylog")("os");
|
||||||
|
|
||||||
bl.log('now starting Test');
|
bl.log('now starting Test');
|
||||||
bl.log('starting with initial CLI commands and options');
|
bl.log('starting with initial CLI commands and options');
|
||||||
|
|
||||||
var commandsString:string = 'You specified the following commands:';
|
var commandsString:string = 'You specified the following commands:';
|
||||||
var commands = smartcli.getCommands();
|
var commands = smartcli.get.commandArray();
|
||||||
for (var key in commands) {
|
for (var key in commands) {
|
||||||
commandsString = commandsString + ' ' + commands[key];
|
commandsString = commandsString + ' ' + commands[key];
|
||||||
}
|
}
|
||||||
bl.log(commandsString);
|
bl.log(commandsString);
|
||||||
|
|
||||||
|
|
||||||
var getCwdTest = function(){
|
/* ------------------------------------------------------------------ *
|
||||||
console.log('The current directory is: ' + smartcli.getCwd().path);
|
* ------------------- CHECKS TESTS --------------------------------- *
|
||||||
};
|
* ------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
var checkCommandTest = function() {
|
var checkCommandTest = function() {
|
||||||
if (smartcli.checkCommand('jazz')) {
|
if (smartcli.check.command('jazz')) {
|
||||||
bl.log('One of your commands is jazz');
|
bl.success('One of your commands is jazz. It is supposed to be there. Perfect!');
|
||||||
} else {
|
} 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();
|
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