From 09152c8f5fd979c911252b0742c2b3a2677f90ce Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 14 Oct 2015 20:59:01 +0200 Subject: [PATCH] improved return objects --- index.js | 26 +++++++++++++++++++------- package.json | 1 + test.js | 32 ++++++++++++++++++++++---------- ts/index.ts | 34 ++++++++++++++++++++-------------- ts/interfaces.ts | 11 +++++++++++ ts/test.ts | 34 ++++++++++++++++++++++++++-------- 6 files changed, 99 insertions(+), 39 deletions(-) create mode 100644 ts/interfaces.ts diff --git a/index.js b/index.js index 2d69c1f..1973fbe 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,6 @@ +/// /// +/// var path = require("path"); var beautylog = require("beautylog"); var cliff = require("cliff"); @@ -24,11 +26,19 @@ smartcli.getCommands = function () { return argv._; }; // options -smartcli.checkOption = function (optionParam) { - if (argv.hasOwnProperty(optionParam)) { - return true; +smartcli.getOption = function (optionName) { + if (argv.hasOwnProperty(optionName)) { + return { + name: optionName, + specified: true, + value: argv[optionName] //we already know from the "if" above that the value is available. + }; } - return false; + return { + name: optionName, + specified: false, + value: false + }; }; smartcli.getOptions = function () { var options = {}; @@ -40,11 +50,13 @@ smartcli.getOptions = function () { return options; }; /** - * returns the current working directory - * @returns {string} + * returns Directory of cwd + * @returns {{path: string}} */ smartcli.getCwd = function () { - return process.cwd(); + return { + path: process.cwd() + }; }; /* ------------------------------------------------------------------------------ *----------------------- in program CLI interaction ----------------------------- diff --git a/package.json b/package.json index 32fa2e1..7e82e41 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "index.js", "scripts": { "test": "(cd ts/compile && gulp)", + "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)" diff --git a/test.js b/test.js index 06a3aa7..6d4a7b3 100644 --- a/test.js +++ b/test.js @@ -9,12 +9,23 @@ 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'); -} +var getCwdTest = function () { + console.log('The current directory is: ' + smartcli.getCwd().path); +}; +var checkCommandTest = function () { + if (smartcli.checkCommand('jazz')) { + bl.log('One of your commands is jazz'); + } + else { + bl.log('None of your commands is jazz'); + } +}; +var getOptionTest = function () { + console.log('We now test for option --test'); + console.log(smartcli.getOption('test')); +}; +var checkOptionTest = function () { +}; var getAnswerTest = function () { smartcli.getAnswer('How do you feel?', function (answer) { console.log('The answer is: ' + answer); @@ -24,10 +35,11 @@ var getAnswerTest = function () { 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()); -}; +//starting command tests +getCwdTest(); +checkCommandTest(); +getOptionTest(); +//starting first interaction test (the other tests are then started via callbacks) getAnswerTest(); diff --git a/ts/index.ts b/ts/index.ts index 622a73e..e1819fe 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -1,4 +1,5 @@ /// +/// var path = require("path"); var beautylog = require("beautylog"); @@ -34,19 +35,22 @@ smartcli.getCommands = function ():string[] { // options -smartcli.checkOption = function(optionParam:string):boolean { - if (argv.hasOwnProperty(optionParam)) { - return true; +smartcli.getOption = function(optionName:string):CliOption { + if (argv.hasOwnProperty(optionName)) { + return { + name:optionName, + specified: true, + value: argv[optionName] //we already know from the "if" above that the value is available. + }; + + } + return { + name:optionName, + specified: false, + value: false } - return false }; -smartcli.getOptionValue = function(optionParam:string):any { - if (smartcli.checkOption(optionParam)) { - return argv[optionParam] - } - return false; -}; smartcli.getOptions = function() { var options = {}; @@ -59,11 +63,13 @@ smartcli.getOptions = function() { }; /** - * returns the current working directory - * @returns {string} + * returns Directory of cwd + * @returns {{path: string}} */ -smartcli.getCwd = function () { - return process.cwd(); +smartcli.getCwd = function():Directory { + return { + path: process.cwd() + } }; diff --git a/ts/interfaces.ts b/ts/interfaces.ts new file mode 100644 index 0000000..1ae072c --- /dev/null +++ b/ts/interfaces.ts @@ -0,0 +1,11 @@ +/// +interface CliOption { + name: string; + specified:boolean; + value: any; +} + +interface Directory { + path: string; +} + diff --git a/ts/test.ts b/ts/test.ts index b4bd533..f860342 100644 --- a/ts/test.ts +++ b/ts/test.ts @@ -12,12 +12,27 @@ for (var key in commands) { } bl.log(commandsString); -if (smartcli.checkCommand('jazz')) { - bl.log('One of your commands is jazz'); -} else { - bl.log('None of your commands is jazz'); + +var getCwdTest = function(){ + console.log('The current directory is: ' + smartcli.getCwd().path); +}; + +var checkCommandTest = function() { + if (smartcli.checkCommand('jazz')) { + bl.log('One of your commands is jazz'); + } else { + bl.log('None of your commands is jazz'); + } +}; + +var getOptionTest = function() { + console.log('We now test for option --test') + console.log(smartcli.getOption('test')); } +var checkOptionTest = function() { + +}; var getAnswerTest = function() { smartcli.getAnswer('How do you feel?',function(answer){ @@ -29,12 +44,15 @@ var getAnswerTest = function() { 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()); -}; + +//starting command tests +getCwdTest(); +checkCommandTest(); +getOptionTest(); + +//starting first interaction test (the other tests are then started via callbacks) getAnswerTest(); \ No newline at end of file