cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an-s...@apache.org
Subject cordova-lib git commit: CB-9834 Introduce compat map for hook requires
Date Fri, 30 Oct 2015 09:15:13 GMT
Repository: cordova-lib
Updated Branches:
  refs/heads/master fb0ca5ba9 -> b52c257fc


CB-9834 Introduce compat map for hook requires


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

Branch: refs/heads/master
Commit: b52c257fcde22e982aea7b2287b3d0b943b16df2
Parents: fb0ca5b
Author: Vladimir Kotikov <v-vlkoti@microsoft.com>
Authored: Tue Oct 27 15:20:10 2015 +0300
Committer: Vladimir Kotikov <v-vlkoti@microsoft.com>
Committed: Fri Oct 30 12:15:18 2015 +0300

----------------------------------------------------------------------
 cordova-lib/src/hooks/Context.js | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/b52c257f/cordova-lib/src/hooks/Context.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/hooks/Context.js b/cordova-lib/src/hooks/Context.js
index a0b6cfd..ed7f8a7 100644
--- a/cordova-lib/src/hooks/Context.js
+++ b/cordova-lib/src/hooks/Context.js
@@ -17,6 +17,9 @@
  under the License.
  */
 
+var path = require('path');
+var events = require('cordova-common').events;
+
 /**
  * Creates hook script context
  * @constructor
@@ -39,13 +42,39 @@ function Context(hook, opts) {
     this.cordova = require('../cordova/cordova');
 }
 
+// As per CB-9834 we need to maintain backward compatibility and provide a compat layer
+// for plugins that still require modules, factored to cordova-common.
+var compatMap = {
+    '../configparser/ConfigParser': function () {
+        return require('cordova-common').ConfigParser;
+    },
+    '../util/xml-helpers': function () {
+        return require('cordova-common').xmlHelpers;
+    }
+};
 
 /**
  * Returns a required module
- * @param {String} path Module path
+ * @param {String} modulePath Module path
  * @returns {Object} */
-Context.prototype.requireCordovaModule = function (path) {
-    return require(path);
+Context.prototype.requireCordovaModule = function (modulePath) {
+    // There is a very common mistake, when hook requires some cordova functionality
+    // using 'cordova-lib/...' path.
+    // This path will be resolved only when running cordova from 'normal' installation
+    // (without symlinked modules). If cordova-lib linked to cordova-cli this path is
+    // never resolved, so hook fails with 'Error: Cannot find module 'cordova-lib''
+    var resolvedPath = path.resolve(__dirname, modulePath.replace(/^cordova-lib/, '../../../cordova-lib'));
+    var relativePath = path.relative(__dirname, resolvedPath).replace(/\\/g, '/');
+    events.emit('verbose', 'Resolving module name for ' + modulePath + ' => ' + relativePath);
+
+    var compatRequire = compatMap[relativePath];
+    if (compatRequire) {
+        events.emit('warn', 'The module "' + path.basename(relativePath) + '" has been factored
' +
+            'into "cordova-common". Consider update your plugin hooks.');
+        return compatRequire();
+    }
+
+    return require(relativePath);
 };
 
 module.exports = Context;


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


Mime
View raw message