treediff
Version:
Compare and find/sync differences in file trees
68 lines (62 loc) • 1.98 kB
JavaScript
var Path, async, fs, localMapper;
fs = require('fs');
Path = require('path');
async = require('async');
localMapper = function(instance, options, callback) {
this.product = {};
this.length = 0;
this.directories = {};
this.generate = (function(_this) {
return function(path, dir, callback) {
return fs.stat(path, function(err, stat) {
var entry, relativePath;
if ((err != null) && err.code === 'ENOENT') {
return callback(null, {});
} else if (err != null) {
return callback(err);
}
_this.length++;
entry = {
size: stat.size,
modified: stat.mtime,
isDirectory: stat.isDirectory(),
directory: dir
};
if (path !== options.path) {
relativePath = Path.relative(options.path, path);
_this.product[relativePath] = entry;
} else {
relativePath = Path.basename(options.path);
}
if (entry.isDirectory) {
return fs.readdir(path, function(err, list) {
if ((relativePath != null) && (list != null)) {
_this.directories[relativePath] = list.length + 1;
}
if (err != null) {
return callback(err);
}
return async.each(list, function(childPath, next) {
childPath = Path.join(path, childPath);
return _this.generate(childPath, relativePath, next);
}, function() {
return callback(null, this.product);
});
});
} else {
return callback(null, _this.product);
}
});
};
})(this);
return this.generate(options.path, Path.basename(options.path), (function(_this) {
return function(err) {
return callback(err, {
product: _this.product,
length: _this.length,
directories: _this.directories
});
};
})(this));
};
module.exports = localMapper;