cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kam...@apache.org
Subject git commit: Add npm cache based downloading to lazy_load
Date Fri, 30 May 2014 21:36:14 GMT
Repository: cordova-lib
Updated Branches:
  refs/heads/master fcbc1a34c -> 55810360f


Add npm cache based downloading to lazy_load

lazy_load was downloading platforms files from github using a url like this
git-wip-us.apache.org/repos/asf?p=cordova-ios.git;a=snapshot;h=3.5.0;sf=tgz

This change adds the possibility to donwload platform file from npm registry
E.g. cordova-ios is published here https://www.npmjs.org/package/cordova-ios

By default the old way is still used, to use npm add --usenpm flag to cordova
cli or opts.usenpm=true in opts parameter of the cordova.raw.platform()
function.


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

Branch: refs/heads/master
Commit: 55810360fe660276a1ab0d04382da633f9cf1290
Parents: fcbc1a3
Author: Mark Koudritsky <kamrik@gmail.com>
Authored: Fri May 30 17:27:29 2014 -0400
Committer: Mark Koudritsky <kamrik@gmail.com>
Committed: Fri May 30 17:27:29 2014 -0400

----------------------------------------------------------------------
 cordova-lib/spec-cordova/lazy_load.spec.js |  2 +-
 cordova-lib/src/cordova/lazy_load.js       | 32 ++++++++++++++++++++++---
 cordova-lib/src/cordova/platform.js        | 12 +++++-----
 3 files changed, 36 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/55810360/cordova-lib/spec-cordova/lazy_load.spec.js
----------------------------------------------------------------------
diff --git a/cordova-lib/spec-cordova/lazy_load.spec.js b/cordova-lib/spec-cordova/lazy_load.spec.js
index 3cdb20a..7db849d 100644
--- a/cordova-lib/spec-cordova/lazy_load.spec.js
+++ b/cordova-lib/spec-cordova/lazy_load.spec.js
@@ -191,7 +191,7 @@ describe('lazy_load module', function() {
         });
         it('should invoke cordova if no custom lib is specified', function(done) {
             lazy_load.based_on_config('yup', 'ios').then(function() {
-                expect(cordova).toHaveBeenCalledWith('ios');
+                expect(cordova).toHaveBeenCalled();
             }, function(err) {
                 expect(err).toBeUndefined();
             }).fin(done);

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/55810360/cordova-lib/src/cordova/lazy_load.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/lazy_load.js b/cordova-lib/src/cordova/lazy_load.js
index 6bb1567..0124996 100644
--- a/cordova-lib/src/cordova/lazy_load.js
+++ b/cordova-lib/src/cordova/lazy_load.js
@@ -29,11 +29,22 @@ var path          = require('path'),
     tar           = require('tar'),
     URL           = require('url'),
     Q             = require('q'),
+    npm           = require('npm'),
     util          = require('./util');
 
 module.exports = {
     // Returns a promise for the path to the lazy-loaded directory.
-    cordova:function lazy_load(platform) {
+
+    cordova: function(platform, opts) {
+        var use_npm = opts && opts.usenpm && platform != 'www';
+        if ( use_npm ) {
+            return module.exports.cordova_npm(platform);
+        } else {
+            return module.exports.cordova_git(platform);
+        }
+    },
+
+    cordova_git: function lazy_load(platform) {
         if (!(platform in platforms)) {
             return Q.reject(new Error('Cordova library "' + platform + '" not recognized.'));
         }
@@ -41,6 +52,21 @@ module.exports = {
         var url = platforms[platform].url + ';a=snapshot;h=' + platforms[platform].version
+ ';sf=tgz';
         return module.exports.custom(url, 'cordova', platform, platforms[platform].version);
     },
+
+    cordova_npm: function lazy_load_npm(platform) {
+        if (!(platform in platforms)) {
+            return Q.reject(new Error('Cordova library "' + platform + '" not recognized.'));
+        }
+        var pkg = 'cordova-' + platform + '@' + platforms[platform].version;
+        return Q.nfcall( npm.load, {cache: path.join(util.libDirectory, 'npm_cache') })
+        .then(function() {
+            return Q.ninvoke(npm.commands, 'cache', ['add', pkg]);
+        }).then(function(info) {
+            var pkgDir = path.resolve(npm.cache, info.name, info.version, 'package');
+            return pkgDir;
+        });
+    },
+
     // Returns a promise for the path to the lazy-loaded directory.
     custom:function(url, id, platform, version) {
         var download_dir;
@@ -139,13 +165,13 @@ module.exports = {
         });
     },
     // Returns a promise for the path to the lazy-loaded directory.
-    based_on_config:function(project_root, platform) {
+    based_on_config:function(project_root, platform, opts) {
         var custom_path = config.has_custom_path(project_root, platform);
         if (custom_path) {
             var dot_file = config.read(project_root);
             return module.exports.custom(dot_file.lib[platform].uri, dot_file.lib[platform].id,
platform, dot_file.lib[platform].version);
         } else {
-            return module.exports.cordova(platform);
+            return module.exports.cordova(platform, opts);
         }
     }
 };

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/55810360/cordova-lib/src/cordova/platform.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/platform.js b/cordova-lib/src/cordova/platform.js
index b69bb34..cb091ec 100644
--- a/cordova-lib/src/cordova/platform.js
+++ b/cordova-lib/src/cordova/platform.js
@@ -64,7 +64,7 @@ function add(hooks, projectRoot, targets, opts) {
     .then(function() {
         return targets.reduce(function(soFar, t) {
             return soFar.then(function() {
-                return lazy_load.based_on_config(projectRoot, t)
+                return lazy_load.based_on_config(projectRoot, t, opts)
                 .then(function(libDir) {
                     var template = config_json.lib && config_json.lib[t] &&
config_json.lib[t].template || null;
                     var copts = null;
@@ -123,7 +123,7 @@ function update(hooks, projectRoot, targets, opts) {
         // First, lazy_load the latest version.
         return hooks.fire('before_platform_update', opts)
         .then(function() {
-            return lazy_load.based_on_config(projectRoot, plat);
+            return lazy_load.based_on_config(projectRoot, plat, opts);
         }).then(function(libDir) {
             // Call the platform's update script.
             var script = path.join(libDir, 'bin', 'update');
@@ -253,7 +253,7 @@ function list(hooks, projectRoot) {
 }
 
 // Returns a promise.
-module.exports = function platform(command, targets) {
+module.exports = function platform(command, targets, opts) {
     var projectRoot = cordova_util.cdProjectRoot();
 
     var hooks = new hooker(projectRoot);
@@ -274,9 +274,9 @@ module.exports = function platform(command, targets) {
         }
     }
 
-    var opts = {
-        platforms:targets
-    };
+    opts = opts || {};
+    opts.platforms = targets;
+
     switch(command) {
         case 'add':
             return add(hooks, projectRoot, targets, opts);


Mime
View raw message