treediff
Version:
Compare and find/sync differences in file trees
85 lines (79 loc) • 2.48 kB
JavaScript
var Path, async, fs, localTransferer;
Path = require('path');
fs = require('fs-extended');
async = require('async');
localTransferer = function(instance, map) {
this.allowStreams = true;
this.read = function(options, callback) {
var path;
path = Path.resolve(map.path, options.path);
if (options.entry.isDirectory) {
return callback();
} else if (options.allowStreams) {
return callback(null, fs.createReadStream(path));
} else {
return fs.readFile(path, callback);
}
};
this.write = function(options, callback) {
var path;
path = Path.resolve(map.path, options.path);
if (options.entry.isDirectory) {
return fs.mkdir(path, function(err) {
if ((err != null) && err.code === 'EEXIST') {
err = null;
}
return callback(err);
});
} else if (options.inputType === 'stream') {
return fs.ensureFile(path, function(err) {
if (err != null) {
return callback(err);
}
return options.input.pipe(fs.createWriteStream(path)).on('close', callback);
});
} else {
return fs.createFile(path, options.input, callback);
}
};
this["delete"] = function(options, callback) {
var cleanEmptyDirRecursive, deleteEntry;
deleteEntry = function(done) {
var path;
path = Path.resolve(map.path, options.path);
if (options.entry.isDirectory) {
map.directories[options.path]--;
return done(null, options.path);
} else if (options.entry.directory != null) {
map.directories[options.entry.directory]--;
return fs.unlink(path, function(err) {
return done(err, options.entry.directory);
});
}
};
cleanEmptyDirRecursive = function(path, next) {
var entry;
entry = map.product[path];
if ((entry != null) && map.directories[path] === 0) {
map.directories[path] = -1;
return fs.rmdir(Path.resolve(map.path, path), function(err) {
if (err != null) {
return next(err);
}
map.directories[entry.directory]--;
return cleanEmptyDirRecursive(entry.directory, next);
});
} else {
return next();
}
};
return deleteEntry(function(err, directory) {
if (err != null) {
return callback(err);
}
return cleanEmptyDirRecursive(directory, callback);
});
};
return this;
};
module.exports = localTransferer;