Implement renaming to index in the FsResolver, fix minor issues.

This commit is contained in:
André Cruz
2013-04-30 19:22:47 +01:00
parent 974373dc40
commit 3a93600195
13 changed files with 186 additions and 53 deletions

View File

@@ -23,7 +23,7 @@ mout.object.mixIn(FsResolver, Resolver);
FsResolver.prototype.hasNew = function (canonicalPkg) {
// If target was specified, simply reject the promise
if (this._target !== '*') {
return Q.reject(createError('File system sources can\'t resolve targets ("' + this._target + '")', 'ENORESTARGET'));
return Q.reject(createError('File system sources can\'t resolve targets', 'ENORESTARGET'));
}
// TODO: should we store latest mtimes in the resolution and compare?
@@ -34,12 +34,13 @@ FsResolver.prototype.hasNew = function (canonicalPkg) {
FsResolver.prototype._resolveSelf = function () {
// If target was specified, simply reject the promise
if (this._target !== '*') {
return Q.reject(createError('File system sources can\'t resolve targets ("' + this._target + '")', 'ENORESTARGET'));
return Q.reject(createError('File system sources can\'t resolve targets', 'ENORESTARGET'));
}
return this._readJson(this._source)
.then(this._copy.bind(this))
.spread(this._extract.bind(this));
.then(this._extract.bind(this))
.then(this._rename.bind(this));
};
// -----------------
@@ -51,6 +52,8 @@ FsResolver.prototype._copy = function (meta) {
copyOpts,
promise;
this._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
copyOpts = {
@@ -68,17 +71,51 @@ FsResolver.prototype._copy = function (meta) {
}
return promise.then(function () {
return [stat, dstFile || this._tempDir ];
return dstFile;
}.bind(this));
}.bind(this));
};
FsResolver.prototype._extract = function (stat, file) {
if (stat.isFile() && extract.canExtract(file)) {
return extract(file, this._tempDir);
FsResolver.prototype._extract = function (file) {
if (!file || !extract.canExtract(file)) {
return Q.resolve();
}
return Q.resolve();
return extract(file, this._tempDir);
};
FsResolver.prototype._rename = function () {
// Only rename if original source was a file
if (!this._sourceStat.isFile()) {
return Q.resolve();
}
return Q.nfcall(fs.readdir, this._tempDir)
.then(function (files) {
var file,
oldPath,
newPath;
if (files.length === 1) {
file = files[0];
this._singleFile = 'index' + path.extname(file);
oldPath = path.join(this._tempDir, file);
newPath = path.join(this._tempDir, this._singleFile);
return Q.nfcall(fs.rename, oldPath, newPath);
}
}.bind(this));
};
FsResolver.prototype._savePkgMeta = function (meta) {
// TODO: store mtime into the package meta
// Store main if is a single file
if (this._singleFile) {
meta.main = this._singleFile;
}
return Resolver.prototype._savePkgMeta.call(this, meta);
};
module.exports = FsResolver;

View File

@@ -19,9 +19,7 @@ mout.object.mixIn(GitResolver, Resolver);
GitResolver.prototype._resolveSelf = function () {
return this._findResolution()
.then(function (resolution) {
this._resolution = resolution;
.then(function () {
return this._checkout()
// Always run cleanup after checkout to ensure that .git is removed!
// If it's not removed, problems might arrise when the "tmp" module attemps
@@ -102,7 +100,7 @@ GitResolver.prototype._findResolution = function (target) {
});
}
return { type: 'version', tag: version.tag, commit: version.commit };
return this._resolution = { type: 'version', tag: version.tag, commit: version.commit };
}.bind(this));
}
@@ -111,7 +109,7 @@ GitResolver.prototype._findResolution = function (target) {
return self.fetchTags(this._source)
.then(function (tags) {
if (mout.object.hasOwn(tags, target)) {
return { type: 'tag', tag: target, commit: tags[target] };
return this._resolution = { type: 'tag', tag: target, commit: tags[target] };
}
// Finally check if is a valid branch
@@ -134,7 +132,7 @@ GitResolver.prototype._findResolution = function (target) {
throw err;
}
return { type: 'branch', branch: target, commit: branches[target] };
return this._resolution = { type: 'branch', branch: target, commit: branches[target] };
}.bind(this));
}.bind(this));
};

View File

@@ -22,8 +22,6 @@ function cmd(command, args, options) {
var fullCommand,
error;
process.removeAllListeners();
if (code) {
// Generate the full command to be presented in the error message
if (!Array.isArray(args)) {

View File

@@ -9,10 +9,12 @@ function copy(reader, writer) {
finish;
finish = function (err) {
// Ensure that all listeners are removed
writer.removeAllListeners();
reader.removeAllListeners();
// If we got an error, simply reject the deferred
// Otherwise resolve it
if (err) {
deferred.reject(err);
} else {
@@ -29,7 +31,8 @@ function copy(reader, writer) {
reader = fstream.Reader(reader);
}
reader.on('error', finish);
reader
.on('error', finish);
// Writer
writer = fstream.Writer(writer)

View File

@@ -6,6 +6,10 @@ var tar = require('tar');
var Q = require('q');
var mout = require('mout');
// This forces the default chunk size to something small in an attempt
// to avoid issue #314
zlib.Z_DEFAULT_CHUNK = 1024 * 8;
var extractors = {
'.zip': extractZip,
'.tar': extractTar,