You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by dp...@apache.org on 2019/04/17 00:06:42 UTC

[cordova-lib] branch master updated: Simpler and better cordova/util.getPlatformApiFunction (#767)

This is an automated email from the ASF dual-hosted git repository.

dpogue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-lib.git


The following commit(s) were added to refs/heads/master by this push:
     new 3be2acb  Simpler and better cordova/util.getPlatformApiFunction (#767)
3be2acb is described below

commit 3be2acb3af2b409ebda81d9e4215cef882cb0605
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Wed Apr 17 02:06:37 2019 +0200

    Simpler and better cordova/util.getPlatformApiFunction (#767)
    
    * Simpler and better cordova/util.getPlatformApiFunction
    
    * Apply suggestions from code review
    
    Co-Authored-By: raphinesse <ra...@gmail.com>
---
 spec/cordova/platforms/platforms.spec.js |  2 +-
 spec/cordova/util.spec.js                |  2 +-
 src/cordova/util.js                      | 44 +++++++++-----------------------
 3 files changed, 14 insertions(+), 34 deletions(-)

diff --git a/spec/cordova/platforms/platforms.spec.js b/spec/cordova/platforms/platforms.spec.js
index 2d8fc5f..15146b4 100644
--- a/spec/cordova/platforms/platforms.spec.js
+++ b/spec/cordova/platforms/platforms.spec.js
@@ -62,7 +62,7 @@ describe('platforms/platforms', () => {
             expect(platformApi).toBeDefined();
             expect(platformApi.platform).toEqual('windows');
             expect(events.emit.calls.count()).toEqual(1);
-            expect(events.emit.calls.argsFor(0)[1]).toEqual('PlatformApi successfully found for platform windows');
+            expect(events.emit.calls.argsFor(0)[1]).toMatch('Platform API successfully found in:');
             expect(util.convertToRealPathSafe.calls.count()).toEqual(1);
             expect(util.isCordova.calls.count()).toEqual(0);
             expect(util.requireNoCache.calls.count()).toEqual(1);
diff --git a/spec/cordova/util.spec.js b/spec/cordova/util.spec.js
index 3289e6e..94ce215 100644
--- a/spec/cordova/util.spec.js
+++ b/spec/cordova/util.spec.js
@@ -220,7 +220,7 @@ describe('util module', function () {
                 var specPlugDir = __dirname.replace('spec-cordova', 'spec-plugman');
                 util.getPlatformApiFunction((path.join(specPlugDir, 'fixtures', 'projects', 'platformApi', 'platforms', 'windows', 'cordova', 'Api.js')), 'windows');
                 expect(events.emit.calls.count()).toBe(1);
-                expect(events.emit.calls.argsFor(0)[1]).toBe('PlatformApi successfully found for platform windows');
+                expect(events.emit.calls.argsFor(0)[1]).toMatch('Platform API successfully found in:');
             });
         });
     });
diff --git a/src/cordova/util.js b/src/cordova/util.js
index 5c1e894..abc1793 100644
--- a/src/cordova/util.js
+++ b/src/cordova/util.js
@@ -351,43 +351,23 @@ function isSymbolicLink (dir) {
     }
 }
 
-// Takes a libDir (root of platform where pkgJson is expected) & a platform name.
-// Platform is used if things go wrong, so we can use polyfill.
-// Potential errors : path doesn't exist, module isn't found or can't load.
-// Message prints if file not named Api.js or falls back to pollyfill.
-function getPlatformApiFunction (libDir, platform) {
-    var PlatformApi;
+// Returns the API of the platform contained in `dir`.
+// Potential errors : module isn't found, can't load or doesn't implement the expected interface.
+function getPlatformApiFunction (dir) {
+    let PlatformApi;
     try {
-        // First we need to find whether platform exposes its' API via js module
-        // If it does, then we require and instantiate it.
-        // This will throw if package.json does not exist, or specify 'main'.
-        var apiEntryPoint = require.resolve(libDir);
-        if (apiEntryPoint) {
-            if (path.basename(apiEntryPoint) !== 'Api.js') {
-                events.emit('verbose', 'File name should be called Api.js.');
-                // Not an error, still load it ...
-            }
-            PlatformApi = exports.requireNoCache(apiEntryPoint);
-            if (!PlatformApi.createPlatform) {
-                PlatformApi = null;
-                events.emit('error', 'Does not appear to implement platform Api.');
-            } else {
-                events.emit('verbose', 'PlatformApi successfully found for platform ' + platform);
-            }
-        } else {
-            events.emit('verbose', 'No Api.js entry point found.');
-        }
+        PlatformApi = exports.requireNoCache(dir);
     } catch (err) {
-        // Emit the err, someone might care ...
-        events.emit('warn', 'Unable to load PlatformApi from platform. ' + err);
-        if (!platforms[platform]) {
-            events.emit('error', 'The platform "' + platform + '" does not appear to be a valid cordova platform. It is missing API.js. ' + platform + ' not supported.');
-        }
+        // Module not found or threw error during loading
+        err.message = `Unable to load Platform API from ${dir}:\n${err.message}`;
+        throw err;
     }
 
-    if (!PlatformApi) {
-        throw new Error('Your ' + platform + ' platform does not have Api.js');
+    // Module doesn't implement the expected interface
+    if (!PlatformApi || !PlatformApi.createPlatform) {
+        throw new Error(`The package at "${dir}" does not appear to implement the Cordova Platform API.`);
     }
 
+    events.emit('verbose', 'Platform API successfully found in: ' + dir);
     return PlatformApi;
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org