Updated Branches: refs/heads/master d0f44cae6 -> c775d2ab6
[CB-4532] Changes the CLI to only use optimist for verbose and version - Also adds limited unit tests for the CLI module Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/c775d2ab Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/c775d2ab Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/c775d2ab Branch: refs/heads/master Commit: c775d2ab693d29b45cf5dab0583babc13685007f Parents: d0f44ca Author: Jeffrey Heifetz <[email protected]> Authored: Wed Aug 7 16:08:37 2013 -0400 Committer: Jeffrey Heifetz <[email protected]> Committed: Fri Aug 9 09:58:04 2013 -0400 ---------------------------------------------------------------------- spec/cli.spec.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/cli.js | 19 +++++++++---- 2 files changed, 93 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c775d2ab/spec/cli.spec.js ---------------------------------------------------------------------- diff --git a/spec/cli.spec.js b/spec/cli.spec.js new file mode 100644 index 0000000..8ecf926 --- /dev/null +++ b/spec/cli.spec.js @@ -0,0 +1,79 @@ +var CLI = require("../src/cli"); + cordova = require("../cordova"); + +describe("cordova cli", function () { + + describe("options", function () { + describe("version", function () { + var version = require("../package").version; + beforeEach(function () { + spyOn(console, "log"); + }); + + it("will spit out the version with -v", function () { + new CLI(["-v"]); + expect(console.log).toHaveBeenCalledWith(version); + }); + + it("will spit out the version with --version", function () { + new CLI(["--version"]); + expect(console.log).toHaveBeenCalledWith(version); + }); + + it("will spit out the version with -v anywher", function () { + new CLI(["one", "-v", "three"]); + expect(console.log).toHaveBeenCalledWith(version); + }); + }); + }); + + describe("project commands other than plugin and platform", function () { + beforeEach(function () { + spyOn(cordova, "build"); + }); + + afterEach(function () { + cordova.removeAllListeners(); + }); + + it("will call command with all arguments passed through", function () { + new CLI(["node", "cordova", "build", "blackberry10", "-k", "abcd1234"]); + expect(cordova.build).toHaveBeenCalledWith({verbose: false, platforms: ["blackberry10"], options: ["-k", "abcd1234"]}); + }); + + it("will consume the first instance of -d", function () { + new CLI(["node", "cordova", "-d", "build", "blackberry10", "-k", "abcd1234", "-d"]); + expect(cordova.build).toHaveBeenCalledWith({verbose: true, platforms: ["blackberry10"], options: ["-k", "abcd1234", "-d"]}); + }); + + it("will consume the first instance of --verbose", function () { + new CLI(["node", "cordova", "--verbose", "build", "blackberry10", "-k", "abcd1234", "--verbose"]); + expect(cordova.build).toHaveBeenCalledWith({verbose: true, platforms: ["blackberry10"], options: ["-k", "abcd1234", "--verbose"]}); + }); + + it("will consume the first instance of either --verbose of -d", function () { + new CLI(["node", "cordova", "--verbose", "build", "blackberry10", "-k", "abcd1234", "-d"]); + expect(cordova.build).toHaveBeenCalledWith({verbose: true, platforms: ["blackberry10"], options: ["-k", "abcd1234", "-d"]}); + }); + + it("will consume the first instance of either --verbose of -d", function () { + new CLI(["node", "cordova", "-d", "build", "blackberry10", "-k", "abcd1234", "--verbose"]); + expect(cordova.build).toHaveBeenCalledWith({verbose: true, platforms: ["blackberry10"], options: ["-k", "abcd1234", "--verbose"]}); + }); + }); + + describe("plugin", function () { + beforeEach(function () { + spyOn(cordova, "plugin"); + }); + + afterEach(function () { + cordova.removeAllListeners(); + }); + + it("will call command with all arguments passed through", function () { + new CLI(["node", "cordova", "plugin", "add", "facebook", "--variable", "FOO=foo"]); + expect(cordova.plugin).toHaveBeenCalledWith("add", ["facebook", "--variable", "FOO=foo"]); + }); + }); +}); http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c775d2ab/src/cli.js ---------------------------------------------------------------------- diff --git a/src/cli.js b/src/cli.js index 16c818c..296ccda 100755 --- a/src/cli.js +++ b/src/cli.js @@ -3,8 +3,8 @@ var optimist = require('optimist'), plugman = require('plugman'), platforms = require("../platforms"); -module.exports = function CLI(args) { - args = optimist(args) +module.exports = function CLI(inputArgs) { + args = optimist(inputArgs) .boolean('d') .boolean('verbose') .boolean('v') @@ -15,13 +15,13 @@ module.exports = function CLI(args) { return console.log(require('../package').version); } - var tokens = args._.slice(2), + var tokens = inputArgs.slice(2), opts = { platforms: [], options: [], verbose: (args.d || args.verbose) }, - cmd = tokens && tokens.length ? tokens[0] : undefined; + cmd; // provide clean output on exceptions rather than dumping a stack trace process.on('uncaughtException', function(err){ @@ -39,13 +39,22 @@ module.exports = function CLI(args) { cordova.on('warn', console.warn); plugman.on('log', console.log); plugman.on('warn', console.warn); + //Remove the corresponding token + if(args.d && args.verbose) { + tokens.splice(Math.min(tokens.indexOf("-d"), tokens.indexOf("--verbose")), 1); + } else if (args.d) { + tokens.splice(tokens.indexOf("-d"), 1); + } else if (args.verbose) { + tokens.splice(tokens.indexOf("--verbose"), 1); + } + } + cmd = tokens && tokens.length ? tokens.splice(0,1) : undefined; if (cmd === undefined) { return cordova.help(); } - tokens = tokens.slice(1); if (cordova.hasOwnProperty(cmd)) { if (cmd == 'emulate' || cmd == 'build' || cmd == 'prepare' || cmd == 'compile' || cmd == 'run') { // Filter all non-platforms into options
