add tests and fix some errors
This commit is contained in:
parent
5444f1d3c0
commit
55cdd7f803
22
README.md
22
README.md
@ -2,14 +2,14 @@
|
|||||||
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)
|
[![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)
|
[![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)
|
[![devDependency Status](https://david-dm.org/pushrocks/smartcli/dev-status.svg)](https://david-dm.org/pushrocks/smartcli#info=devDependencies)
|
||||||
|
|
||||||
### Install the package
|
## Install the package
|
||||||
npm install smartcli
|
npm install smartcli
|
||||||
|
|
||||||
### Usage
|
## Usage
|
||||||
|
|
||||||
this plugin tries to establish some logic in which CLI tools work.
|
this plugin tries to establish some logic in which CLI tools work.
|
||||||
|
|
||||||
@ -24,11 +24,23 @@ mytool function argument1 argument2 --option1 option1Value --option2 option2Valu
|
|||||||
* option is an option you can add (like -m for message)
|
* option is an option you can add (like -m for message)
|
||||||
* optionValue is the referenced option value (like a commit 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
|
```js
|
||||||
var smartcli = require("smartcli");
|
var smartcli = require("smartcli");
|
||||||
|
|
||||||
|
/* -------------- Check Functions -------------------*/
|
||||||
//returns true for terminal command "node myjs.js jazz"
|
//returns true for terminal command "node myjs.js jazz"
|
||||||
smartcli.checkCommand('jazz');
|
smartcli.check.command('jazz');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns an object for terminal command "node myjs.js --myoption something" like so
|
* returns an object for terminal command "node myjs.js --myoption something" like so
|
||||||
@ -38,7 +50,7 @@ smartcli.checkCommand('jazz');
|
|||||||
* value: 'something'
|
* value: 'something'
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
smartcli.getOption('myoption');
|
smartcli.get.option('myoption');
|
||||||
```
|
```
|
||||||
|
|
||||||
Cheers
|
Cheers
|
||||||
|
22
index.js
22
index.js
@ -95,8 +95,8 @@ var SmartcliGetters;
|
|||||||
smartcli.get.command = function () {
|
smartcli.get.command = function () {
|
||||||
var cliCommand = {
|
var cliCommand = {
|
||||||
specified: smartcli.check.commandPresence(),
|
specified: smartcli.check.commandPresence(),
|
||||||
name: plugins.argv._[1],
|
name: plugins.argv._[0],
|
||||||
arguments: smartcli.get.commandArguments(1)
|
arguments: smartcli.get.commandArgs()
|
||||||
};
|
};
|
||||||
return cliCommand;
|
return cliCommand;
|
||||||
};
|
};
|
||||||
@ -122,12 +122,21 @@ var SmartcliGetters;
|
|||||||
return commandArgument;
|
return commandArgument;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
*
|
* returns array with commandArgs
|
||||||
* @returns {CliCommandArgument[]}
|
* @returns {CliCommandArgument[]}
|
||||||
*/
|
*/
|
||||||
smartcli.get.commandArgs = function () {
|
smartcli.get.commandArgs = function () {
|
||||||
var commandArgs = [];
|
var commandArgs = [];
|
||||||
for (var command in plugins.argv._)
|
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;
|
return commandArgs;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@ -135,7 +144,8 @@ var SmartcliGetters;
|
|||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
smartcli.get.commandArray = function () {
|
smartcli.get.commandArray = function () {
|
||||||
return plugins.argv._;
|
var commandArray = plugins.argv._;
|
||||||
|
return commandArray;
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* returns a cli option
|
* returns a cli option
|
||||||
@ -148,7 +158,7 @@ var SmartcliGetters;
|
|||||||
specified: false,
|
specified: false,
|
||||||
value: false
|
value: false
|
||||||
};
|
};
|
||||||
if (plugins.argv.hasOwnProperty(optionName)) {
|
if (plugins.smartparam.exists(plugins.argv, optionName)) {
|
||||||
cliOption.name = optionName;
|
cliOption.name = optionName;
|
||||||
cliOption.specified = true;
|
cliOption.specified = true;
|
||||||
cliOption.value = plugins.argv[optionName]; //we already know from the "if" above that the value is available.
|
cliOption.value = plugins.argv[optionName]; //we already know from the "if" above that the value is available.
|
||||||
|
61
test.js
61
test.js
@ -79,6 +79,67 @@ checkCommandArgumentPresenceTest();
|
|||||||
/* ------------------------------------------------------------------ *
|
/* ------------------------------------------------------------------ *
|
||||||
* ------------------- GETTERS TESTS -------------------------------- *
|
* ------------------- 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 () {
|
var getCwdTest = function () {
|
||||||
bl.info('The current directory is: ' + smartcli.get.cwd().path);
|
bl.info('The current directory is: ' + smartcli.get.cwd().path);
|
||||||
};
|
};
|
||||||
|
@ -10,8 +10,8 @@ module SmartcliGetters {
|
|||||||
smartcli.get.command = function():CliCommand {
|
smartcli.get.command = function():CliCommand {
|
||||||
var cliCommand = {
|
var cliCommand = {
|
||||||
specified: smartcli.check.commandPresence(),
|
specified: smartcli.check.commandPresence(),
|
||||||
name: plugins.argv._[1],
|
name: plugins.argv._[0],
|
||||||
arguments: smartcli.get.commandArguments(1)
|
arguments: smartcli.get.commandArgs()
|
||||||
}
|
}
|
||||||
return cliCommand;
|
return cliCommand;
|
||||||
};
|
};
|
||||||
@ -39,12 +39,21 @@ module SmartcliGetters {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* returns array with commandArgs
|
||||||
* @returns {CliCommandArgument[]}
|
* @returns {CliCommandArgument[]}
|
||||||
*/
|
*/
|
||||||
smartcli.get.commandArgs = function():CliCommandArgument[] {
|
smartcli.get.commandArgs = function():CliCommandArgument[] {
|
||||||
var commandArgs:CliCommandArgument[] = [];
|
var commandArgs:CliCommandArgument[] = [];
|
||||||
for (var command in plugins.argv._)
|
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;
|
return commandArgs;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -53,7 +62,8 @@ module SmartcliGetters {
|
|||||||
* @returns {any}
|
* @returns {any}
|
||||||
*/
|
*/
|
||||||
smartcli.get.commandArray = function ():string[] {
|
smartcli.get.commandArray = function ():string[] {
|
||||||
return plugins.argv._;
|
var commandArray = plugins.argv._;
|
||||||
|
return commandArray;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -68,7 +78,7 @@ module SmartcliGetters {
|
|||||||
specified: false,
|
specified: false,
|
||||||
value: false
|
value: false
|
||||||
};
|
};
|
||||||
if (plugins.argv.hasOwnProperty(optionName)) {
|
if (plugins.smartparam.exists(plugins.argv,optionName)) {
|
||||||
cliOption.name = optionName;
|
cliOption.name = optionName;
|
||||||
cliOption.specified = true;
|
cliOption.specified = true;
|
||||||
cliOption.value = plugins.argv[optionName] //we already know from the "if" above that the value is available.
|
cliOption.value = plugins.argv[optionName] //we already know from the "if" above that the value is available.
|
||||||
|
61
ts/test.ts
61
ts/test.ts
@ -84,6 +84,67 @@ checkCommandArgumentPresenceTest();
|
|||||||
/* ------------------------------------------------------------------ *
|
/* ------------------------------------------------------------------ *
|
||||||
* ------------------- GETTERS TESTS -------------------------------- *
|
* ------------------- 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(){
|
var getCwdTest = function(){
|
||||||
bl.info('The current directory is: ' + smartcli.get.cwd().path);
|
bl.info('The current directory is: ' + smartcli.get.cwd().path);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user