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:30 UTC

[1/3] git commit: Use "npm info" for fetching plugin metadata

Repository: cordova-lib
Updated Branches:
  refs/heads/master 6f4022bd8 -> 2f3493e21


Use "npm info" for fetching plugin metadata

npm info is an alias for npm view.

By default npm view won't call the registry if there is a recent .cache.json
file for this package.
Forcing npm to ignore the cache by setting cache timeouts to 0
via npm settings cache-min = cache-max = 0.


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

Branch: refs/heads/master
Commit: 719539369f1c44dd00c47a047bd60ea06214c98b
Parents: 6b2ad7f
Author: Mark Koudritsky <ka...@gmail.com>
Authored: Wed May 14 00:16:42 2014 -0400
Committer: Mark Koudritsky <ka...@gmail.com>
Committed: Wed May 14 11:48:48 2014 -0400

----------------------------------------------------------------------
 cordova-lib/src/plugman/registry/registry.js | 44 +++++++++--------------
 1 file changed, 17 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/71953936/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 73c9f82..108a8f6 100644
--- a/cordova-lib/src/plugman/registry/registry.js
+++ b/cordova-lib/src/plugman/registry/registry.js
@@ -15,31 +15,6 @@ var npm = require('npm'),
     plugmanConfigDir = path.resolve(home, '.plugman'),
     plugmanCacheDir = path.resolve(plugmanConfigDir, 'cache');
 
-/**
- * @method getPackageInfo
- * @param {String} args Package names
- * @return {Promise.<Object>} Promised package info.
- */
-function getPackageInfo(args) {
-    var thing = args.length ? args.shift().split("@") : [],
-        name = thing.shift(),
-        version = thing.join("@") || 'latest';
-    var settings = module.exports.settings;
-
-    var d = Q.defer();
-    var req = makeRequest('GET', settings.registry + '/' + name + '/' + version, function(err, res, body){
-        if(err || res.statusCode != 200) {
-          d.reject(new Error('Failed to fetch package information for '+name));
-        } else {
-          d.resolve(JSON.parse(body));
-        }
-    });
-    req.on('error', function(err) {
-        d.reject(err);
-    });
-    return d.promise;
-}
-
 
 module.exports = {
     settings: null,
@@ -159,10 +134,25 @@ module.exports = {
      * @param {String} name Plugin name
      * @return {Promise.<Object>} Promised package info.
      */
-    info: function(args) {
+    info: function(plugin) {
+        plugin = plugin.shift();
         return initSettings()
+        .then(Q.nbind(npm.load, npm))
         .then(function() {
-            return getPackageInfo(args);
+            // --force is not needed
+            npm.config.set('force', false);
+            // Set cache timout limits to 0 to force npm to call the registry
+            // even when it has a recent .cache.json file.
+            npm.config.set('cache-min', 0);
+            npm.config.set('cache-max', 0);
+            return Q.ninvoke(npm.commands, 'view', [plugin], /* silent = */ true );
+        })
+        .then(function(info) {
+            // Plugin info should be accessed as info[version]. If a version
+            // specifier like >=x.y.z was used when calling npm view, info
+            // can contain several versions, but we take the first one here.
+            var version = Object.keys(info)[0];
+            return info[version];
         });
     }
 }


[3/3] git commit: Remove --force from default npm settings for plugin registry

Posted by ka...@apache.org.
Remove --force from default npm settings for plugin registry

Changed to setting --force specifically for (un)publish where it's needed
and removed it from the default settings.

For publish --force is needed when overwriting an already published version.
Otherwise the registry will reply with a 409 HTTP response.

For unpublishing the registry only allows to unpublish specific versions,
if you want to remove the entire thing with all the published version
--force is needed.


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

Branch: refs/heads/master
Commit: 2f3493e210fa19a3a3b3c3d1476962381648fa5c
Parents: 7195393
Author: Mark Koudritsky <ka...@gmail.com>
Authored: Wed May 14 16:36:26 2014 -0400
Committer: Mark Koudritsky <ka...@gmail.com>
Committed: Wed May 14 17:34:40 2014 -0400

----------------------------------------------------------------------
 cordova-lib/spec-plugman/registry/registry.spec.js |  1 +
 cordova-lib/src/plugman/registry/registry.js       | 16 +++++++++++-----
 2 files changed, 12 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/2f3493e2/cordova-lib/spec-plugman/registry/registry.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-plugman/registry/registry.spec.js b/cordova-lib/spec-plugman/registry/registry.spec.js
index 95c4f86..c48e839 100644
--- a/cordova-lib/spec-plugman/registry/registry.spec.js
+++ b/cordova-lib/spec-plugman/registry/registry.spec.js
@@ -90,6 +90,7 @@ describe('registry', function() {
             });
 
             npm.commands = fakeNPMCommands;
+            npm.config.set = function(){};
         });
         it('should run config', function() {
             var params = ['set', 'registry', 'http://registry.cordova.io'];

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/2f3493e2/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 108a8f6..e7c00c9 100644
--- a/cordova-lib/src/plugman/registry/registry.js
+++ b/cordova-lib/src/plugman/registry/registry.js
@@ -70,6 +70,9 @@ module.exports = {
             .then(function() {
                 return Q.ninvoke(npm, 'load', settings);
             }).then(function() {
+                // With  no --force we'll get a 409 (conflict) when trying to
+                // overwrite an existing package@version.
+                npm.config.set('force', true);
                 return Q.ninvoke(npm.commands, 'publish', args)
             }).fin(function() {
                 fs.unlink(path.resolve(args[0], 'package.json'));
@@ -101,8 +104,16 @@ module.exports = {
         .then(function(settings) {
             return Q.ninvoke(npm, 'load', settings);
         }).then(function() {
+            // --force is required to delete an entire plugin with all versions.
+            // Without --force npm can only unpublish a specific version.
+            npm.config.set('force', true);
+            // Note, npm.unpublish does not report back errors (at least some)
+            // e.g.: `unpublish non.existent.plugin`
+            // will complete with no errors.
             return Q.ninvoke(npm.commands, 'unpublish', args);
         }).then(function() {
+            // npm.unpublish removes the cache for the unpublished package
+            // cleaning the entire cache might not be necessary.
             return Q.ninvoke(npm.commands, 'cache', ["clean"]);
         });
     },
@@ -117,8 +128,6 @@ module.exports = {
         return initSettings()
         .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) {
@@ -139,8 +148,6 @@ module.exports = {
         return initSettings()
         .then(Q.nbind(npm.load, npm))
         .then(function() {
-            // --force is not needed
-            npm.config.set('force', false);
             // Set cache timout limits to 0 to force npm to call the registry
             // even when it has a recent .cache.json file.
             npm.config.set('cache-min', 0);
@@ -177,7 +184,6 @@ function initSettings() {
     module.exports.settings =
     rc('plugman', {
          cache: plugmanCacheDir,
-         force: true,
          registry: 'http://registry.cordova.io',
          logstream: fs.createWriteStream(path.resolve(plugmanConfigDir, 'plugman.log')),
          userconfig: path.resolve(plugmanConfigDir, 'config')


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

Posted by ka...@apache.org.
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)