diff --git a/lib/core/PackageRepository.js b/lib/core/PackageRepository.js index bd3ff374..b0adfdcd 100644 --- a/lib/core/PackageRepository.js +++ b/lib/core/PackageRepository.js @@ -26,6 +26,7 @@ var PackageRepository = function (options) { PackageRepository.prototype.fetch = function (decEndpoint) { var resolver; var deferred = Q.defer(); + var that = this; // Get the appropriate resolver resolverFactory(decEndpoint, this._options) @@ -37,53 +38,54 @@ PackageRepository.prototype.fetch = function (decEndpoint) { decEndpoint.resolverName = res.getName(); // If force flag is used, bypass cache - if (this._options.force) { - return []; + if (that._options.force) { + deferred.notify({ type: 'action', data: 'Resolving' }); + return that._resolve(resolver); } // Note that we use the resolver methods to query the // cache because transformations/normalisations can occur - return this._cache.retrieve(resolver.getSource(), resolver.getTarget()); - }.bind(this)) - // Decide if we can use the one from the resolve cache - .spread(function (canonicalPkg, pkgMeta) { - // If there's no package in the cache - if (!canonicalPkg) { - // And the offline flag is passed, error out - if (this._options.offline) { - throw createError('No cached version for ' + resolver.getTarget(), 'ENOCACHE'); + return that._cache.retrieve(resolver.getSource(), resolver.getTarget()) + // Decide if we can use the one from the resolve cache + .spread(function (canonicalPkg, pkgMeta) { + // If there's no package in the cache + if (!canonicalPkg) { + // And the offline flag is passed, error out + if (that._options.offline) { + throw createError('No cached version for ' + resolver.getTarget(), 'ENOCACHE'); + } + + // Otherwise, we have to resolve it + deferred.notify({ type: 'action', data: 'No cached version, resolving..' }); + return that._resolve(resolver); } - // Otherwise, we have to resolve it - deferred.notify({ type: 'action', data: 'No cached version, resolving..' }); - return this._resolve(resolver); - } - - // If offline flag is used, use directly the cached one - if (this._options.offline) { - deferred.notify({ type: 'action', data: 'Got cached version' }); - return [canonicalPkg, pkgMeta]; - } - - process.nextTick(function () { - deferred.notify({ type: 'action', data: 'Got cached version, validating..' }); - }); - - // Otherwise check for new contents - return resolver.hasNew(canonicalPkg, pkgMeta) - .then(function (hasNew) { - // If there are no new contents, resolve to - // the cached one - if (!hasNew) { + // If offline flag is used, use directly the cached one + if (that._options.offline) { + deferred.notify({ type: 'action', data: 'Got cached version' }); return [canonicalPkg, pkgMeta]; } - // Otherwise resolve to the newest one - deferred.notify({ type: 'action', data: 'There\'s a new version, resolving..' }); + process.nextTick(function () { + deferred.notify({ type: 'action', data: 'Got cached version, validating..' }); + }); - return this._resolve(resolver); + // Otherwise check for new contents + return resolver.hasNew(canonicalPkg, pkgMeta) + .then(function (hasNew) { + // If there are no new contents, resolve to + // the cached one + if (!hasNew) { + return [canonicalPkg, pkgMeta]; + } + + // Otherwise resolve to the newest one + deferred.notify({ type: 'action', data: 'There\'s a new version, resolving..' }); + + return that._resolve(resolver); + }); }); - }.bind(this)) + }) .then(deferred.resolve, deferred.reject, deferred.notify); return deferred.promise; diff --git a/lib/core/Project.js b/lib/core/Project.js index 92ef2588..73bafbaa 100644 --- a/lib/core/Project.js +++ b/lib/core/Project.js @@ -61,6 +61,9 @@ Project.prototype.install = function (endpoints) { // Mark jsons to be resolved if they are not installed // Even if they are installed, its semver must match // against the installed ones + + // TODO: If the user deletes a deep dependency this won't work out + // Find a better solution mout.object.forOwn(jsons, function (decEndpoint) { var local = locals[decEndpoint.name]; diff --git a/lib/core/ResolveCache.js b/lib/core/ResolveCache.js index b3d69250..7453b22a 100644 --- a/lib/core/ResolveCache.js +++ b/lib/core/ResolveCache.js @@ -5,6 +5,7 @@ var semver = require('semver'); var mout = require('mout'); var Q = require('q'); var mkdirp = require('mkdirp'); +var rimraf = require('rimraf'); var ResolveCache = function (dir) { // TODO: Make some options, such as: @@ -79,8 +80,20 @@ ResolveCache.prototype.store = function (canonicalPkg, pkgMeta) { pkgVersion = pkgMeta.version || '_unversioned'; dir = path.join(this._dir, sourceId, pkgVersion); - // Create sourceId directory - return Q.nfcall(mkdirp, path.dirname(dir)) + // Check if directory exists + return Q.nfcall(fs.stat, dir) + .then(function () { + // If it does exists, remove it + return Q.nfcall(rimraf, dir); + }, function (err) { + // If directory does not exists, ensure its basename + // is created + if (err.code === 'ENOENT') { + return Q.nfcall(mkdirp, path.dirname(dir)); + } + + throw err; + }) // Move the canonical to sourceId/target .then(function () { return Q.nfcall(fs.rename, canonicalPkg, dir); @@ -91,16 +104,18 @@ ResolveCache.prototype.store = function (canonicalPkg, pkgMeta) { var versions = this._versions[sourceId]; var inCache; - // Check if this exact version already exists in cache - inCache = versions && versions.some(function (version) { - return pkgVersion === version; - }); + if (versions) { + // Check if this exact version already exists in cache + inCache = versions.some(function (version) { + return pkgVersion === version; + }); - // If it doesn't, add it to the in memory cache - // and sort the versions afterwards - if (!inCache) { - versions.push(pkgVersion); - this._sortVersions(versions); + // If it doesn't, add it to the in memory cache + // and sort the versions afterwards + if (!inCache) { + versions.push(pkgVersion); + this._sortVersions(versions); + } } // Resolve with the final location diff --git a/lib/core/resolvers/UrlResolver.js b/lib/core/resolvers/UrlResolver.js index 52e16f6e..a5b6b412 100644 --- a/lib/core/resolvers/UrlResolver.js +++ b/lib/core/resolvers/UrlResolver.js @@ -215,7 +215,7 @@ UrlResolver.prototype._savePkgMeta = function (meta) { // Store ETAG under _release if (meta._cacheHeaders.ETag) { - meta._release = meta._cacheHeaders.ETag; + meta._release = 'e-tag: ' + mout.string.trim(meta._cacheHeaders.ETag, '"'); } // Store main if is a single file