Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
b51bbb00a4 | |||
5bd32f03c5 | |||
68f7e7fc03 | |||
55cdd7f803 | |||
5444f1d3c0 | |||
5611ad03aa | |||
d6cbefce2c | |||
afa6f885ad | |||
b1565dec3e | |||
f6969ecfd5 | |||
9bb85ca666 | |||
9442f738d9 | |||
5043829132 | |||
09152c8f5f | |||
fd7352533f | |||
4b977eee3b | |||
bec0c4bbfa | |||
686fa6c6f9 | |||
400c5c6574 | |||
80770612f5 | |||
a65fcaffea |
10
.travis.yml
10
.travis.yml
@ -1,11 +1,8 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "4.2.2"
|
||||
before_install:
|
||||
- nvm install stable
|
||||
- node -v
|
||||
- npm -v
|
||||
- npm install -g gulp
|
||||
- npm install gulp
|
||||
- npm install gulp-typescript
|
||||
- npm install -g tsd
|
||||
deploy:
|
||||
provider: npm
|
||||
email: npm@smart-coordination.com
|
||||
@ -17,3 +14,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
|
59
README.md
59
README.md
@ -1,16 +1,57 @@
|
||||
# smartcli
|
||||
nodejs wrapper for CLI related tasks
|
||||
nodejs wrapper for CLI related tasks
|
||||
[](https://github.com/pushrocks/smartcli/commits/dev)
|
||||
|
||||
### Buildstatus/Dependencies
|
||||
## Buildstatus/Dependencies
|
||||
[](https://travis-ci.org/pushrocks/smartcli)
|
||||
[](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
|
||||
|
356
index.js
356
index.js
@ -1,101 +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;
|
||||
/* ------------------------------------------------------------------------------
|
||||
*----------------------- 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.checkOption = function (optionParam) {
|
||||
if (argv.hasOwnProperty(optionParam)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
smartcli.getOptions = function () {
|
||||
var options = {};
|
||||
for (var key in argv) {
|
||||
if (key != "_") {
|
||||
options['key'] = argv['key'];
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
/**
|
||||
* returns the current working directory
|
||||
* @returns {string}
|
||||
*/
|
||||
smartcli.getCwd = function () {
|
||||
return 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
|
||||
|
13
package.json
13
package.json
@ -1,10 +1,12 @@
|
||||
{
|
||||
"name": "smartcli",
|
||||
"version": "0.0.7",
|
||||
"version": "0.0.11",
|
||||
"description": "nodejs wrapper for CLI related tasks",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "(cd ts/compile && gulp)",
|
||||
"test": "(cd ts/compile && node compile.js) && (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",
|
||||
|
182
test.js
182
test.js
@ -1,33 +1,173 @@
|
||||
/// <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);
|
||||
if (smartcli.checkCommand('jazz')) {
|
||||
bl.log('One of your commands is jazz');
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- 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 () {
|
||||
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.log('None of your commands is jazz');
|
||||
bl.info("--silent option is specified, thus we are not running interaction tests.");
|
||||
endTests();
|
||||
}
|
||||
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);
|
||||
getCwdTest();
|
||||
});
|
||||
};
|
||||
var getCwdTest = function () {
|
||||
console.log('The current directory is: ' + smartcli.getCwd());
|
||||
};
|
||||
getAnswerTest();
|
||||
;
|
||||
|
@ -22,4 +22,5 @@ gulp.task('compileTestTS', function() {
|
||||
|
||||
gulp.task('default',['compileTS','compileTestTS'], function() {
|
||||
console.log('Typescript compiled');
|
||||
});
|
||||
});
|
||||
gulp.start.apply(gulp, ['default']);
|
136
ts/index.ts
136
ts/index.ts
@ -1,133 +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.
|
||||
|
||||
/* ------------------------------------------------------------------------------
|
||||
*----------------------- initial call CLI args -----------------------------
|
||||
*----------------------------------------------------------------------------- */
|
||||
//init checks. Checks return boolean. That means they can be used as question with an answer of yes or no.
|
||||
|
||||
// commands
|
||||
SmartcliChecks.init(); // is defined in smartcli.checks.ts
|
||||
SmartcliGetters.init(); // is defined in smartcli.getters.ts
|
||||
SmartcliInteraction.init(); // is defined in smartcli.interaction.ts
|
||||
|
||||
|
||||
smartcli.checkCommand = function(commandString:string):boolean {
|
||||
if (argv._.indexOf(commandString) != -1) {
|
||||
return true
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
smartcli.getCommands = function ():string[] {
|
||||
return argv._;
|
||||
};
|
||||
|
||||
|
||||
// options
|
||||
smartcli.checkOption = function(optionParam:string):boolean {
|
||||
if (argv.hasOwnProperty(optionParam)) {
|
||||
return true;
|
||||
}
|
||||
return false
|
||||
};
|
||||
|
||||
smartcli.getOptionValue = function(optionParam:string):any {
|
||||
if (smartcli.checkOption(optionParam)) {
|
||||
return argv[optionParam]
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
smartcli.getOptions = function() {
|
||||
var options = {};
|
||||
for (var key in argv) {
|
||||
if (key != "_") {
|
||||
options['key'] = argv['key'];
|
||||
}
|
||||
}
|
||||
return options;
|
||||
};
|
||||
|
||||
/**
|
||||
* returns the current working directory
|
||||
* @returns {string}
|
||||
*/
|
||||
smartcli.getCwd = function () {
|
||||
return 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: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);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
module.exports = smartcli;
|
||||
module.exports = smartcli; // expose smartcli to outside world
|
||||
|
70
ts/smartcli.checks.ts
Normal file
70
ts/smartcli.checks.ts
Normal 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
110
ts/smartcli.getters.ts
Normal 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()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
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);
|
||||
});
|
||||
|
||||
};
|
||||
}
|
||||
}
|
23
ts/smartcli.interfaces.ts
Normal file
23
ts/smartcli.interfaces.ts
Normal 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
14
ts/smartcli.plugins.ts
Normal 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;
|
||||
}
|
||||
}
|
186
ts/test.ts
186
ts/test.ts
@ -1,40 +1,188 @@
|
||||
/// <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);
|
||||
|
||||
if (smartcli.checkCommand('jazz')) {
|
||||
bl.log('One of your commands is jazz');
|
||||
} else {
|
||||
bl.log('None of your commands is jazz');
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* ------------------- CHECKS TESTS --------------------------------- *
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
|
||||
var getAnswerTest = function() {
|
||||
smartcli.getAnswer('How do you feel?',function(answer){
|
||||
console.log('The answer is: ' + answer);
|
||||
getChoiceTest();
|
||||
});
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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 getChoiceTest = function() {
|
||||
smartcli.getChoice('What music do you like to hear?',['Jazz','Blues','Classical'],function(answer){
|
||||
console.log('The answer is: ' + answer);
|
||||
getCwdTest();
|
||||
});
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user