You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ka...@apache.org on 2014/05/14 23:36:31 UTC

[2/3] git commit: Use "npm cache add" for downloading plugins

Use "npm cache add" for downloading plugins

Plugins were downloaded using the request library. Changing the registry.js
code to use "npm cache add" as the main fetching mechanism. This results in
considerably less code to maintain.

Npm's default proxy settings are used automatically (unless overridden in
~/.plugman/config).

Platform fetching done by cordova/fetch should eventually use npm as well, but
this will require publishing the platforms as npm package tarballs.


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

Branch: refs/heads/master
Commit: 6b2ad7f2eb24384114e02fbd2852731254150e36
Parents: 6f4022b
Author: Mark Koudritsky <ka...@gmail.com>
Authored: Tue May 13 22:13:26 2014 -0400
Committer: Mark Koudritsky <ka...@gmail.com>
Committed: Wed May 14 11:48:48 2014 -0400

----------------------------------------------------------------------
 cordova-lib/src/plugman/registry/registry.js | 96 +++++++++--------------
 1 file changed, 38 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/6b2ad7f2/cordova-lib/src/plugman/registry/registry.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/plugman/registry/registry.js b/cordova-lib/src/plugman/registry/registry.js
index e0d733d..73c9f82 100644
--- a/cordova-lib/src/plugman/registry/registry.js
+++ b/cordova-lib/src/plugman/registry/registry.js
@@ -40,57 +40,6 @@ function getPackageInfo(args) {
     return d.promise;
 }
 
-/**
- * @method fetchPackage
- * @param {String} info Package info
- * @return {Promise.<string>} Promised path to the package.
- */
-function fetchPackage(info, cl) {
-    var settings = module.exports.settings;
-    var d = Q.defer();
-    var cached = path.resolve(settings.cache, info.name, info.version, 'package');
-    if(fs.existsSync(cached)) {
-        d.resolve(cached);
-    } else {
-        var download_dir = path.resolve(cached, '..');
-        shell.mkdir('-p', download_dir);
-
-        var req = makeRequest('GET', info.dist.tarball, function (err, res, body) {
-            if(err || res.statusCode != 200) {
-                d.reject(new Error('failed to fetch the plugin archive'));
-            } else {
-                // Update the download count for this plugin.
-                // Fingers crossed that the timestamps are unique, and that no plugin is downloaded
-                // twice in a single millisecond.
-                //
-                // This is acceptable, because the failure mode is Couch gracefully rejecting the second one
-                // (for lacking a _rev), and dropped a download count is not important.
-                var now = new Date();
-                var pkgId = info._id.substring(0, info._id.indexOf('@'));
-                var message = {
-                    day: now.getUTCFullYear() + '-' + (now.getUTCMonth()+1) + '-' + now.getUTCDate(),
-                    pkg: pkgId,
-                    client: cl
-                };
-                var remote = settings.registry + '/downloads'
-
-                makeRequest('POST', remote, message, function (err, res, body) {
-                    // ignore errors
-                });
-            }
-        });
-        req.pipe(zlib.createUnzip())
-        .pipe(tar.Extract({path:download_dir}))
-        .on('error', function(err) {
-            shell.rm('-rf', download_dir);
-            d.reject(err);
-        })
-        .on('end', function() {
-            d.resolve(path.resolve(download_dir, 'package'));
-        });
-    }
-    return d.promise;
-}
 
 module.exports = {
     settings: null,
@@ -185,16 +134,23 @@ module.exports = {
 
     /**
      * @method fetch
-     * @param {String} name Plugin name
+     * @param {Array} with one element - the plugin id or "id@version"
      * @return {Promise.<string>} Promised path to fetched package.
      */
-    fetch: function(args, client) {
-        var cl = (client === 'plugman' ? 'plugman' : 'cordova-cli');
+    fetch: function(plugin, client) {
+        plugin = plugin.shift();
         return initSettings()
-        .then(function(settings) {
-            return getPackageInfo(args);
-        }).then(function(info) {
-            return fetchPackage(info, cl);
+        .then(Q.nbind(npm.load, npm))
+        .then(function() {
+            // With no --force, npm won't re-download if appropriate version is already cached.
+            npm.config.set('force', false);
+            return Q.ninvoke(npm.commands, 'cache', ['add', plugin]);
+        })
+        .then(function(info) {
+            var cl = (client === 'plugman' ? 'plugman' : 'cordova-cli');
+            bumpCounter(info, cl);
+            var pluginDir = path.resolve(npm.cache, info.name, info.version, 'package');
+            return pluginDir;
         });
     },
 
@@ -240,6 +196,30 @@ function initSettings() {
 }
 
 
+// Send a message to the registry to update download counts.
+function bumpCounter(info, client) {
+    // Update the download count for this plugin.
+    // Fingers crossed that the timestamps are unique, and that no plugin is downloaded
+    // twice in a single millisecond.
+    //
+    // This is acceptable, because the failure mode is Couch gracefully rejecting the second one
+    // (for lacking a _rev), and dropped a download count is not important.
+    var settings = module.exports.settings;
+    var now = new Date();
+    var message = {
+        day: now.getUTCFullYear() + '-' + (now.getUTCMonth()+1) + '-' + now.getUTCDate(),
+        pkg: info.name,
+        client: client,
+        version: info.version
+    };
+    var remote = settings.registry + '/downloads'
+
+    makeRequest('POST', remote, message, function (err, res, body) {
+        // ignore errors
+    });
+}
+
+
 function makeRequest (method, where, what, cb_) {
   var settings = module.exports.settings
   var remote = url.parse(where)