From 382c857a4b4e140a9562f29c5772c3398fcbb041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Cruz?= Date: Thu, 23 May 2013 23:43:11 +0100 Subject: [PATCH] Tweaks to notifications. --- lib/core/PackageRepository.js | 4 +- lib/core/resolvers/GitFsResolver.js | 2 +- lib/core/resolvers/GitRemoteResolver.js | 2 +- lib/core/resolvers/GitResolver.js | 22 +++-------- lib/core/resolvers/Resolver.js | 6 +-- lib/core/resolvers/UrlResolver.js | 50 +++++++++++++++++++++---- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/lib/core/PackageRepository.js b/lib/core/PackageRepository.js index b6bdf796..a6c18f60 100644 --- a/lib/core/PackageRepository.js +++ b/lib/core/PackageRepository.js @@ -45,7 +45,7 @@ PackageRepository.prototype.fetch = function (decEndpoint) { level: 'action', tag: 'resolve', resolver: resolver, - data: 'Resolving ' + resolver.getSource(), + data: resolver.getSource() + '#' + resolver.getTarget() }); return that._resolve(resolver); } @@ -67,7 +67,7 @@ PackageRepository.prototype.fetch = function (decEndpoint) { // Otherwise, we have to resolve it deferred.notify({ level: 'info', - tag: 'not-cached', + tag: 'uncached', data: 'No cached version for ' + resolver.getSource() + '#' + resolver.getTarget(), }); deferred.notify({ diff --git a/lib/core/resolvers/GitFsResolver.js b/lib/core/resolvers/GitFsResolver.js index a5739677..8dd3a6fd 100644 --- a/lib/core/resolvers/GitFsResolver.js +++ b/lib/core/resolvers/GitFsResolver.js @@ -47,7 +47,7 @@ GitFsResolver.fetchRefs = function (source) { cache = this._refs[source]; if (cache) { // If cached value is a promise, simply return it - // This is to avoid duplicate fetches for the same source + // This avoids duplicate fetches for the same source if (cache.then) { return cache; } diff --git a/lib/core/resolvers/GitRemoteResolver.js b/lib/core/resolvers/GitRemoteResolver.js index 4bf05bb2..873ddc49 100644 --- a/lib/core/resolvers/GitRemoteResolver.js +++ b/lib/core/resolvers/GitRemoteResolver.js @@ -56,7 +56,7 @@ GitRemoteResolver.fetchRefs = function (source) { cache = this._refs[source]; if (cache) { // If cached value is a promise, simply return it - // This is to avoid duplicate fetches for the same source + // This avoids duplicate fetches for the same source if (cache.then) { return cache; } diff --git a/lib/core/resolvers/GitResolver.js b/lib/core/resolvers/GitResolver.js index a2c366df..ef5da6c1 100644 --- a/lib/core/resolvers/GitResolver.js +++ b/lib/core/resolvers/GitResolver.js @@ -38,35 +38,25 @@ GitResolver.prototype._hasNew = function (canonicalPkg, pkgMeta) { }; GitResolver.prototype._resolve = function () { + var that = this; var deferred = Q.defer(); - deferred.notify({ - level: 'action', - tag: 'versions', - data: 'Finding resolution' - }); - this._findResolution() .then(function (resolution) { deferred.notify({ level: 'action', tag: 'checkout', - data: 'Checking out "' + (resolution.tag || resolution.branch || resolution.commit) + '"' + data: resolution.tag || resolution.branch || resolution.commit }); - return this._checkout() + return that._checkout() // Always run cleanup after checkout to ensure that .git is removed! // If it's not removed, problems might arise when the "tmp" module attempts // to delete the temporary folder .fin(function () { - deferred.notify({ - level: 'action', - tag: 'cleanup', - data: 'Cleaning up git artifacts' - }); - return this._cleanup(); - }.bind(this)); - }.bind(this)) + return that._cleanup(); + }); + }) .then(deferred.resolve, deferred.reject, deferred.notify); return deferred.promise; diff --git a/lib/core/resolvers/Resolver.js b/lib/core/resolvers/Resolver.js index 2e814ced..2f70f729 100644 --- a/lib/core/resolvers/Resolver.js +++ b/lib/core/resolvers/Resolver.js @@ -85,9 +85,7 @@ Resolver.prototype.resolve = function () { // Resolve self .then(this._resolve.bind(this)) // Read json, generating the package meta - .then(function () { - return that._readJson(that._tempDir); - }) + .then(this._readJson.bind(this, this._tempDir)) .then(function (meta) { return Q.all([ // Apply package meta @@ -158,7 +156,7 @@ Resolver.prototype._readJson = function (dir) { level: 'warn', tag: 'deprecated', json: filename, - data: 'Package "' + this._name + '" is using the deprecated component.json file' + data: 'Package ' + this._name + ' is using the deprecated component.json file' }); } diff --git a/lib/core/resolvers/UrlResolver.js b/lib/core/resolvers/UrlResolver.js index 840705ab..e8bd964c 100644 --- a/lib/core/resolvers/UrlResolver.js +++ b/lib/core/resolvers/UrlResolver.js @@ -92,9 +92,18 @@ UrlResolver.prototype._resolve = function () { return Q.reject(createError('URL sources can\'t resolve targets', 'ENORESTARGET')); } + // Download return this._download() + // Parse headers .spread(this._parseHeaders.bind(this)) - .spread(this._extract.bind(this)) + // Extract file + .spread(function (file, response) { + return this._extract(file, response) + .progress(function (notification) { + return notification; + }); + }.bind(this)) + // Rename file to index .then(this._rename.bind(this)); }; @@ -102,13 +111,23 @@ UrlResolver.prototype._resolve = function () { UrlResolver.prototype._download = function () { var file = path.join(this._tempDir, path.basename(this._source)); - var deferred = Q.defer(); var reqHeaders = {}; + var that = this; + var deferred = Q.defer(); if (this._config.userAgent) { reqHeaders['User-Agent'] = this._config.userAgent; } + process.nextTick(function () { + deferred.notify({ + level: 'action', + tag: 'download', + data: that._source, + url: that._source + }); + }); + // Download the file request(this._source, { proxy: this._remote.protocol === 'https:' ? this._config.httpsProxy : this._config.proxy, @@ -118,15 +137,15 @@ UrlResolver.prototype._download = function () { agent: false // Do not use keep alive, solves #437 }) .on('response', function (response) { - this._response = response; - }.bind(this)) + that._response = response; + }) .on('error', deferred.reject) // Pipe read stream to write stream .pipe(fs.createWriteStream(file)) .on('error', deferred.reject) .on('close', function () { - deferred.resolve([file, this._response]); - }.bind(this)); + deferred.resolve([file, that._response]); + }); return deferred.promise; }; @@ -174,6 +193,7 @@ UrlResolver.prototype._parseHeaders = function (file, response) { UrlResolver.prototype._extract = function (file, response) { var mimeType = response.headers['content-type']; + var deferred; if (mimeType) { // Clean everything after ; and trim the end result @@ -184,9 +204,23 @@ UrlResolver.prototype._extract = function (file, response) { return Q.resolve(); } - return extract(file, this._tempDir, { - mimeType: mimeType + deferred = Q.defer(); + + process.nextTick(function () { + deferred.notify({ + level: 'action', + tag: 'extract', + data: path.basename(file), + file: file + }); }); + + extract(file, this._tempDir, { + mimeType: mimeType + }) + .then(deferred.resolve, deferred.reject, deferred.notify); + + return deferred.promise; }; UrlResolver.prototype._rename = function () {