From 9fa91aab36aa68e836a76ba209a4ecbe9d9ce247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Cruz?= Date: Fri, 24 May 2013 00:03:44 +0100 Subject: [PATCH] More tweaks related to notifications, add notifications to remaining resolvers. --- lib/core/resolvers/FsResolver.js | 47 ++++++++++++++++++++----- lib/core/resolvers/GitFsResolver.js | 16 +++++++-- lib/core/resolvers/GitRemoteResolver.js | 19 ++++++++-- lib/core/resolvers/GitResolver.js | 16 ++------- 4 files changed, 73 insertions(+), 25 deletions(-) diff --git a/lib/core/resolvers/FsResolver.js b/lib/core/resolvers/FsResolver.js index ad56277b..8f1e777b 100644 --- a/lib/core/resolvers/FsResolver.js +++ b/lib/core/resolvers/FsResolver.js @@ -42,13 +42,25 @@ FsResolver.prototype._resolve = function () { // ----------------- FsResolver.prototype._copy = function (meta) { - return Q.nfcall(fs.stat, this._source) + var that = this; + var deferred = Q.defer(); + + process.nextTick(function () { + deferred.notify({ + level: 'action', + tag: 'copy', + data: that._source, + source: that._source + }); + }); + + Q.nfcall(fs.stat, this._source) .then(function (stat) { var dstFile; var copyOpts; var promise; - this._sourceStat = stat; + that._sourceStat = stat; // Pass in the ignore to the copy options to avoid copying ignored files // Also, pass in the mode to avoid additional stat calls when copying @@ -59,25 +71,44 @@ FsResolver.prototype._copy = function (meta) { // If it's a folder if (stat.isDirectory()) { - promise = copy.copyDir(this._source, this._tempDir, copyOpts); + promise = copy.copyDir(that._source, that._tempDir, copyOpts); // Else it's a file } else { - dstFile = path.join(this._tempDir, path.basename(this._source)); - promise = copy.copyFile(this._source, dstFile, copyOpts); + dstFile = path.join(that._tempDir, path.basename(that._source)); + promise = copy.copyFile(that._source, dstFile, copyOpts); } return promise.then(function () { return dstFile; - }.bind(this)); - }.bind(this)); + }); + }) + .then(deferred.resolve, deferred.reject, deferred.notify); + + return deferred.promise; }; FsResolver.prototype._extract = function (file) { + var deferred; + if (!file || !extract.canExtract(file)) { return Q.resolve(); } - return extract(file, this._tempDir); + deferred = Q.defer(); + + process.nextTick(function () { + deferred.notify({ + level: 'action', + tag: 'copy', + data: this._source, + source: this._source + }); + }.bind(this)); + + extract(file, this._tempDir) + .then(deferred.resolve, deferred.reject, deferred.notify); + + return deferred.promise; }; FsResolver.prototype._rename = function () { diff --git a/lib/core/resolvers/GitFsResolver.js b/lib/core/resolvers/GitFsResolver.js index 8dd3a6fd..6fe442a9 100644 --- a/lib/core/resolvers/GitFsResolver.js +++ b/lib/core/resolvers/GitFsResolver.js @@ -25,15 +25,27 @@ GitFsResolver.prototype._copy = function () { // Override the checkout function to work with the local copy GitFsResolver.prototype._checkout = function () { var resolution = this._resolution; + var deferred = Q.defer(); // The checkout process could be similar to the GitRemoteResolver by prepending file:// to the source // But from my performance measures, it's faster to copy the folder and just checkout in there + process.nextTick(function () { + deferred.notify({ + level: 'action', + tag: 'checkout', + data: resolution.tag || resolution.branch || resolution.commit + }); + }); + // Copy files to the temporary directory first - return this._copy() + this._copy() .then(cmd.bind(cmd, 'git', ['checkout', '-f', resolution.tag || resolution.branch || resolution.commit], { cwd: this._tempDir })) // Cleanup unstaged files - .then(cmd.bind(cmd, 'git', ['clean', '-f', '-d'], { cwd: this._tempDir })); + .then(cmd.bind(cmd, 'git', ['clean', '-f', '-d'], { cwd: this._tempDir })) + .then(deferred.resolve, deferred.reject, deferred.notify); + + return deferred.promise; }; // ----------------- diff --git a/lib/core/resolvers/GitRemoteResolver.js b/lib/core/resolvers/GitRemoteResolver.js index 873ddc49..94cf68f5 100644 --- a/lib/core/resolvers/GitRemoteResolver.js +++ b/lib/core/resolvers/GitRemoteResolver.js @@ -30,18 +30,33 @@ mout.object.mixIn(GitRemoteResolver, GitResolver); GitRemoteResolver.prototype._checkout = function () { var branch; + var promise; var resolution = this._resolution; + var deferred = Q.defer(); + + process.nextTick(function () { + deferred.notify({ + level: 'action', + tag: 'checkout', + data: resolution.tag || resolution.branch || resolution.commit + }); + }); // If resolution is a commit, we need to clone the entire repo and check it out // Because a commit is not a named ref, there's no better solution if (resolution.type === 'commit') { - return cmd('git', ['clone', this._source, this._tempDir]) + promise = cmd('git', ['clone', this._source, this._tempDir]) .then(cmd.bind(cmd, 'git', ['checkout', resolution.commit], { cwd: this._tempDir })); // Otherwise we are checking out a named ref so we can optimize it } else { branch = resolution.tag || resolution.branch; - return cmd('git', ['clone', this._source, '-b', branch, '--depth', 1, '.'], { cwd: this._tempDir }); + promise = cmd('git', ['clone', this._source, '-b', branch, '--depth', 1, '.'], { cwd: this._tempDir }); } + + promise + .then(deferred.resolve, deferred.reject, deferred.notify); + + return deferred.promise; }; // ------------------------------ diff --git a/lib/core/resolvers/GitResolver.js b/lib/core/resolvers/GitResolver.js index ef5da6c1..02aa1fc6 100644 --- a/lib/core/resolvers/GitResolver.js +++ b/lib/core/resolvers/GitResolver.js @@ -39,16 +39,9 @@ GitResolver.prototype._hasNew = function (canonicalPkg, pkgMeta) { GitResolver.prototype._resolve = function () { var that = this; - var deferred = Q.defer(); - - this._findResolution() - .then(function (resolution) { - deferred.notify({ - level: 'action', - tag: 'checkout', - data: resolution.tag || resolution.branch || resolution.commit - }); + return this._findResolution() + .then(function () { 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 @@ -56,10 +49,7 @@ GitResolver.prototype._resolve = function () { .fin(function () { return that._cleanup(); }); - }) - .then(deferred.resolve, deferred.reject, deferred.notify); - - return deferred.promise; + }); }; // -----------------