You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2014/01/23 21:25:30 UTC

git commit: CB-5782 Use CordovaError wherever relevant.

Updated Branches:
  refs/heads/master 584338bba -> 2676ff88a


CB-5782 Use CordovaError wherever relevant.

Other than the trivial changes that look like
new Error(...) -> new CordovaError(...)
there is also a change in util.js to throw the error instead of returning it
and associated changes in specs.


Project: http://git-wip-us.apache.org/repos/asf/cordova-cli/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-cli/commit/2676ff88
Tree: http://git-wip-us.apache.org/repos/asf/cordova-cli/tree/2676ff88
Diff: http://git-wip-us.apache.org/repos/asf/cordova-cli/diff/2676ff88

Branch: refs/heads/master
Commit: 2676ff88a8b547d9542bc51e9e0b18bd6966ef4d
Parents: 584338b
Author: Mark Koudritsky <ka...@gmail.com>
Authored: Tue Jan 21 21:21:35 2014 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Thu Jan 23 15:25:07 2014 -0500

----------------------------------------------------------------------
 spec/build.spec.js   | 50 ++++++++++++++++++++++++++++-------------------
 spec/compile.spec.js | 18 ++++++++++-------
 spec/emulate.spec.js | 20 +++++++++++--------
 spec/prepare.spec.js |  6 +++---
 spec/run.spec.js     |  4 ++--
 src/build.js         |  3 ---
 src/compile.js       |  3 ---
 src/create.js        |  3 ++-
 src/emulate.js       |  3 ---
 src/platform.js      | 21 ++++++++++----------
 src/plugin.js        |  9 +++++----
 src/prepare.js       |  3 ---
 src/run.js           |  3 ---
 src/util.js          |  4 ++--
 14 files changed, 78 insertions(+), 72 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/spec/build.spec.js
----------------------------------------------------------------------
diff --git a/spec/build.spec.js b/spec/build.spec.js
index 98b5ccc..a563cb8 100644
--- a/spec/build.spec.js
+++ b/spec/build.spec.js
@@ -53,30 +53,37 @@ describe('build command', function() {
         prepare_spy = spyOn(cordova.raw, 'prepare').andReturn(Q());
         compile_spy = spyOn(cordova.raw, 'compile').andReturn(Q());
     });
-    describe('failure', function(done) {
-        it('should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function() {
+    describe('failure', function() {
+        it('should not run inside a project with no platforms', function(done) {
             list_platforms.andReturn([]);
-            runs(function() {
-                buildPromise(cordova.raw.build());
-            });
-            waitsFor(function() { return result; }, 'promise never resolved', 500);
-            runs(function() {
-                expect(result).toEqual(new Error('No platforms added to this project. Please use `cordova platform add <platform>`.'));
-            });
+            Q().then(cordova.raw.build).then(function() {
+                expect('this call').toBe('fail');
+            }, function(err) {
+                expect(err.message).toEqual(
+                    'No platforms added to this project. Please use `cordova platform add <platform>`.'
+                )
+            }).fin(done);
         });
-        it('should not run outside of a Cordova-based project', function() {
+
+        it('should not run outside of a Cordova-based project', function(done) {
             is_cordova.andReturn(false);
-            wrapper(cordova.raw.build(), function() {
-                expect(result).toEqual(new Error('Current working directory is not a Cordova-based project.'));
-            });
+
+            Q().then(cordova.raw.build).then(function() {
+                expect('this call').toBe('fail');
+            }, function(err) {
+                expect(err.message).toEqual(
+                    'Current working directory is not a Cordova-based project.'
+                )
+            }).fin(done);
         });
     });
 
     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.raw.build(['android','ios']).then(function() {
-                expect(prepare_spy).toHaveBeenCalledWith({verbose: false, platforms: ['android', 'ios'], options: []});
-                expect(compile_spy).toHaveBeenCalledWith({verbose: false, platforms: ['android', 'ios'], options: []});
+                var opts = {verbose: false, platforms: ['android', 'ios'], options: []};
+                expect(prepare_spy).toHaveBeenCalledWith(opts);
+                expect(compile_spy).toHaveBeenCalledWith(opts);
                 done();
             });
         });
@@ -106,12 +113,15 @@ describe('build command', function() {
         });
 
         describe('with no platforms added', function() {
-            it('should not fire the hooker', function() {
+            it('should not fire the hooker', function(done) {
                 list_platforms.andReturn([]);
-                wrapper(cordova.raw.build(), function() {
-                    expect(result).toEqual(new Error('No platforms added to this project. Please use `cordova platform add <platform>`.'));
-                    expect(fire).not.toHaveBeenCalled();
-                });
+                Q().then(cordova.raw.build).then(function() {
+                    expect('this call').toBe('fail');
+                }, function(err) {
+                    expect(err.message).toEqual(
+                        'No platforms added to this project. Please use `cordova platform add <platform>`.'
+                    )
+                }).fin(done);
             });
         });
     });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/spec/compile.spec.js
----------------------------------------------------------------------
diff --git a/spec/compile.spec.js b/spec/compile.spec.js
index 91228a9..79761bf 100644
--- a/spec/compile.spec.js
+++ b/spec/compile.spec.js
@@ -66,7 +66,7 @@ describe('compile command', function() {
 
     function wrapper(f, post) {
         runs(function() {
-            f.then(function() { result = true; }, function(err) { result = err; });
+            Q().then(f).then(function() { result = true; }, function(err) { result = err; });
         });
         waitsFor(function() { return result; }, 'promise never resolved', 500);
         runs(post);
@@ -81,13 +81,13 @@ describe('compile command', function() {
     describe('failure', function() {
         it('should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function() {
             list_platforms.andReturn([]);
-            wrapper(cordova.raw.compile(), function() {
+            wrapper(cordova.raw.compile, function() {
                 expect(result).toEqual(new Error('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);
-            wrapper(cordova.raw.compile(), function() {
+            wrapper(cordova.raw.compile, function() {
                 expect(result).toEqual(new Error('Current working directory is not a Cordova-based project.'));
             });
         });
@@ -134,12 +134,16 @@ describe('compile command', function() {
         });
 
         describe('with no platforms added', function() {
-            it('should not fire the hooker', function() {
+            it('should not fire the hooker', function(done) {
                 list_platforms.andReturn([]);
-                wrapper(cordova.raw.compile(), function() {
-                    expect(result).toEqual(new Error('No platforms added to this project. Please use `cordova platform add <platform>`.'));
+                Q().then(cordova.raw.compile).then(function() {
+                    expect('this call').toBe('fail');
+                }, function(err) {
                     expect(fire).not.toHaveBeenCalled();
-                });
+                    expect(err.message).toEqual(
+                        'No platforms added to this project. Please use `cordova platform add <platform>`.'
+                    )
+                }).fin(done);
             });
         });
     });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/spec/emulate.spec.js
----------------------------------------------------------------------
diff --git a/spec/emulate.spec.js b/spec/emulate.spec.js
index 4809cc0..7256936 100644
--- a/spec/emulate.spec.js
+++ b/spec/emulate.spec.js
@@ -65,7 +65,7 @@ describe('emulate command', function() {
 
     function wrapper(f, post) {
         runs(function() {
-            f.then(function() { result = true; }, function(err) { result = err; });
+            Q().then(f).then(function() { result = true; }, function(err) { result = err; });
         });
         waitsFor(function() { return result; }, 'promise never resolved', 500);
         runs(post);
@@ -82,13 +82,13 @@ describe('emulate command', function() {
     describe('failure', function() {
         it('should not run inside a Cordova-based project with no added platforms by calling util.listPlatforms', function() {
             list_platforms.andReturn([]);
-            wrapper(cordova.raw.emulate(), function() {
+            wrapper(cordova.raw.emulate, function() {
                 expect(result).toEqual(new Error('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);
-            wrapper(cordova.raw.emulate(), function() {
+            wrapper(cordova.raw.emulate, function() {
                 expect(result).toEqual(new Error('Current working directory is not a Cordova-based project.'));
             });
         });
@@ -138,12 +138,16 @@ describe('emulate command', function() {
         });
 
         describe('with no platforms added', function() {
-            it('should not fire the hooker', function() {
+            it('should not fire the hooker', function(done) {
                 list_platforms.andReturn([]);
-                wrapper(cordova.raw.emulate(), function() {
-                    expect(result).toEqual(new Error('No platforms added to this project. Please use `cordova platform add <platform>`.'));
-                });
-                expect(fire).not.toHaveBeenCalled();
+                Q().then(cordova.raw.emulate).then(function() {
+                    expect('this call').toBe('fail');
+                }, function(err) {
+                    expect(fire).not.toHaveBeenCalled();
+                    expect(err.message).toEqual(
+                        'No platforms added to this project. Please use `cordova platform add <platform>`.'
+                    )
+                }).fin(done);
             });
         });
     });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/spec/prepare.spec.js
----------------------------------------------------------------------
diff --git a/spec/prepare.spec.js b/spec/prepare.spec.js
index 0388ec8..ebdeffc 100644
--- a/spec/prepare.spec.js
+++ b/spec/prepare.spec.js
@@ -77,7 +77,7 @@ describe('prepare command', function() {
     describe('failure', function() {
         it('should not run outside of a cordova-based project by calling util.isCordova', function(done) {
             is_cordova.andReturn(false);
-            cordova.raw.prepare().then(function() {
+            Q().then(cordova.raw.prepare).then(function() {
                 expect('this call').toBe('fail');
             }, function(err) {
                 expect(err).toEqual(new Error('Current working directory is not a Cordova-based project.'));
@@ -85,7 +85,7 @@ describe('prepare command', function() {
         });
         it('should not run inside a cordova-based project with no platforms', function(done) {
             list_platforms.andReturn([]);
-            cordova.raw.prepare().then(function() {
+            Q().then(cordova.raw.prepare).then(function() {
                 expect('this call').toBe('fail');
             }, function(err) {
                 expect(err).toEqual(new Error('No platforms added to this project. Please use `cordova platform add <platform>`.'));
@@ -207,7 +207,7 @@ describe('prepare command', function() {
                 list_platforms.andReturn([]);
             });
             it('should not fire the hooker', function(done) {
-                cordova.raw.prepare().then(function() {
+                Q().then(cordova.raw.prepare).then(function() {
                     expect('this call').toBe('fail');
                 }, function(err) {
                     expect(err).toEqual(jasmine.any(Error));

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/spec/run.spec.js
----------------------------------------------------------------------
diff --git a/spec/run.spec.js b/spec/run.spec.js
index 159ea00..41122fa 100644
--- a/spec/run.spec.js
+++ b/spec/run.spec.js
@@ -140,11 +140,11 @@ describe('run command', function() {
         describe('with no platforms added', function() {
             it('should not fire the hooker', function(done) {
                 list_platforms.andReturn([]);
-                cordova.raw.run().then(function() {
+                Q().then(cordova.raw.run).then(function() {
                     expect('this call').toBe('fail');
                 }, function(err) {
                     expect(fire).not.toHaveBeenCalled();
-                    expect(err).toEqual(new Error('No platforms added to this project. Please use `cordova platform add <platform>`.'));
+                    expect(err.message).toEqual('No platforms added to this project. Please use `cordova platform add <platform>`.');
                 }).fin(done);
             });
         });

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/build.js
----------------------------------------------------------------------
diff --git a/src/build.js b/src/build.js
index 54eb7ec..5a0f0d3 100644
--- a/src/build.js
+++ b/src/build.js
@@ -33,9 +33,6 @@ module.exports = function build(options) {
     }
 
     options = cordova_util.preProcessOptions(options);
-    if (options.constructor.name === "Error") {
-        return Q.reject(options);
-    }
 
     // fire build hooks
     var hooks = new hooker(projectRoot);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/compile.js
----------------------------------------------------------------------
diff --git a/src/compile.js b/src/compile.js
index 78c72ed..0daf56a 100644
--- a/src/compile.js
+++ b/src/compile.js
@@ -83,9 +83,6 @@ module.exports = function compile(options) {
     }
 
     options = cordova_util.preProcessOptions(options);
-    if (options.constructor.name === "Error") {
-        return Q.reject(options);
-    }
 
     hooks = new hooker(projectRoot);
     return hooks.fire('before_compile', options)

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/create.js
----------------------------------------------------------------------
diff --git a/src/create.js b/src/create.js
index 3de4e77..2ee3c45 100644
--- a/src/create.js
+++ b/src/create.js
@@ -26,6 +26,7 @@ var path          = require('path'),
     config        = require('./config'),
     lazy_load     = require('./lazy_load'),
     Q             = require('q'),
+    CordovaError  = require('./CordovaError'),
     util          = require('./util');
 
 var DEFAULT_NAME = "HelloCordova",
@@ -75,7 +76,7 @@ module.exports = function create (dir, id, name, cfg) {
     }
 
     if (fs.existsSync(dir) && !sanedircontents(dir)) {
-        return Q.reject(new Error('Path already exists and is not empty: ' + dir));
+        return Q.reject(new CordovaError('Path already exists and is not empty: ' + dir));
     }
 
     // Create basic project structure.

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/emulate.js
----------------------------------------------------------------------
diff --git a/src/emulate.js b/src/emulate.js
index be248c5..fee4c28 100644
--- a/src/emulate.js
+++ b/src/emulate.js
@@ -85,9 +85,6 @@ module.exports = function emulate(options) {
     }
 
     options = cordova_util.preProcessOptions(options);
-    if (options.constructor.name === "Error") {
-        return Q.reject(options);
-    }
 
     hooks = new hooker(projectRoot);
     return hooks.fire('before_emulate', options)

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/platform.js
----------------------------------------------------------------------
diff --git a/src/platform.js b/src/platform.js
index 1b7f179..e9443c7 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -25,6 +25,7 @@ var config            = require('./config'),
     hooker            = require('./hooker'),
     events            = require('./events'),
     lazy_load         = require('./lazy_load'),
+    CordovaError      = require('./CordovaError'),
     Q                 = require('q'),
     platforms         = require('../platforms'),
     child_process     = require('child_process'),
@@ -43,13 +44,13 @@ module.exports = function platform(command, targets) {
         var err;
         targets.forEach(function(t) {
             if (!(t in platforms)) {
-                err = new Error('Platform "' + t + '" not recognized as a core cordova platform. See "platform list".');
+                err = new CordovaError('Platform "' + t + '" not recognized as a core cordova platform. See "platform list".');
             }
         });
         if (err) return Q.reject(err);
     } else {
         if (command == 'add' || command == 'rm') {
-            return Q.reject(new Error('You need to qualify `add` or `remove` with one or more platforms!'));
+            return Q.reject(new CordovaError('You need to qualify `add` or `remove` with one or more platforms!'));
         }
     }
 
@@ -62,7 +63,7 @@ module.exports = function platform(command, targets) {
     switch(command) {
         case 'add':
             if (!targets || !targets.length) {
-                return Q.reject(new Error('No platform specified. Please specify a platform to add. See "platform list".'));
+                return Q.reject(new CordovaError('No platform specified. Please specify a platform to add. See "platform list".'));
             }
             var config_json = config.read(projectRoot);
 
@@ -88,7 +89,7 @@ module.exports = function platform(command, targets) {
         case 'rm':
         case 'remove':
             if (!targets || !targets.length) {
-                return Q.reject(new Error('No platform[s] specified. Please specify platform[s] to remove. See "platform list".'));
+                return Q.reject(new CordovaError('No platform[s] specified. Please specify platform[s] to remove. See "platform list".'));
             }
             return hooks.fire('before_platform_rm', opts)
             .then(function() {
@@ -106,15 +107,15 @@ module.exports = function platform(command, targets) {
         case 'up':
             // Shell out to the update script provided by the named platform.
             if (!targets || !targets.length) {
-                return Q.reject(new Error('No platform specified. Please specify a platform to update. See "platform list".'));
+                return Q.reject(new CordovaError('No platform specified. Please specify a platform to update. See "platform list".'));
             } else if (targets.length > 1) {
-                return Q.reject(new Error('Platform update can only be executed on one platform at a time.'));
+                return Q.reject(new CordovaError('Platform update can only be executed on one platform at a time.'));
             } else {
                 var plat = targets[0];
                 var platformPath = path.join(projectRoot, 'platforms', plat);
                 var installed_platforms = cordova_util.listPlatforms(projectRoot);
                 if (installed_platforms.indexOf(plat) < 0) {
-                    return Q.reject(new Error('Platform "' + plat + '" is not installed. See "platform list".'));
+                    return Q.reject(new CordovaError('Platform "' + plat + '" is not installed. See "platform list".'));
                 }
 
                 function copyCordovaJs() {
@@ -204,12 +205,12 @@ module.exports = function platform(command, targets) {
 
 module.exports.supports = function(project_root, name) {
     // required parameters
-    if (!name) return Q.reject(new Error('requires a platform name parameter'));
+    if (!name) return Q.reject(new CordovaError('requires a platform name parameter'));
 
     // check if platform exists
     var platform = platforms[name];
     if (!platform) {
-        return Q.reject(new Error(util.format('"%s" platform does not exist', name)));
+        return Q.reject(new CordovaError(util.format('"%s" platform does not exist', name)));
     }
 
     // look up platform meta-data parser
@@ -236,7 +237,7 @@ function call_into_create(target, projectRoot, cfg, libDir, template_dir) {
 
     // Check if output directory already exists.
     if (fs.existsSync(output)) {
-        return Q.reject(new Error('Platform ' + target + ' already added'));
+        return Q.reject(new CordovaError('Platform ' + target + ' already added'));
     } else {
         // Make sure we have minimum requirements to work with specified platform
         events.emit('verbose', 'Checking if platform "' + target + '" passes minimum requirements...');

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/plugin.js
----------------------------------------------------------------------
diff --git a/src/plugin.js b/src/plugin.js
index 5ef98ce..76cc0af 100644
--- a/src/plugin.js
+++ b/src/plugin.js
@@ -24,6 +24,7 @@ module.exports = function plugin(command, targets, opts) {
         hooker        = require('./hooker'),
         config        = require('./config'),
         Q             = require('q'),
+        CordovaError  = require('./CordovaError'),
         events        = require('./events');
 
     var projectRoot = cordova_util.cdProjectRoot(),
@@ -59,7 +60,7 @@ module.exports = function plugin(command, targets, opts) {
     plugins = cordova_util.findPlugins(pluginPath);
     if (!targets || !targets.length) {
         if (command == 'add' || command == 'rm') {
-            return Q.reject(new Error('You need to qualify `add` or `remove` with one or more plugins!'));
+            return Q.reject(new CordovaError('You need to qualify `add` or `remove` with one or more plugins!'));
         } else {
             targets = [];
         }
@@ -80,7 +81,7 @@ module.exports = function plugin(command, targets, opts) {
     switch(command) {
         case 'add':
             if (!targets || !targets.length) {
-                return Q.reject(new Error('No plugin specified. Please specify a plugin to add. See "plugin search".'));
+                return Q.reject(new CordovaError('No plugin specified. Please specify a plugin to add. See "plugin search".'));
             }
 
             var config_json = config(projectRoot, {});
@@ -142,14 +143,14 @@ module.exports = function plugin(command, targets, opts) {
         case 'rm':
         case 'remove':
             if (!targets || !targets.length) {
-                return Q.reject(new Error('No plugin specified. Please specify a plugin to remove. See "plugin list".'));
+                return Q.reject(new CordovaError('No plugin specified. Please specify a plugin to remove. See "plugin list".'));
             }
             return hooks.fire('before_plugin_rm', opts)
             .then(function() {
                 return opts.plugins.reduce(function(soFar, target) {
                     // Check if we have the plugin.
                     if (plugins.indexOf(target) < 0) {
-                        return Q.reject(new Error('Plugin "' + target + '" is not present in the project. See "plugin list".'));
+                        return Q.reject(new CordovaError('Plugin "' + target + '" is not present in the project. See "plugin list".'));
                     }
 
                     var targetPath = path.join(pluginPath, target);

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/prepare.js
----------------------------------------------------------------------
diff --git a/src/prepare.js b/src/prepare.js
index 2dd7d35..64815b3 100644
--- a/src/prepare.js
+++ b/src/prepare.js
@@ -43,9 +43,6 @@ module.exports = function prepare(options) {
     }
 
     options = cordova_util.preProcessOptions(options);
-    if (options.constructor.name === "Error") {
-        return Q.reject(options);
-    }
 
     var xml = cordova_util.projectConfig(projectRoot);
     var paths = options.platforms.map(function(p) {

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/run.js
----------------------------------------------------------------------
diff --git a/src/run.js b/src/run.js
index 198bd32..ed799cc 100644
--- a/src/run.js
+++ b/src/run.js
@@ -85,9 +85,6 @@ module.exports = function run(options) {
     }
 
     options = cordova_util.preProcessOptions(options);
-    if (options.constructor.name === "Error") {
-        return Q.reject(options);
-    }
 
     hooks = new hooker(projectRoot);
     return hooks.fire('before_run', options)

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/2676ff88/src/util.js
----------------------------------------------------------------------
diff --git a/src/util.js b/src/util.js
index be843be..75b718d 100644
--- a/src/util.js
+++ b/src/util.js
@@ -143,11 +143,11 @@ exports = module.exports = {
             projectRoot = this.isCordova();
 
         if (!projectRoot) {
-            return new Error('Current working directory is not a Cordova-based project.');
+            throw new CordovaError('Current working directory is not a Cordova-based project.');
         }
         var projectPlatforms = this.listPlatforms(projectRoot);
         if (projectPlatforms.length === 0) {
-            return new Error('No platforms added to this project. Please use `cordova platform add <platform>`.');
+            throw new CordovaError('No platforms added to this project. Please use `cordova platform add <platform>`.');
         }
         /**
          * Current Desired Arguments