Skip to content

Commit

Permalink
[CB-4532] Changes the CLI to only use optimist for verbose and version
Browse files Browse the repository at this point in the history
- Also adds limited unit tests for the CLI module
  • Loading branch information
Jeffrey Heifetz committed Aug 9, 2013
1 parent d0f44ca commit c775d2a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
79 changes: 79 additions & 0 deletions spec/cli.spec.js
Original file line number Diff line number Diff line change
@@ -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"]);
});
});
});
19 changes: 14 additions & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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){
Expand All @@ -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
Expand Down

0 comments on commit c775d2a

Please sign in to comment.