Repository: cordova-lib Updated Branches: refs/heads/master 153092f2c -> 71c380a53
CB-7100: Use npm based lazy-load by default Older git based download is still available under a --usegit flag e.g: `cordova platform add ios --usegit` `cordova prepare ios` The cached files are stored in different locations depending on where they were downloaded from because we don't want dirs that are not npm packages in an npm managed cache dir. ~/.cordova/lib ~/.cordova/lib/npm_cache If a copy downloaded via git already exists in the cache, lazy_load should not attempt to download another copy from npm. Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/71c380a5 Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/71c380a5 Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/71c380a5 Branch: refs/heads/master Commit: 71c380a53faa9130bd1c47a17b91789a04eb2654 Parents: 153092f Author: Mark Koudritsky <[email protected]> Authored: Tue Jul 8 19:03:39 2014 -0400 Committer: Mark Koudritsky <[email protected]> Committed: Tue Jul 8 19:03:39 2014 -0400 ---------------------------------------------------------------------- cordova-lib/spec-cordova/lazy_load.spec.js | 10 ++----- cordova-lib/src/cordova/lazy_load.js | 37 +++++++++++++++++++++---- 2 files changed, 34 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/71c380a5/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 27b5b91..e6ce1eb 100644 --- a/cordova-lib/spec-cordova/lazy_load.spec.js +++ b/cordova-lib/spec-cordova/lazy_load.spec.js @@ -30,8 +30,10 @@ var lazy_load = require('../src/cordova/lazy_load'), describe('lazy_load module', function() { var custom_path; + var npm_cache_add; beforeEach(function() { custom_path = spyOn(config, 'has_custom_path').andReturn(false); + npm_cache_add = spyOn(lazy_load, 'npm_cache_add').andReturn(Q(path.join('lib','dir'))); fakeLazyLoad = function(id, platform, version) { if (platform == 'wp7' || platform == 'wp8') { return Q(path.join('lib', 'wp', id, version, platform)); @@ -54,14 +56,8 @@ describe('lazy_load module', function() { }); it('should invoke lazy_load.custom with appropriate url, platform, and version as specified in platforms manifest', function(done) { var url = platforms.android.url + ';a=snapshot;h=' + platforms.android.version + ';sf=tgz'; - custom.andCallFake(function (platforms, platform) { - expect(platform).toEqual('android'); - expect(platforms[platform].url).toEqual(url); - expect(platforms[platform].id).toEqual('cordova'); - return fakeLazyLoad(platforms[platform].id, platform, platforms[platform].version); - }); lazy_load.cordova('android').then(function(dir) { - expect(custom).toHaveBeenCalled(); + expect(npm_cache_add).toHaveBeenCalled(); expect(dir).toBeDefined(); done(); }); http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/71c380a5/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 2830cb7..94e34be 100644 --- a/cordova-lib/src/cordova/lazy_load.js +++ b/cordova-lib/src/cordova/lazy_load.js @@ -50,6 +50,7 @@ var path = require('path'), exports.cordova = cordova; exports.cordova_git = cordova_git; exports.cordova_npm = cordova_npm; +exports.npm_cache_add = npm_cache_add; exports.custom = custom; exports.based_on_config = based_on_config; @@ -71,11 +72,11 @@ function based_on_config(project_root, platform, opts) { // Returns a promise for the path to the lazy-loaded directory. function cordova(platform, opts) { - var use_npm = opts && opts.usenpm && platform != 'www'; - if ( use_npm ) { - return module.exports.cordova_npm(platform); - } else { + var use_git = opts && opts.usegit || platform === 'www'; + if ( use_git ) { return module.exports.cordova_git(platform); + } else { + return module.exports.cordova_npm(platform); } } @@ -104,11 +105,35 @@ function cordova_npm(platform) { if ( !(platform in platforms) ) { return Q.reject(new Error('Cordova library "' + platform + '" not recognized.')); } - // In most cases platfrom does not specify a version and we use + // In most cases platform does not specify a version and we use // the hard-coded default version from platforms.js version = version || platforms[platform].version; + + // Check if this version was already downloaded from git, if yes, use that copy. + // TODO: remove this once we fully switch to npm workflow. + var platdir = platforms[platform].altplatform || platform; + var git_dload_dir = path.join(util.libDirectory, platdir, 'cordova', version); + if (fs.existsSync(git_dload_dir)) { + var subdir = platforms[platform].subdirectory; + if (subdir) { + git_dload_dir = path.join(git_dload_dir, subdir); + } + events.emit('verbose', 'Platform files for "' + platform + '" previously downloaded not from npm. Using that copy.'); + return Q(git_dload_dir); + } + var pkg = 'cordova-' + platform + '@' + version; - return Q.nfcall( npm.load, {cache: path.join(util.libDirectory, 'npm_cache') }) + return exports.npm_cache_add(pkg); +} + +// Equivalent to a command like +// npm cache add [email protected] +// Returns a promise that resolves to directory containing the package. +function npm_cache_add(pkg) { + var npm_cache_dir = path.join(util.libDirectory, 'npm_cache'); + // 'cache-min' is the time in seconds npm considers the files fresh and + // does not ask the registry if it got a fresher version. + return Q.nfcall( npm.load, { 'cache-min': 3600*24, cache: npm_cache_dir }) .then(function() { return Q.ninvoke(npm.commands, 'cache', ['add', pkg]); }).then(function(info) {
