Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First run at pass through. Works for run command #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 25 additions & 12 deletions bin/cordova
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,15 +24,16 @@ 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);
}
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);
Expand All @@ -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 {
Expand Down
17 changes: 12 additions & 5 deletions spec/build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
});
});
Expand All @@ -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();
});
});
Expand Down
11 changes: 9 additions & 2 deletions spec/compile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
11 changes: 9 additions & 2 deletions spec/emulate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
20 changes: 13 additions & 7 deletions spec/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('plugin command', function() {
expect(is_cordova).toHaveBeenCalled();
});

describe('`ls`', function() {
describe('`ls`', function() {
afterEach(function() {
cordova.removeAllListeners('results');
});
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
});
});
});
Expand Down
8 changes: 4 additions & 4 deletions spec/prepare.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
});
Expand Down
11 changes: 9 additions & 2 deletions spec/run.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
47 changes: 19 additions & 28 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,50 @@
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'),
hooker = require('./hooker'),
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;
Expand All @@ -81,5 +72,5 @@ module.exports = function build(platformList, callback) {
}
});
}
});;
});
};