You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2021/09/26 19:04:34 UTC
[cordova-lib] branch master updated: feat(cordova/util): support
loading platform API from node_modules (#860)
This is an automated email from the ASF dual-hosted git repository.
raphinesse 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 5f1fb55 feat(cordova/util): support loading platform API from node_modules (#860)
5f1fb55 is described below
commit 5f1fb55959dd7b95bd88af575419972f7a58bd17
Author: Darryl Pogue <da...@dpogue.ca>
AuthorDate: Sun Sep 26 12:04:27 2021 -0700
feat(cordova/util): support loading platform API from node_modules (#860)
* fix(cordova/util): Support requiring from node_modules
* Add a few comments
* fix: allow caching when loading from node_modules
* test(cordova/util): test loading platform API from node_modules
* refactor: terser way to build canonical platform name
* feat: improve logged errors
* feat: improve success message
Co-authored-by: Raphael von der GrĂ¼n <ra...@gmail.com>
---
spec/cordova/platforms/platforms.spec.js | 2 +-
spec/cordova/util.spec.js | 11 ++++++-----
src/cordova/util.js | 24 ++++++++++++++++++------
3 files changed, 25 insertions(+), 12 deletions(-)
diff --git a/spec/cordova/platforms/platforms.spec.js b/spec/cordova/platforms/platforms.spec.js
index 15146b4..e703585 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]).toMatch('Platform API successfully found in:');
+ expect(events.emit.calls.argsFor(0)[1]).toMatch('Loaded API for windows project');
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 09a2a3a..dfe927e 100644
--- a/spec/cordova/util.spec.js
+++ b/spec/cordova/util.spec.js
@@ -20,7 +20,6 @@
var path = require('path');
var fs = require('fs-extra');
var util = require('../../src/cordova/util');
-var events = require('../../cordova-lib').events;
var helpers = require('../helpers');
var cwd = process.cwd();
@@ -217,12 +216,14 @@ describe('util module', function () {
it('Test 030 : successfully find platform Api', function () {
const FIXTURE_PROJECT = path.join(__dirname, 'fixtures/projects/platformApi/');
const API_PATH = path.join(FIXTURE_PROJECT, 'platforms/windows/cordova/Api.js');
- spyOn(events, 'emit');
- util.getPlatformApiFunction(API_PATH, 'cordova-platform-fixture');
+ const Api = util.getPlatformApiFunction(API_PATH, 'windows');
+ expect(Api.createPlatform().platform).toBe('windows');
+ });
- expect(events.emit.calls.count()).toBe(1);
- expect(events.emit.calls.argsFor(0)[1]).toMatch('Platform API successfully found in:');
+ it('successfully loads platform Api from node_modules', () => {
+ const Api = util.getPlatformApiFunction(null, 'android');
+ expect(Api).toBe(require('cordova-android'));
});
});
});
diff --git a/src/cordova/util.js b/src/cordova/util.js
index dab307a..8eae4dd 100644
--- a/src/cordova/util.js
+++ b/src/cordova/util.js
@@ -301,14 +301,26 @@ function isDirectory (dir) {
// 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) {
+function getPlatformApiFunction (dir, platform) {
let PlatformApi;
try {
+ // First try to load the platform API from the platform project
+ // This is necessary to support older platform API versions
PlatformApi = exports.requireNoCache(dir);
- } catch (err) {
- // Module not found or threw error during loading
- err.message = `Unable to load Platform API from ${dir}:\n${err.message}`;
- throw err;
+ } catch (loadFromDirError) {
+ events.emit('verbose', `Unable to load Platform API from ${dir}:`);
+ events.emit('verbose', CordovaError.fullStack(loadFromDirError));
+
+ const cdvPlatform = platform.replace(/^(?:cordova-)?/, 'cordova-');
+ try {
+ // Load the platform API directly from node_modules
+ PlatformApi = require(cdvPlatform);
+ } catch (loadByNameError) {
+ events.emit('verbose', `Unable to load module ${cdvPlatform} by name:`);
+ events.emit('verbose', CordovaError.fullStack(loadByNameError));
+
+ throw new CordovaError(`Could not load API for ${platform} project ${dir}`);
+ }
}
// Module doesn't implement the expected interface
@@ -316,6 +328,6 @@ function getPlatformApiFunction (dir) {
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);
+ events.emit('verbose', `Loaded API for ${platform} project ${dir}`);
return PlatformApi;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org