Compare commits

...

23 Commits

Author SHA1 Message Date
68f7e7fc03 0.0.10 2015-11-09 04:02:51 +01:00
55cdd7f803 add tests and fix some errors 2015-11-09 04:02:46 +01:00
5444f1d3c0 add Tests and improve TypeScript organization 2015-11-09 00:58:40 +01:00
5611ad03aa start smarter CLI logic 2015-11-05 21:43:34 +01:00
d6cbefce2c fix small comment error 2015-10-14 21:16:42 +02:00
afa6f885ad fix small comment error 2015-10-14 21:16:12 +02:00
b1565dec3e 0.0.9 2015-10-14 21:14:29 +02:00
f6969ecfd5 improved readme 2015-10-14 21:14:20 +02:00
9bb85ca666 updated readme 2015-10-14 21:09:26 +02:00
9442f738d9 added devStatus badge 2015-10-14 21:01:39 +02:00
5043829132 0.0.8 2015-10-14 20:59:22 +02:00
09152c8f5f improved return objects 2015-10-14 20:59:01 +02:00
fd7352533f Merge pull request #3 from pushrocks/greenkeeper-beautylog-0.0.15
Update beautylog to version 0.0.15 🚀
2015-10-12 22:45:44 +02:00
4b977eee3b chore(package): update beautylog to version 0.0.15
http://greenkeeper.io/
2015-10-12 22:38:34 +02:00
bec0c4bbfa Merge pull request #2 from pushrocks/greenkeeper-beautylog-0.0.14
Update beautylog to version 0.0.14 🚀
2015-10-12 20:03:37 +02:00
686fa6c6f9 chore(package): update beautylog to version 0.0.14
http://greenkeeper.io/
2015-10-12 20:02:29 +02:00
400c5c6574 Merge pull request #1 from pushrocks/greenkeeper-beautylog-0.0.13
Update beautylog to version 0.0.13 🚀
2015-10-12 14:42:35 +02:00
80770612f5 chore(package): update beautylog to version 0.0.13
http://greenkeeper.io/
2015-10-12 13:15:41 +02:00
a65fcaffea disabled travis email 2015-10-08 00:50:10 +02:00
f80006310b 0.0.7 2015-10-08 00:49:15 +02:00
236cce7297 0.0.6 2015-10-07 01:16:28 +02:00
05595ac997 small update 2015-10-07 01:16:15 +02:00
ce1dde6d54 now handling CLI options 2015-10-07 01:07:38 +02:00
12 changed files with 956 additions and 174 deletions

View File

@ -17,3 +17,4 @@ deploy:
notifications:
slack:
secure: f5Uss0z9RPl/QcA/DroB8loyE93aOYI6bqCkrsiUscmZtlv/TVQtT4dxqGA6uvcG6iTQDBi3Ul88dQxWkRm4IqbhY35/iMaV2dHW4FVYMAh8GQMbsfL2sALCcufxD9blw47awv3iFcwhV1EeyesscjgL0JIjduk96v/7G/6QIO2838M1lzlgtj+kRUkim8qkaEs1je3gRrhMUIjLuAdscMXyUKYFMjWo9ACSjVUl30R/ZNemb18itIja6i92GotreBgcfEMczvy58ovDC7xdJUsY8LjMI01DwY+WPRnI0tAhsuI8moBwwcdM4e3bAjKjucQRjO33O5bMWRZ6QCiYd0DnCEFyCPQLJ4GSy/tkD00n8ijLHAOSV3AH1zNbdK1EAdSPQXDvlI36KJn/2hyQLoitGHVUPr76ujJWP82ypO2tgIp3XQU0dJVCxDuHnwJO2+hjdI+gCPqxNTpjeujHx3UdkTGNRjuuf9dlZ/D08fApjYxy2fxItTqo3QjP/nrqvBXUOPP8yPHpjIT4H2t5Pr4SJjBGI6X4qhKyFj6s9rA/Xu1rL+45zu1C3uC3z+u3T9UwrbzJ/cZM6r6UQvQmUvIfBNaMlg4I/diQCDIPL+Rhop2nylY3IcHmJnk2itn7kOqj1tohCpFEml5pRuSZy4udWywkdtyBAsHWFLF7oiQ=
email: false

View File

@ -1,16 +1,57 @@
# smartcli
nodejs wrapper for CLI related tasks
nodejs wrapper for CLI related tasks
[![Dev Status](https://img.shields.io/badge/DevStatus-Active-green.svg)](https://github.com/pushrocks/smartcli/commits/dev)
### Buildstatus/Dependencies
## Buildstatus/Dependencies
[![Build Status](https://travis-ci.org/pushrocks/smartcli.svg?branch=master)](https://travis-ci.org/pushrocks/smartcli)
[![devDependency Status](https://david-dm.org/pushrocks/smartcli/dev-status.svg)](https://david-dm.org/pushrocks/smartcli#info=devDependencies)
### Usage
This npm package comes with everything you need to start your own gulp plugin.
## Install the package
npm install smartcli
We recommend modifying the ts/index.ts file,
then run `npm install` to install the dev dependencies
and use `npm test` to compile the TypeScript file.
Cheers
## Usage
this plugin tries to establish some logic in which CLI tools work.
take the following commandline input:
```
mytool function argument1 argument2 --option1 option1Value --option2 option2Value
```
* 'mytool' obviously is the tool (like git)
* function is the main thing the tool shall do (like commit)
* option is an option you can add (like -m for message)
* optionValue is the referenced option value (like a commit message)
### The inner organization of smartcli
**smartcli** exposes three major groups of functions:
* check functions
* are grouped in **smartcli.checks** object
* get functions
* are grouped in **smartcli.get** object
* async interaction functions
* are grouped in **smartcli.interaction** object
```js
var smartcli = require("smartcli");
/* -------------- Check Functions -------------------*/
//returns true for terminal command "node myjs.js jazz"
smartcli.check.command('jazz');
/**
* returns an object for terminal command "node myjs.js --myoption something" like so
* {
* name: 'myoption',
* specified: true,
* value: 'something'
* }
*/
smartcli.get.option('myoption');
```
Cheers
Phil from Lossless Digital

324
index.js
View File

@ -1,69 +1,263 @@
/// <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._[0],
arguments: smartcli.get.commandArgs()
};
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 array with commandArgs
* @returns {CliCommandArgument[]}
*/
smartcli.get.commandArgs = function () {
var commandArgs = [];
var argsArray = smartcli.get.commandArray().slice(0);
argsArray.shift();
for (var item in argsArray) {
var commandArgument = {
specified: true,
name: argsArray[item],
level: item + 1
};
commandArgs.push(commandArgument);
}
return commandArgs;
};
/**
* returns complete command array
* @returns {any}
*/
smartcli.get.commandArray = function () {
var commandArray = plugins.argv._;
return commandArray;
};
/**
* returns a cli option
* @param optionName
* @returns {CliOption}
*/
smartcli.get.option = function (optionName) {
var cliOption = {
name: optionName,
specified: false,
value: false
};
if (plugins.smartparam.exists(plugins.argv, 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" />
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;
/**
* returns the current working directory
* @returns {string}
*/
smartcli.getCwd = function () {
return process.cwd();
};
/**
* 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

View File

@ -1,10 +1,12 @@
{
"name": "smartcli",
"version": "0.0.5",
"version": "0.0.10",
"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)",
"startdev": "(git checkout master && git pull origin master)"
@ -25,10 +27,11 @@
},
"homepage": "https://github.com/pushrocks/smartcli",
"dependencies": {
"beautylog": "0.0.12",
"beautylog": "1.0.2",
"cliff": "^0.1.10",
"inquirer": "^0.10.1",
"yargs": "^3.26.0"
"inquirer": "^0.11.0",
"smartparam": "0.0.7",
"yargs": "^3.29.0"
},
"devDependencies": {
"gulp": "3.9.0",

179
test.js
View File

@ -1,18 +1,173 @@
/// <reference path="typings/tsd.d.ts" />
var smartcli = require("./index.js");
var getAnswerTest = function () {
smartcli.getAnswer('How do you feel?', function (answer) {
console.log('The answer is: ' + answer);
getChoiceTest();
});
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.get.commandArray();
for (var key in commands) {
commandsString = commandsString + ' ' + commands[key];
}
bl.log(commandsString);
/* ------------------------------------------------------------------ *
* ------------------- CHECKS TESTS --------------------------------- *
* ------------------------------------------------------------------ */
/**
*
*/
var checkCommandTest = function () {
if (smartcli.check.command('jazz')) {
bl.success('One of your commands is jazz. It is supposed to be there. Perfect!');
}
else {
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 getChoiceTest = function () {
smartcli.getChoice('What music do you like to hear?', ['Jazz', 'Blues', 'Classical'], function (answer) {
console.log('The answer is: ' + answer);
getCwdTest();
});
checkCommandTest();
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 getCommandTest = function () {
var cliCommand = smartcli.get.command();
if (cliCommand.name == "jazz") {
bl.success('The specified command name is "jazz". Perfect!');
}
else {
bl.error('The specified command name is not "jazz". Something is wrong!');
process.exit(1);
}
};
getCommandTest();
var getCommandArgumentTest = function () {
var cliArgument = smartcli.get.commandArgument(1);
var cliArgument2 = smartcli.get.commandArgument(2);
if (cliArgument.name == "jam") {
bl.success('The first specified command argument name is "jam". Perfect!');
}
else {
bl.error('The first specified command argument name is not "jam". Something is wrong!');
process.exit(1);
}
if (cliArgument2.name == "undefined") {
bl.success('The second specified command argument name is "undefined". Perfect!');
}
else {
bl.error('The second specified command argument name is not "undefined". Something is wrong!');
process.exit(1);
}
};
getCommandArgumentTest();
var getCommandArgsTest = function () {
var commandArgs = smartcli.get.commandArgs();
if (commandArgs[0].name == "jam") {
bl.success("first command argument is 'jam'. Perfect!");
}
else {
bl.error("first command argument is not 'jam'. Something is wromg!");
console.log(commandArgs[0].name);
process.exit(1);
}
};
getCommandArgsTest();
var getOptionTest = function () {
var cliOption = smartcli.get.option("awesome");
var cliOption2 = smartcli.get.option("terrific");
if (cliOption.specified) {
bl.success("awesome is specified. Perfect!");
}
else {
bl.error("awesome is not specified. Somehthing is wrong");
process.exit(1);
}
if (!cliOption2.specified) {
bl.success("terrific is not specified. Perfect!");
}
else {
bl.error("terrific is specified. Somehthing is wrong");
process.exit(1);
}
};
getOptionTest();
var getCwdTest = function () {
console.log('The current directory is: ' + smartcli.getCwd());
bl.info('The current directory is: ' + smartcli.get.cwd().path);
};
getAnswerTest();
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();
}
;

View File

@ -1,81 +1,25 @@
/// <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 path = require("path");
var beautylog = require("beautylog");
var cliff = require("cliff");
var inquirer = require("inquirer");
var argv = require('yargs').argv;
var plugins = smartcliPlugins.init(); //get all the required npm modules under plugins
//define the smartcli object
var smartcli:any = {};
//add plugins from above for direct use
smartcli.inquirer = inquirer;
smartcli.cliff = cliff;
smartcli.argv = argv;
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.
/**
* returns the current working directory
* @returns {string}
*/
smartcli.getCwd = function () {
return process.cwd();
};
//init checks. Checks return boolean. That means they can be used as question with an answer of yes or no.
/**
* 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);
});
};
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;
module.exports = smartcli; // expose smartcli to outside world

70
ts/smartcli.checks.ts Normal file
View File

@ -0,0 +1,70 @@
/// <reference path="./index.ts" />
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.check.command = function(commandString:string):boolean {
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():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.check.option = function(optionString):boolean {
if(plugins.smartparam.exists(plugins.argv, optionString)) {
return true;
}
return false;
};
smartcli.check.optionPresence = function():boolean {
if (plugins.argv.indexOf() != -1) {
return true
}
return false;
};
}
}

110
ts/smartcli.getters.ts Normal file
View File

@ -0,0 +1,110 @@
/// <reference path="./index.ts" />
module SmartcliGetters {
export function init() {
smartcli.get = {};
/**
*
* @param commandString
* @returns {{specified: boolean, name: any, arguments: CliCommandArgument}}
*/
smartcli.get.command = function():CliCommand {
var cliCommand = {
specified: smartcli.check.commandPresence(),
name: plugins.argv._[0],
arguments: smartcli.get.commandArgs()
}
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):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 array with commandArgs
* @returns {CliCommandArgument[]}
*/
smartcli.get.commandArgs = function():CliCommandArgument[] {
var commandArgs:CliCommandArgument[] = [];
var argsArray = smartcli.get.commandArray().slice(0);
argsArray.shift();
for (var item in argsArray){
var commandArgument:CliCommandArgument = {
specified:true,
name:argsArray[item],
level: item + 1
}
commandArgs.push(commandArgument);
}
return commandArgs;
};
/**
* returns complete command array
* @returns {any}
*/
smartcli.get.commandArray = function ():string[] {
var commandArray = plugins.argv._;
return commandArray;
};
/**
* 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.smartparam.exists(plugins.argv,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():Directory {
return {
path: process.cwd()
}
};
}
}

View 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);
});
};
}
}

23
ts/smartcli.interfaces.ts Normal file
View File

@ -0,0 +1,23 @@
/// <reference path="index.ts" />
interface CliOption {
name: string;
specified:boolean;
value: any;
}
interface Directory {
path: string;
}
interface CliCommand {
specified: boolean;
name: string;
arguments:string[];
}
interface CliCommandArgument {
specified:boolean;
name:string;
level:number;
}

14
ts/smartcli.plugins.ts Normal file
View File

@ -0,0 +1,14 @@
/// <reference path="./index.ts" />
module smartcliPlugins {
var plugins:any = {};
export 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;
}
}

View File

@ -1,22 +1,188 @@
/// <reference path="typings/tsd.d.ts" />
var smartcli = require("./index.js");
var bl = require("beautylog")("os");
var getAnswerTest = function() {
smartcli.getAnswer('How do you feel?',function(answer){
console.log('The answer is: ' + answer);
getChoiceTest();
});
};
bl.log('now starting Test');
bl.log('starting with initial CLI commands and options');
var getChoiceTest = function() {
smartcli.getChoice('What music do you like to hear?',['Jazz','Blues','Classical'],function(answer){
console.log('The answer is: ' + answer);
getCwdTest();
});
var commandsString:string = 'You specified the following commands:';
var commands = smartcli.get.commandArray();
for (var key in commands) {
commandsString = commandsString + ' ' + commands[key];
}
bl.log(commandsString);
/* ------------------------------------------------------------------ *
* ------------------- CHECKS TESTS --------------------------------- *
* ------------------------------------------------------------------ */
/**
*
*/
var checkCommandTest = function() {
if (smartcli.check.command('jazz')) {
bl.success('One of your commands is jazz. It is supposed to be there. Perfect!');
} else {
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);
}
};
checkCommandTest();
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 getCommandTest = function(){
var cliCommand = smartcli.get.command();
if(cliCommand.name == "jazz") {
bl.success('The specified command name is "jazz". Perfect!');
} else {
bl.error('The specified command name is not "jazz". Something is wrong!');
process.exit(1);
}
};
getCommandTest();
var getCommandArgumentTest = function() {
var cliArgument = smartcli.get.commandArgument(1);
var cliArgument2 = smartcli.get.commandArgument(2);
if(cliArgument.name == "jam") {
bl.success('The first specified command argument name is "jam". Perfect!');
} else {
bl.error('The first specified command argument name is not "jam". Something is wrong!');
process.exit(1);
}
if(cliArgument2.name == "undefined") {
bl.success('The second specified command argument name is "undefined". Perfect!');
} else {
bl.error('The second specified command argument name is not "undefined". Something is wrong!');
process.exit(1);
}
};
getCommandArgumentTest();
var getCommandArgsTest = function() {
var commandArgs = smartcli.get.commandArgs();
if(commandArgs[0].name == "jam") {
bl.success("first command argument is 'jam'. Perfect!");
} else {
bl.error("first command argument is not 'jam'. Something is wromg!");
console.log(commandArgs[0].name);
process.exit(1);
}
};
getCommandArgsTest();
var getOptionTest = function() {
var cliOption = smartcli.get.option("awesome");
var cliOption2 = smartcli.get.option("terrific");
if(cliOption.specified){
bl.success("awesome is specified. Perfect!")
} else {
bl.error("awesome is not specified. Somehthing is wrong");
process.exit(1);
}
if(!cliOption2.specified){
bl.success("terrific is not specified. Perfect!")
} else {
bl.error("terrific is specified. Somehthing is wrong");
process.exit(1);
}
};
getOptionTest();
var getCwdTest = function(){
console.log('The current directory is: ' + smartcli.getCwd());
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();
});
};
getAnswerTest();
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();
};