You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by fi...@apache.org on 2013/07/31 19:38:56 UTC
[2/2] git commit: [CB-4273] Allows arguments pass-through to platform
scripts - Splits platforms from options in the bin/cordova script effectively
changing the JS interface
[CB-4273] Allows arguments pass-through to platform scripts
- Splits platforms from options in the bin/cordova script effectively
changing the JS interface
Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/c39b7018
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/c39b7018
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/c39b7018
Branch: refs/heads/master
Commit: c39b701840b5f34426e130ba6fec4e8004fd7710
Parents: bd1e9c8
Author: Jeffrey Heifetz <jh...@blackberry.com>
Authored: Fri Jul 26 17:27:42 2013 -0400
Committer: Fil Maj <ma...@gmail.com>
Committed: Wed Jul 31 10:37:42 2013 -0700
----------------------------------------------------------------------
bin/cordova | 37 +++++++++++++++-------
spec/build.spec.js | 17 +++++++---
spec/compile.spec.js | 11 +++++--
spec/emulate.spec.js | 11 +++++--
spec/plugin.spec.js | 20 +++++++-----
spec/prepare.spec.js | 8 ++---
spec/run.spec.js | 11 +++++--
src/build.js | 47 ++++++++++++----------------
src/compile.js | 48 ++++++++++++----------------
src/create.js | 10 ++++++
src/emulate.js | 50 ++++++++++++------------------
src/plugin.js | 79 +++++++++++++++++++++++++++++++++++++----------
src/prepare.js | 46 +++++++++++----------------
src/run.js | 50 ++++++++++++------------------
src/util.js | 41 ++++++++++++++++++++++++
15 files changed, 290 insertions(+), 196 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/bin/cordova
----------------------------------------------------------------------
diff --git a/bin/cordova b/bin/cordova
index d42ed91..63a3a90 100755
--- a/bin/cordova
+++ b/bin/cordova
@@ -1,14 +1,21 @@
#!/usr/bin/env node
var tokens = process.argv.slice(2, process.argv.length),
cordova= require('../cordova'),
- plugman= require('plugman');
-
-var cmd, version = false, verbose = false, current;
+ plugman= require('plugman'),
+ platforms= require("../platforms"),
+ opts = {
+ platforms: [],
+ options: [],
+ verbose: false
+ },
+ cmd,
+ version = false,
+ current;
while (current = tokens.shift()) {
if (current[0] == '-') {
if (current.indexOf('version') > -1 || current[1] == 'v') version = true;
- if (current.indexOf('verbose') > -1 || current[1] == 'd') verbose = true;
+ if (current.indexOf('verbose') > -1 || current[1] == 'd') opts.verbose = true;
} else {
cmd = current;
break;
@@ -17,7 +24,7 @@ while (current = tokens.shift()) {
// provide clean output on exceptions rather than dumping a stack trace
process.on('uncaughtException', function(err){
- if (verbose) {
+ if (opts.verbose) {
console.error(err.stack);
} else {
console.error(err);
@@ -25,7 +32,8 @@ process.on('uncaughtException', function(err){
process.exit(1);
});
cordova.on('results', console.log);
-if (verbose) {
+
+if (opts.verbose) {
cordova.on('log', console.log);
cordova.on('warn', console.warn);
plugman.on('log', console.log);
@@ -37,17 +45,22 @@ if (version) {
} else if (cmd === undefined) {
cordova.help();
} else if (cordova.hasOwnProperty(cmd)) {
- var opts = Array.prototype.slice.call(tokens, 0);
- if (cmd == 'create' || cmd == 'docs' || cmd == 'serve') {
+ if (cmd == 'create' || cmd == 'serve') {
+ opts = Array.prototype.slice.call(tokens, 0);
cordova[cmd].apply(this, opts);
} else if (cmd == 'emulate' || cmd == 'build' || cmd == 'prepare' || cmd == 'compile' || cmd == 'run') {
+ //Filter all non-platforms into options
+ Array.prototype.slice.call(tokens, 0).forEach(function(option, index) {
+ if (platforms.hasOwnProperty(option)) {
+ opts.platforms.push(option);
+ } else {
+ opts.options.push(option);
+ }
+ });
cordova[cmd].call(this, opts);
} else {
// platform or plugin cmds
- if (tokens.length > 2) {
- opts = [tokens.shift()];
- opts.push(tokens);
- }
+ opts = Array.prototype.slice.call(tokens, 0);
cordova[cmd].apply(this, opts);
}
} else {
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/spec/build.spec.js
----------------------------------------------------------------------
diff --git a/spec/build.spec.js b/spec/build.spec.js
index 0a48293..801fc1b 100644
--- a/spec/build.spec.js
+++ b/spec/build.spec.js
@@ -48,7 +48,7 @@ describe('build command', function() {
list_platforms.andReturn([]);
expect(function() {
cordova.build();
- }).toThrow('No platforms added! `cordova platform add <platform>` to add a platform.');
+ }).toThrow('No platforms added to this project. Please use `cordova platform add <platform>`.');
});
it('should not run outside of a Cordova-based project', function() {
is_cordova.andReturn(false);
@@ -61,8 +61,15 @@ describe('build command', function() {
describe('success', function() {
it('should run inside a Cordova-based project with at least one added platform and call both prepare and compile', function(done) {
cordova.build(['android','ios'], function(err) {
- expect(prepare_spy).toHaveBeenCalledWith(['android', 'ios'], jasmine.any(Function));
- expect(compile_spy).toHaveBeenCalledWith(['android', 'ios'], jasmine.any(Function));
+ expect(prepare_spy).toHaveBeenCalledWith({verbose: false, platforms: ['android', 'ios'], options: []}, jasmine.any(Function));
+ expect(compile_spy).toHaveBeenCalledWith({verbose: false, platforms: ['android', 'ios'], options: []}, jasmine.any(Function));
+ done();
+ });
+ });
+ it('should pass down options', function(done) {
+ cordova.build({platforms: ['android'], options: ['--release']}, function(err) {
+ expect(prepare_spy).toHaveBeenCalledWith({platforms: ['android'], options: ["--release"]}, jasmine.any(Function));
+ expect(compile_spy).toHaveBeenCalledWith({platforms: ['android'], options: ["--release"]}, jasmine.any(Function));
done();
});
});
@@ -72,11 +79,11 @@ describe('build command', function() {
describe('when platforms are added', function() {
it('should fire before hooks through the hooker module', function() {
cordova.build(['android', 'ios']);
- expect(fire).toHaveBeenCalledWith('before_build', {platforms:['android', 'ios']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('before_build', {verbose: false, platforms:['android', 'ios'], options: []}, jasmine.any(Function));
});
it('should fire after hooks through the hooker module', function(done) {
cordova.build('android', function() {
- expect(fire).toHaveBeenCalledWith('after_build', {platforms:['android']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('after_build', {verbose: false, platforms:['android'], options: []}, jasmine.any(Function));
done();
});
});
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/spec/compile.spec.js
----------------------------------------------------------------------
diff --git a/spec/compile.spec.js b/spec/compile.spec.js
index 54226f2..975fcdc 100644
--- a/spec/compile.spec.js
+++ b/spec/compile.spec.js
@@ -61,17 +61,24 @@ describe('compile command', function() {
done();
});
});
+ it('should pass down optional parameters', function (done) {
+ cordova.compile({platforms:["blackberry10"], options:["--release"]}, function (err) {
+ var buildCommand = path.join(project_dir, 'platforms', 'blackberry10', 'cordova', 'build');
+ expect(exec).toHaveBeenCalledWith('"' + buildCommand + '" --release', jasmine.any(Object), jasmine.any(Function));
+ done();
+ });
+ });
});
describe('hooks', function() {
describe('when platforms are added', function() {
it('should fire before hooks through the hooker module', function() {
cordova.compile(['android', 'ios']);
- expect(fire).toHaveBeenCalledWith('before_compile', {platforms:['android', 'ios']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('before_compile', {verbose: false, platforms:['android', 'ios'], options: []}, jasmine.any(Function));
});
it('should fire after hooks through the hooker module', function(done) {
cordova.compile('android', function() {
- expect(fire).toHaveBeenCalledWith('after_compile', {platforms:['android']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('after_compile', {verbose: false, platforms:['android'], options: []}, jasmine.any(Function));
done();
});
});
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/spec/emulate.spec.js
----------------------------------------------------------------------
diff --git a/spec/emulate.spec.js b/spec/emulate.spec.js
index 03151e3..807553c 100644
--- a/spec/emulate.spec.js
+++ b/spec/emulate.spec.js
@@ -65,17 +65,24 @@ describe('emulate command', function() {
done();
});
});
+ it('should pass down options', function(done) {
+ cordova.emulate({platforms: ['ios'], options:["--optionTastic"]}, function(err) {
+ expect(prepare_spy).toHaveBeenCalledWith(['ios'], jasmine.any(Function));
+ expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'ios', 'cordova', 'run') + '" --optionTastic', jasmine.any(Object), jasmine.any(Function));
+ done();
+ });
+ });
});
describe('hooks', function() {
describe('when platforms are added', function() {
it('should fire before hooks through the hooker module', function() {
cordova.emulate(['android', 'ios']);
- expect(fire).toHaveBeenCalledWith('before_emulate', {platforms:['android', 'ios']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('before_emulate', {verbose: false, platforms:['android', 'ios'], options: []}, jasmine.any(Function));
});
it('should fire after hooks through the hooker module', function(done) {
cordova.emulate('android', function() {
- expect(fire).toHaveBeenCalledWith('after_emulate', {platforms:['android']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('after_emulate', {verbose: false, platforms:['android'], options: []}, jasmine.any(Function));
done();
});
});
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/spec/plugin.spec.js
----------------------------------------------------------------------
diff --git a/spec/plugin.spec.js b/spec/plugin.spec.js
index f9265ce..f9c679a 100644
--- a/spec/plugin.spec.js
+++ b/spec/plugin.spec.js
@@ -94,7 +94,7 @@ describe('plugin command', function() {
expect(is_cordova).toHaveBeenCalled();
});
- describe('`ls`', function() {
+ describe('`ls`', function() {
afterEach(function() {
cordova.removeAllListeners('results');
});
@@ -125,17 +125,23 @@ describe('plugin command', function() {
it('should call plugman.fetch for each plugin', function() {
cordova.plugin('add', sample_plugins);
sample_plugins.forEach(function(p) {
- expect(plugman_fetch).toHaveBeenCalledWith(p, plugins_dir, {}, jasmine.any(Function));
+ expect(plugman_fetch).toHaveBeenCalledWith(p, plugins_dir, {}, jasmine.any(Function));
});
});
it('should call plugman.install, for each plugin, for every platform', function() {
cordova.plugin('add', sample_plugins);
sample_plugins.forEach(function(plug) {
supported_platforms.forEach(function(plat) {
- expect(plugman_install).toHaveBeenCalledWith((plat=='blackberry'?'blackberry10':plat), path.join(project_dir, 'platforms', plat), plug, plugins_dir, jasmine.any(Object));
+ expect(plugman_install).toHaveBeenCalledWith((plat=='blackberry'?'blackberry10':plat), path.join(project_dir, 'platforms', plat), plug, plugins_dir, jasmine.any(Object));
});
});
});
+ it('should pass down variables into plugman', function() {
+ cordova.plugin('add', "one", "--variable", "foo=bar");
+ supported_platforms.forEach(function(plat) {
+ expect(plugman_install).toHaveBeenCalledWith((plat=='blackberry'?'blackberry10':plat), path.join(project_dir, 'platforms', plat), "one", plugins_dir, {www_dir: jasmine.any(String), cli_variables: { FOO: "bar"}});
+ });
+ });
it('should trigger callback without an error', function(done) {
cordova.plugin('add', sample_plugins, function(e) {
expect(e).not.toBeDefined();
@@ -204,18 +210,18 @@ describe('plugin command', function() {
describe('remove (rm) hooks', function() {
it('should fire before hooks through the hooker module', function() {
cordova.plugin('rm', 'two');
- expect(fire).toHaveBeenCalledWith('before_plugin_rm', {plugins:['two']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('before_plugin_rm', {plugins:['two'], options: []}, jasmine.any(Function));
});
it('should fire after hooks through the hooker module', function() {
cordova.plugin('rm', 'one');
- expect(fire).toHaveBeenCalledWith('after_plugin_rm', {plugins:['one']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('after_plugin_rm', {plugins:['one'], options:[]}, jasmine.any(Function));
});
});
describe('add hooks', function() {
it('should fire before and after hooks through the hooker module', function() {
cordova.plugin('add', 'android');
- expect(fire).toHaveBeenCalledWith('before_plugin_add', {plugins:['android']}, jasmine.any(Function));
- expect(fire).toHaveBeenCalledWith('after_plugin_add', {plugins:['android']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('before_plugin_add', {plugins:['android'], options: []}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('after_plugin_add', {plugins:['android'], options: []}, jasmine.any(Function));
});
});
});
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/spec/prepare.spec.js
----------------------------------------------------------------------
diff --git a/spec/prepare.spec.js b/spec/prepare.spec.js
index c9b184e..19710c9 100644
--- a/spec/prepare.spec.js
+++ b/spec/prepare.spec.js
@@ -30,7 +30,7 @@ var cordova = require('../cordova'),
var project_dir = '/some/path';
var supported_platforms = Object.keys(platforms).filter(function(p) { return p != 'www'; });
-var supported_platforms_paths = supported_platforms.map(function(p) { return path.join(project_dir, 'platforms', p, 'www'); });
+var supported_platforms_paths = supported_platforms.map(function(p) { return path.join(project_dir, 'platforms', p, 'www'); });
describe('prepare command', function() {
var is_cordova, list_platforms, fire, config_parser, parsers = {}, plugman_prepare, find_plugins, plugman_get_json, load;
@@ -71,7 +71,7 @@ describe('prepare command', function() {
}).toThrow('No platforms added to this project. Please use `cordova platform add <platform>`.');
});
});
-
+
describe('success', function() {
it('should run inside a Cordova-based project by calling util.isCordova', function() {
cordova.prepare();
@@ -139,11 +139,11 @@ describe('prepare command', function() {
describe('when platforms are added', function() {
it('should fire before hooks through the hooker module, and pass in platforms and paths as data object', function() {
cordova.prepare();
- expect(fire).toHaveBeenCalledWith('before_prepare', {platforms:supported_platforms, paths:supported_platforms_paths}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('before_prepare', {verbose: false, platforms:supported_platforms, options: [], paths:supported_platforms_paths}, jasmine.any(Function));
});
it('should fire after hooks through the hooker module, and pass in platforms and paths as data object', function(done) {
cordova.prepare('android', function() {
- expect(fire).toHaveBeenCalledWith('after_prepare', {platforms:['android'], paths:[path.join(project_dir, 'platforms', 'android', 'www')]}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('after_prepare', {verbose: false, platforms:['android'], options: [], paths:[path.join(project_dir, 'platforms', 'android', 'www')]}, jasmine.any(Function));
done();
});
});
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/spec/run.spec.js
----------------------------------------------------------------------
diff --git a/spec/run.spec.js b/spec/run.spec.js
index 5d16dfd..df113bd 100644
--- a/spec/run.spec.js
+++ b/spec/run.spec.js
@@ -65,17 +65,24 @@ describe('run command', function() {
done();
});
});
+ it('should pass down parameters', function(done) {
+ cordova.run({platforms: ['blackberry10'], options:['--device', '--password', '1q1q']}, function(err) {
+ expect(prepare_spy).toHaveBeenCalledWith(['blackberry10'], jasmine.any(Function));
+ expect(exec).toHaveBeenCalledWith('"' + path.join(project_dir, 'platforms', 'blackberry10', 'cordova', 'run') + '" --device --password 1q1q', jasmine.any(Object), jasmine.any(Function));
+ done();
+ });
+ });
});
describe('hooks', function() {
describe('when platforms are added', function() {
it('should fire before hooks through the hooker module', function() {
cordova.run(['android', 'ios']);
- expect(fire).toHaveBeenCalledWith('before_run', {platforms:['android', 'ios']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('before_run', {verbose: false, platforms:['android', 'ios'], options: []}, jasmine.any(Function));
});
it('should fire after hooks through the hooker module', function(done) {
cordova.run('android', function() {
- expect(fire).toHaveBeenCalledWith('after_run', {platforms:['android']}, jasmine.any(Function));
+ expect(fire).toHaveBeenCalledWith('after_run', {verbose: false, platforms:['android'], options: []}, jasmine.any(Function));
done();
});
});
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/src/build.js
----------------------------------------------------------------------
diff --git a/src/build.js b/src/build.js
index 28874bb..5883762 100644
--- a/src/build.js
+++ b/src/build.js
@@ -16,7 +16,7 @@
specific language governing permissions and limitations
under the License.
*/
-var util = require('./util'),
+var cordova_util = require('./util'),
path = require('path'),
fs = require('fs'),
shell = require('shelljs'),
@@ -24,51 +24,42 @@ var util = require('./util'),
events = require('./events'),
n = require('ncallbacks');
-module.exports = function build(platformList, callback) {
- var projectRoot = util.isCordova(process.cwd());
- if (!projectRoot) {
- var err = new Error('Current working directory is not a Cordova-based project.');
- if (callback) callback(err);
- else throw err;
- return;
- }
+module.exports = function build(options, callback) {
+ var projectRoot = cordova_util.isCordova(process.cwd());
- if (arguments.length === 0 || (platformList instanceof Array && platformList.length === 0)) {
- platformList = util.listPlatforms(projectRoot);
- } else if (typeof platformList == 'string') platformList = [platformList];
- else if (platformList instanceof Function && callback === undefined) {
- callback = platformList;
- platformList = util.listPlatforms(projectRoot);
+ if (options instanceof Function && callback === undefined) {
+ callback = options;
+ options = {
+ verbose: false,
+ platforms: [],
+ options: []
+ };
}
- if (platformList.length === 0) {
- var err = new Error('No platforms added! `cordova platform add <platform>` to add a platform.');
- if (callback) callback(err);
- else throw err;
- return;
+ options = cordova_util.preProcessOptions(options);
+ if (options.constructor.name === "Error") {
+ if (callback) return callback(options);
+ else throw options;
}
// fire build hooks
var hooks = new hooker(projectRoot);
- var opts = {
- platforms:platformList
- };
- hooks.fire('before_build', opts, function(err) {
+ hooks.fire('before_build', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
- require('../cordova').prepare(platformList, function(err) {
+ require('../cordova').prepare(options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
- require('../cordova').compile(platformList, function(err) {
+ require('../cordova').compile(options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
- hooks.fire('after_build', opts, function(err) {
+ hooks.fire('after_build', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
@@ -81,5 +72,5 @@ module.exports = function build(platformList, callback) {
}
});
}
- });;
+ });
};
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/src/compile.js
----------------------------------------------------------------------
diff --git a/src/compile.js b/src/compile.js
index a99f109..e1ffde7 100644
--- a/src/compile.js
+++ b/src/compile.js
@@ -25,8 +25,8 @@ var cordova_util = require('./util'),
events = require('./events'),
n = require('ncallbacks');
-function shell_out_to_build(projectRoot, platform, error_callback, done) {
- var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 'build') + '"';
+function shell_out_to_build(projectRoot, platform, options, error_callback, done) {
+ var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 'build') + (options.length ? '" ' + options.join(" ") : '"');
events.emit('log', 'Compiling platform "' + platform + '" with command "' + cmd + '" (output to follow)...');
shell.exec(cmd, {silent:true, async:true}, function(code, output) {
events.emit('log', output);
@@ -41,42 +41,32 @@ function shell_out_to_build(projectRoot, platform, error_callback, done) {
});
}
-module.exports = function compile(platformList, callback) {
+module.exports = function compile(options, callback) {
var projectRoot = cordova_util.isCordova(process.cwd());
- if (!projectRoot) {
- var err = new Error('Current working directory is not a Cordova-based project.');
- if (callback) callback(err);
- else throw err;
- return;
+ if (options instanceof Function && callback === undefined) {
+ callback = options;
+ options = {
+ verbose: false,
+ platforms: [],
+ options: []
+ };
}
- if (arguments.length === 0 || (platformList instanceof Array && platformList.length === 0)) {
- platformList = cordova_util.listPlatforms(projectRoot);
- } else if (typeof platformList == 'string') platformList = [platformList];
- else if (platformList instanceof Function && callback === undefined) {
- callback = platformList;
- platformList = cordova_util.listPlatforms(projectRoot);
+ options = cordova_util.preProcessOptions(options);
+ if (options.constructor.name === "Error") {
+ if (callback) return callback(options);
+ else throw options;
}
- if (platformList.length === 0) {
- var err = new Error('No platforms added to this project. Please use `cordova platform add <platform>`.');
- if (callback) callback(err);
- else throw err;
- return;
- }
- var opts = {
- platforms:platformList
- };
-
var hooks = new hooker(projectRoot);
- hooks.fire('before_compile', opts, function(err) {
+ hooks.fire('before_compile', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
- var end = n(platformList.length, function() {
- hooks.fire('after_compile', opts, function(err) {
+ var end = n(options.platforms.length, function() {
+ hooks.fire('after_compile', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
@@ -87,9 +77,9 @@ module.exports = function compile(platformList, callback) {
});
// Iterate over each added platform
- platformList.forEach(function(platform) {
+ options.platforms.forEach(function(platform) {
try {
- shell_out_to_build(projectRoot, platform, callback, end);
+ shell_out_to_build(projectRoot, platform, options.options, callback, end);
} catch(e) {
if (callback) callback(e);
else throw e;
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/src/create.js
----------------------------------------------------------------------
diff --git a/src/create.js b/src/create.js
index b8c54de..9c25ab6 100644
--- a/src/create.js
+++ b/src/create.js
@@ -36,6 +36,8 @@ var DEFAULT_NAME = "HelloCordova",
* create(dir, id, name) - you get the gist
**/
module.exports = function create (dir, id, name, callback) {
+ var options = [];
+
if (arguments.length === 0) {
return help();
}
@@ -44,7 +46,10 @@ module.exports = function create (dir, id, name, callback) {
var args = Array.prototype.slice.call(arguments, 0);
if (typeof args[args.length-1] == 'function') {
callback = args.pop();
+ } else if (typeof callback !== 'function') {
+ callback = undefined;
}
+
if (args.length === 0) {
dir = process.cwd();
id = DEFAULT_ID;
@@ -54,6 +59,11 @@ module.exports = function create (dir, id, name, callback) {
name = DEFAULT_NAME;
} else if (args.length == 2) {
name = DEFAULT_NAME;
+ } else {
+ dir = args.shift();
+ id = args.shift();
+ name = args.shift();
+ options = args;
}
// Make absolute.
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/src/emulate.js
----------------------------------------------------------------------
diff --git a/src/emulate.js b/src/emulate.js
index b9cbe70..b37471a 100644
--- a/src/emulate.js
+++ b/src/emulate.js
@@ -27,8 +27,8 @@ var cordova_util = require('./util'),
hooker = require('./hooker'),
util = require('util');
-function shell_out_to_emulate(root, platform, error_callback, done) {
- var cmd = '"' + path.join(root, 'platforms', platform, 'cordova', 'run') + '" --emulator';
+function shell_out_to_emulate(root, platform, options, error_callback, done) {
+ var cmd = '"' + path.join(root, 'platforms', platform, 'cordova', 'run') + '" ' + (options.length ? options.join(" ") : '--emulator');
events.emit('log', 'Running on emulator for platform "' + platform + '" via command "' + cmd + '" (output to follow)...');
shell.exec(cmd, {silent:true, async:true}, function(code, output) {
events.emit('log', output);
@@ -43,42 +43,32 @@ function shell_out_to_emulate(root, platform, error_callback, done) {
});
}
-module.exports = function emulate (platformList, callback) {
+module.exports = function emulate (options, callback) {
var projectRoot = cordova_util.isCordova(process.cwd());
- if (!projectRoot) {
- var err = new Error('Current working directory is not a Cordova-based project.');
- if (callback) callback(err);
- else throw err;
- return;
+ if (options instanceof Function && callback === undefined) {
+ callback = options;
+ options = {
+ verbose: false,
+ platforms: [],
+ options: []
+ };
}
- if (arguments.length === 0 || (platformList instanceof Array && platformList.length === 0)) {
- platformList = cordova_util.listPlatforms(projectRoot);
- } else if (typeof platformList == 'string') platformList = [platformList];
- else if (platformList instanceof Function && callback === undefined) {
- callback = platformList;
- platformList = cordova_util.listPlatforms(projectRoot);
+ options = cordova_util.preProcessOptions(options);
+ if (options.constructor.name === "Error") {
+ if (callback) return callback(options)
+ else throw options;
}
- if (platformList.length === 0) {
- var err = new Error('No platforms added to this project. Please use `cordova platform add <platform>`.');
- if (callback) callback(err);
- else throw err;
- return;
- }
- var opts = {
- platforms:platformList
- };
-
var hooks = new hooker(projectRoot);
- hooks.fire('before_emulate', opts, function(err) {
+ hooks.fire('before_emulate', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
- var end = n(platformList.length, function() {
- hooks.fire('after_emulate', opts, function(err) {
+ var end = n(options.platforms.length, function() {
+ hooks.fire('after_emulate', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
@@ -89,14 +79,14 @@ module.exports = function emulate (platformList, callback) {
});
// Run a prepare first!
- require('../cordova').prepare(platformList, function(err) {
+ require('../cordova').prepare(options.platforms, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
- platformList.forEach(function(platform) {
+ options.platforms.forEach(function(platform) {
try {
- shell_out_to_emulate(projectRoot, platform, callback, end);
+ shell_out_to_emulate(projectRoot, platform, options.options, callback, end);
} catch(e) {
if (callback) callback(e);
else throw e;
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/src/plugin.js
----------------------------------------------------------------------
diff --git a/src/plugin.js b/src/plugin.js
index 662e3e3..842cdf2 100644
--- a/src/plugin.js
+++ b/src/plugin.js
@@ -28,15 +28,28 @@ var cordova_util = require('./util'),
events = require('./events');
module.exports = function plugin(command, targets, callback) {
- var projectRoot = cordova_util.isCordova(process.cwd());
+ var projectRoot = cordova_util.isCordova(process.cwd()),
+ err;
if (!projectRoot) {
- var err = new Error('Current working directory is not a Cordova-based project.');
+ err = new Error('Current working directory is not a Cordova-based project.');
if (callback) callback(err);
else throw err;
return;
}
- if (arguments.length === 0) command = 'ls';
+
+ if (arguments.length === 0){
+ command = 'ls';
+ targets = [];
+ } else if (arguments.length > 3) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ if (typeof args[args.length - 1] === "function") {
+ callback = args.pop();
+ } else {
+ callback = undefined;
+ targets = args.slice(1);
+ }
+ }
var hooks = new hooker(projectRoot);
var platformList = cordova_util.listPlatforms(projectRoot);
@@ -51,19 +64,34 @@ module.exports = function plugin(command, targets, callback) {
}
} else {
if (command == 'add' || command == 'rm') {
- var err = new Error('You need to qualify `add` or `remove` with one or more plugins!');
+ err = new Error('You need to qualify `add` or `remove` with one or more plugins!');
if (callback) return callback(err);
else throw err;
+ } else {
+ targets = [];
}
}
var opts = {
- plugins:targets
+ plugins: [],
+ options: []
};
+ //Split targets between plugins and options
+ //Assume everything after a token with a '-' is an option
+ var i;
+ for (i = 0; i < targets.length; i++) {
+ if (targets[i].match(/^-/)) {
+ opts.options = targets.slice(i);
+ break;
+ } else {
+ opts.plugins.push(targets[i]);
+ }
+ }
+
switch(command) {
- case 'add':
- var end = n(targets.length, function() {
+ case 'add':
+ var end = n(opts.plugins.length, function() {
hooks.fire('after_plugin_add', opts, function(err) {
if (err) {
if (callback) callback(err);
@@ -78,7 +106,7 @@ module.exports = function plugin(command, targets, callback) {
if (callback) callback(err);
else throw err;
} else {
- targets.forEach(function(target, index) {
+ opts.plugins.forEach(function(target, index) {
var pluginsDir = path.join(projectRoot, 'plugins');
if (target[target.length - 1] == path.sep) {
@@ -89,17 +117,34 @@ module.exports = function plugin(command, targets, callback) {
events.emit('log', 'Calling plugman.fetch on plugin "' + target + '"');
plugman.fetch(target, pluginsDir, {}, function(err, dir) {
if (err) {
- var err = new Error('Error fetching plugin: ' + err);
+ err = new Error('Error fetching plugin: ' + err);
if (callback) callback(err);
else throw err;
} else {
// Iterate over all platforms in the project and install the plugin.
platformList.forEach(function(platform) {
- var platformRoot = path.join(projectRoot, 'platforms', platform);
- var parser = new platforms[platform].parser(platformRoot);
- events.emit('log', 'Calling plugman.install on plugin "' + dir + '" for platform "' + platform + '"');
- plugman.install(platform, platformRoot,
- path.basename(dir), pluginsDir, { www_dir: parser.staging_dir() });
+ var platformRoot = path.join(projectRoot, 'platforms', platform),
+ parser = new platforms[platform].parser(platformRoot),
+ options = {
+ www_dir: parser.staging_dir(),
+ cli_variables: {}
+ },
+ tokens,
+ key,
+ i;
+ //parse variables into cli_variables
+ for (i=0; i< opts.options.length; i++) {
+ if (opts.options[i] === "--variable" && typeof opts.options[++i] === "string") {
+ tokens = opts.options[i].split('=');
+ key = tokens.shift().toUpperCase();
+ if (/^[\w-_]+$/.test(key)) {
+ options.cli_variables[key] = tokens.join('=');
+ }
+ }
+ }
+
+ events.emit('log', 'Calling plugman.install on plugin "' + dir + '" for platform "' + platform + '" with options "' + JSON.stringify(options) + '"');
+ plugman.install(platform, platformRoot, path.basename(dir), pluginsDir, options);
});
end();
}
@@ -110,7 +155,7 @@ module.exports = function plugin(command, targets, callback) {
break;
case 'rm':
case 'remove':
- var end = n(targets.length, function() {
+ var end = n(opts.plugins.length, function() {
hooks.fire('after_plugin_rm', opts, function(err) {
if (err) {
if (callback) callback(err);
@@ -125,7 +170,7 @@ module.exports = function plugin(command, targets, callback) {
if (callback) callback(err);
else throw err;
} else {
- targets.forEach(function(target, index) {
+ opts.plugins.forEach(function(target, index) {
// Check if we have the plugin.
if (plugins.indexOf(target) > -1) {
var targetPath = path.join(pluginPath, target);
@@ -164,7 +209,7 @@ module.exports = function plugin(command, targets, callback) {
if(callback) callback(err);
else throw err;
} else {
- plugman.search(targets, function(err, plugins) {
+ plugman.search(opts.plugins, function(err, plugins) {
if(err) return console.log(err);
for(var plugin in plugins) {
console.log();
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/src/prepare.js
----------------------------------------------------------------------
diff --git a/src/prepare.js b/src/prepare.js
index 178da02..fa53443 100644
--- a/src/prepare.js
+++ b/src/prepare.js
@@ -32,51 +32,41 @@ var cordova_util = require('./util'),
plugman = require('plugman'),
util = require('util');
-module.exports = function prepare(platformList, callback) {
+module.exports = function prepare(options, callback) {
var projectRoot = cordova_util.isCordova(process.cwd());
- if (!projectRoot) {
- var err = new Error('Current working directory is not a Cordova-based project.');
- if (callback) callback(err);
- else throw err;
- return;
+ if (options instanceof Function && callback === undefined) {
+ callback = options;
+ options = {
+ verbose: false,
+ platforms: [],
+ options: []
+ };
}
- if (arguments.length === 0 || (platformList instanceof Array && platformList.length === 0)) {
- platformList = cordova_util.listPlatforms(projectRoot);
- } else if (typeof platformList == 'string') platformList = [platformList];
- else if (platformList instanceof Function && callback === undefined) {
- callback = platformList;
- platformList = cordova_util.listPlatforms(projectRoot);
- }
-
- if (platformList.length === 0) {
- var err = new Error('No platforms added to this project. Please use `cordova platform add <platform>`.');
- if (callback) callback(err);
- else throw err;
- return;
+ options = cordova_util.preProcessOptions(options);
+ if (options.constructor.name === "Error") {
+ if (callback) return callback(options)
+ else throw options;
}
var xml = cordova_util.projectConfig(projectRoot);
- var opts = {
- platforms:platformList
- };
- var paths = platformList.map(function(p) {
+ var paths = options.platforms.map(function(p) {
var platform_path = path.join(projectRoot, 'platforms', p);
var parser = (new platforms[p].parser(platform_path));
return parser.www_dir();
});
- opts.paths = paths;
+ options.paths = paths;
var hooks = new hooker(projectRoot);
- hooks.fire('before_prepare', opts, function(err) {
+ hooks.fire('before_prepare', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
var cfg = new cordova_util.config_parser(xml);
- var end = n(platformList.length, function() {
- hooks.fire('after_prepare', opts, function(err) {
+ var end = n(options.platforms.length, function() {
+ hooks.fire('after_prepare', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
@@ -87,7 +77,7 @@ module.exports = function prepare(platformList, callback) {
});
// Iterate over each added platform
- platformList.forEach(function(platform) {
+ options.platforms.forEach(function(platform) {
lazy_load.based_on_config(projectRoot, platform, function(err) {
if (err) {
if (callback) callback(err);
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/src/run.js
----------------------------------------------------------------------
diff --git a/src/run.js b/src/run.js
index 4bca403..3d7053b 100644
--- a/src/run.js
+++ b/src/run.js
@@ -25,8 +25,8 @@ var cordova_util = require('./util'),
events = require('./events'),
n = require('ncallbacks');
-function shell_out_to_run(projectRoot, platform, error_callback, done) {
- var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 'run') + '" --device';
+function shell_out_to_run(projectRoot, platform, options, error_callback, done) {
+ var cmd = '"' + path.join(projectRoot, 'platforms', platform, 'cordova', 'run') + '" ' + ( options.length ? options.join(" ") : '--device');
// TODO: inconsistent API for BB10 run command
/* if (platform == 'blackberry') {
var bb_project = path.join(projectRoot, 'platforms', 'blackberry')
@@ -56,42 +56,32 @@ function shell_out_to_run(projectRoot, platform, error_callback, done) {
});
}
-module.exports = function run(platformList, callback) {
+module.exports = function run(options, callback) {
var projectRoot = cordova_util.isCordova(process.cwd());
- if (!projectRoot) {
- var err = new Error('Current working directory is not a Cordova-based project.');
- if (callback) callback(err);
- else throw err;
- return;
+ if (options instanceof Function && callback === undefined) {
+ callback = options;
+ options = {
+ verbose: false,
+ platforms: [],
+ options: []
+ };
}
- if (arguments.length === 0 || (platformList instanceof Array && platformList.length === 0)) {
- platformList = cordova_util.listPlatforms(projectRoot);
- } else if (typeof platformList == 'string') platformList = [platformList];
- else if (platformList instanceof Function && callback === undefined) {
- callback = platformList;
- platformList = cordova_util.listPlatforms(projectRoot);
- }
-
- if (platformList.length === 0) {
- var err = new Error('No platforms added to this project. Please use `cordova platform add <platform>`.');
- if (callback) callback(err);
- else throw err;
- return;
+ options = cordova_util.preProcessOptions(options);
+ if (options.constructor.name === "Error") {
+ if (callback) return callback(options)
+ else throw options;
}
var hooks = new hooker(projectRoot);
- var opts = {
- platforms:platformList
- }
- hooks.fire('before_run', opts, function(err) {
+ hooks.fire('before_run', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
- var end = n(platformList.length, function() {
- hooks.fire('after_run', opts, function(err) {
+ var end = n(options.platforms.length, function() {
+ hooks.fire('after_run', options, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
@@ -102,14 +92,14 @@ module.exports = function run(platformList, callback) {
});
// Run a prepare first, then shell out to run
- require('../cordova').prepare(platformList, function(err) {
+ require('../cordova').prepare(options.platforms, function(err) {
if (err) {
if (callback) callback(err);
else throw err;
} else {
- platformList.forEach(function(platform) {
+ options.platforms.forEach(function(platform) {
try {
- shell_out_to_run(projectRoot, platform, callback, end);
+ shell_out_to_run(projectRoot, platform, options.options, callback, end);
} catch(e) {
if (callback) callback(e);
else throw e;
http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/c39b7018/src/util.js
----------------------------------------------------------------------
diff --git a/src/util.js b/src/util.js
index 8b843c2..858d20b 100644
--- a/src/util.js
+++ b/src/util.js
@@ -94,5 +94,46 @@ module.exports = {
},
projectConfig: function(projectDir) {
return path.join(projectDir, 'www', 'config.xml');
+ },
+ preProcessOptions: function (inputOptions) {
+ var projectRoot = this.isCordova(process.cwd()),
+ projectPlatforms = this.listPlatforms(projectRoot),
+ DEFAULT_OPTIONS = {
+ verbose: false,
+ platforms: [],
+ options: []
+ },
+ result = inputOptions || DEFAULT_OPTIONS;
+
+ if (!projectRoot) {
+ result = new Error('Current working directory is not a Cordova-based project.');
+ } else if (projectPlatforms.length === 0) {
+ result = new Error('No platforms added to this project. Please use `cordova platform add <platform>`.');
+ } else {
+ /**
+ * Current Desired Arguments
+ * options: {verbose: boolean, platforms: [String], options: [String]}
+ * Accepted Arguments
+ * platformList: [String] -- assume just a list of platforms
+ * platform: String -- assume this is a platform
+ */
+ if (Array.isArray(inputOptions)) {
+ result = {
+ verbose: false,
+ platforms: inputOptions,
+ options: []
+ };
+ } else if (typeof inputOptions === 'string') {
+ result = {
+ verbose: false,
+ platforms: [inputOptions],
+ options: []
+ };
+ }
+ if (!result.platforms || (result.platforms && result.platforms.length === 0) ) {
+ result.platforms = projectPlatforms;
+ }
+ }
+ return result;
}
};