cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bra...@apache.org
Subject [1/4] Refactor to use Q.js promises in place of callbacks everywhere.
Date Mon, 23 Sep 2013 18:00:10 GMT
Updated Branches:
  refs/heads/master f8494a2be -> 017b7d9b6


http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/017b7d9b/src/util.js
----------------------------------------------------------------------
diff --git a/src/util.js b/src/util.js
index 1a4e4d5..83c3284 100644
--- a/src/util.js
+++ b/src/util.js
@@ -134,12 +134,36 @@ exports = module.exports = {
     }
 };
 
-function addModuleProperty(module, symbol, modulePath, opt_obj) {
+// opt_wrap is a boolean: True means that a callback-based wrapper for the promise-based
function
+// should be created.
+function addModuleProperty(module, symbol, modulePath, opt_wrap, opt_obj) {
     var val = null;
-    Object.defineProperty(opt_obj || module.exports, symbol, {
-        get : function() { return val = val || module.require(modulePath); },
-        set : function(v) { val = v; }
-    });
+    if (opt_wrap) {
+        module.exports[symbol] = function() {
+            val = val || module.require(modulePath);
+            if (arguments.length && typeof arguments[arguments.length - 1] === 'function')
{
+                // If args exist and the last one is a function, it's the callback.
+                var args = Array.prototype.slice.call(arguments);
+                var cb = args.pop();
+                val.apply(module.exports, args).done(cb, cb);
+            } else {
+                val.apply(module.exports, arguments).done(null, function(err) { throw err;
});
+            }
+        };
+    } else {
+        Object.defineProperty(opt_obj || module.exports, symbol, {
+            get : function() { return val = val || module.require(modulePath); },
+            set : function(v) { val = v; }
+        });
+    }
+
+    // Add the module.raw.foo as well.
+    if(module.exports.raw) {
+        Object.defineProperty(module.exports.raw, symbol, {
+            get : function() { return val = val || module.require(modulePath); },
+            set : function(v) { val = v; }
+        });
+    }
 }
 
 addModuleProperty(module, 'config_parser', './config_parser');

http://git-wip-us.apache.org/repos/asf/cordova-cli/blob/017b7d9b/src/xml-helpers.js
----------------------------------------------------------------------
diff --git a/src/xml-helpers.js b/src/xml-helpers.js
index 1d2a36a..b1fb459 100644
--- a/src/xml-helpers.js
+++ b/src/xml-helpers.js
@@ -26,13 +26,17 @@ var fs = require('fs')
   , et = require('elementtree');
 
 module.exports = {
-    moveProjFile: function(origFile, projPath, callback) {
+    // Returns a promise.
+    moveProjFile: function(origFile, projPath) {
         var src = path.resolve(projPath, origFile)
           , dest = src.replace('.orig', '');
 
+        var d = Q.defer();
         fs.createReadStream(src)
             .pipe(fs.createWriteStream(dest))
-            .on('close', callback);
+            .on('close', d.resolve)
+            .on('error', d.reject);
+        return d.promise;
     },
 
     // compare two et.XML nodes, see if they match


Mime
View raw message