More tweaks related to notifications, add notifications to remaining resolvers.

This commit is contained in:
André Cruz
2013-05-24 00:03:44 +01:00
parent 2897b66a1c
commit 9fa91aab36
4 changed files with 73 additions and 25 deletions

View File

@@ -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 () {

View File

@@ -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;
};
// -----------------

View File

@@ -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;
};
// ------------------------------

View File

@@ -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;
});
};
// -----------------