cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwbro...@apache.org
Subject [2/2] git commit: [#3050] Add cordova.platform.support(name, callback).
Date Wed, 17 Apr 2013 22:05:34 GMT
[#3050] Add cordova.platform.support(name, callback).


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

Branch: refs/heads/master
Commit: cbfe309a13bc2444f90bacc05f6bef17a41ae3d6
Parents: ec204ce
Author: Michael Brooks <michael@michaelbrooks.ca>
Authored: Wed Apr 17 15:11:11 2013 -0600
Committer: Michael Brooks <michael@michaelbrooks.ca>
Committed: Wed Apr 17 15:50:53 2013 -0600

----------------------------------------------------------------------
 spec/platform.spec.js |   59 ++++++++++++++++++++++++++++++++++++++++++++
 src/platform.js       |   37 ++++++++++++++++++++++++++-
 2 files changed, 94 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/cbfe309a/spec/platform.spec.js
----------------------------------------------------------------------
diff --git a/spec/platform.spec.js b/spec/platform.spec.js
index b8dedec..7138c7e 100644
--- a/spec/platform.spec.js
+++ b/spec/platform.spec.js
@@ -278,3 +278,62 @@ describe('platform command', function() {
         });
     });
 });
+
+describe('platform.supports(name, callback)', function() {
+    var androidParser = require('../src/metadata/android_parser');
+
+    beforeEach(function() {
+        spyOn(androidParser, 'check_requirements');
+    });
+
+    it('should require a platform name', function() {
+        expect(function() {
+            cordova.platform.supports(undefined, function(e){});
+        }).toThrow();
+    });
+
+    it('should require a callback function', function() {
+        expect(function() {
+            cordova.platform.supports('android', undefined);
+        }).toThrow();
+    });
+
+    describe('when platform is unknown', function() {
+        it('should trigger callback with false', function(done) {
+            cordova.platform.supports('windows-3.1', function(e) {
+                expect(e).toEqual(jasmine.any(Error));
+                done();
+            });
+        });
+    });
+
+    describe('when platform is supported', function() {
+        beforeEach(function() {
+            androidParser.check_requirements.andCallFake(function(callback) {
+                callback(null);
+            });
+        });
+
+        it('should trigger callback without error', function(done) {
+            cordova.platform.supports('android', function(e) {
+                expect(e).toBeNull();
+                done();
+            });
+        });
+    });
+
+    describe('when platform is unsupported', function() {
+        beforeEach(function() {
+            androidParser.check_requirements.andCallFake(function(callback) {
+                callback(new Error('could not find the android sdk'));
+            });
+        });
+
+        it('should trigger callback with error', function(done) {
+            cordova.platform.supports('android', function(e) {
+                expect(e).toEqual(jasmine.any(Error));
+                done();
+            });
+        });
+    });
+});

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/cbfe309a/src/platform.js
----------------------------------------------------------------------
diff --git a/src/platform.js b/src/platform.js
index 966a76f..00f2157 100644
--- a/src/platform.js
+++ b/src/platform.js
@@ -78,9 +78,9 @@ module.exports = function platform(command, targets, callback) {
                 }
 
                 // Make sure we have minimum requirements to work with specified platform
-                require('./metadata/' + target + '_parser').check_requirements(function(err)
{
+                module.exports.supports(target, function(err) {
                     if (err) {
-                        throw new Error('Your system does not meet the requirements to create
' + target + ' projects: ' + err);
+                        throw new Error('Your system does not meet the requirements to create
' + target + ' projects: ' + err.message);
                     } else {
                         // Create a platform app using the ./bin/create scripts that exist
in each repo.
                         // TODO: eventually refactor to allow multiple versions to be created.
@@ -121,3 +121,36 @@ module.exports = function platform(command, targets, callback) {
             throw new Error('Unrecognized command "' + command + '". Use either `add`, `remove`,
or `list`.');
     }
 };
+
+/**
+ * Check Platform Support.
+ *
+ * Options:
+ *
+ *   - {String} `name` of the platform to test.
+ *   - {Function} `callback` is triggered with the answer.
+ *     - {Error} `e` null when a platform is supported otherwise describes error.
+ */
+
+module.exports.supports = function(name, callback) {
+    // required parameters
+    if (!name) throw new Error('requires a platform name parameter');
+    if (!callback) throw new Error('requires a callback parameter');
+
+    // look up platform meta-data parser
+    var platformParser = parsers[name];
+    if (!platformParser) {
+        callback(new Error(util.format('"%s" platform does not exist', name)));
+        return;
+    }
+
+    // check for platform support
+    platformParser.check_requirements(function(e) {
+        // typecast String to Error
+        e = (e instanceof String) ? new Error(e) : e;
+        // typecast false Boolean to null
+        e = (e) ? e : null;
+
+        callback(e);
+    });
+};


Mime
View raw message