Merges pull request #57. copyFileSync kept away for the moment, the rest seems rocking.

This commit is contained in:
Ryan McGrath 2013-10-16 07:22:13 +09:00
commit 5cf0de3420
2 changed files with 95 additions and 13 deletions

View file

@ -120,6 +120,8 @@ exports.readdirRecursive = function(baseDir, fn) {
/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent); /* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent);
* *
* Recursively dives through directories and obliterates everything about it. This is a * Recursively dives through directories and obliterates everything about it. This is a
@ -155,6 +157,55 @@ exports.rmdirSyncRecursive = function(path, failSilent) {
return fs.rmdirSync(path); return fs.rmdirSync(path);
}; };
function isFileIncluded(opts, dir, filename) {
function isMatch(filter) {
if (typeof filter === 'function') {
return filter(filename, dir) === true;
}
else {
// Maintain backwards compatibility and use just the filename
return filename.match(filter);
}
}
if (opts.include || opts.exclude) {
if (opts.exclude) {
if (isMatch(opts.exclude)) {
return false;
}
}
if (opts.include) {
if (isMatch(opts.include)) {
return true;
}
else {
return false;
}
}
return true;
}
else if (opts.filter) {
var filter = opts.filter;
if (!opts.whitelist) {
// if !opts.whitelist is false every file or directory
// which does match opts.filter will be ignored
return isMatch(filter) ? false : true;
} else {
// if opts.whitelist is true every file or directory
// which doesn't match opts.filter will be ignored
return !isMatch(filter) ? false : true;
}
}
return true;
}
/* wrench.copyDirSyncRecursive("directory_to_copy", "new_directory_location", opts); /* wrench.copyDirSyncRecursive("directory_to_copy", "new_directory_location", opts);
* *
* Recursively dives through a directory and moves all its files to a new location. This is a * Recursively dives through a directory and moves all its files to a new location. This is a
@ -164,14 +215,16 @@ exports.rmdirSyncRecursive = function(path, failSilent) {
* Note: Directories should be passed to this function without a trailing slash. * Note: Directories should be passed to this function without a trailing slash.
*/ */
exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) { exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
opts = opts || {};
try { try {
if(fs.statSync(newDirLocation).isDirectory()) { if(fs.statSync(newDirLocation).isDirectory()) {
if(typeof opts !== 'undefined' && opts.forceDelete) { if(typeof opts !== 'undefined' && opts.forceDelete) {
exports.rmdirSyncRecursive(newDirLocation); exports.rmdirSyncRecursive(newDirLocation);
} else { } else {
return new Error('You are trying to delete a directory that already exists. Specify forceDelete in the opts argument to override this. Bailing~'); return new Error('You are trying to delete a directory that already exists. Specify forceDelete in the opts argument to override this. Bailing~');
} }
} }
} catch(e) { } } catch(e) { }
/* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */ /* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */
@ -184,13 +237,18 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
} }
var files = fs.readdirSync(sourceDir); var files = fs.readdirSync(sourceDir);
var hasFilter = opts.filter || opts.include || opts.exclude;
var preserveFiles = opts.preserveFiles === true;
for(var i = 0; i < files.length; i++) { for(var i = 0; i < files.length; i++) {
// ignores all files or directories which match the RegExp in opts.filter // ignores all files or directories which match the RegExp in opts.filter
if(typeof opts !== 'undefined') { if(typeof opts !== 'undefined') {
if(!opts.whitelist && opts.filter && files[i].match(opts.filter)) continue; if (hasFilter) {
// if opts.whitelist is true every file or directory which doesn't match opts.filter will be ignored if (!isFileIncluded(opts, sourceDir, files[i])) {
if(opts.whitelist && opts.filter && !files[i].match(opts.filter)) continue; continue;
}
}
if (opts.excludeHiddenUnix && /^\./.test(files[i])) continue; if (opts.excludeHiddenUnix && /^\./.test(files[i])) continue;
} }
@ -295,10 +353,15 @@ exports.rmdirRecursive = function rmdirRecursive(dir, failSilent, clbk){
fs.readdir(dir, function(err, files) { fs.readdir(dir, function(err, files) {
if(err && typeof failSilent === 'boolean' && !failSilent) if(err && typeof failSilent === 'boolean' && !failSilent)
return clbk(err); return clbk(err);
<<<<<<< HEAD
if(typeof failSilent === 'function') if(typeof failSilent === 'function')
clbk = failSilent; clbk = failSilent;
=======
if(typeof failSilent === 'function')
clbk = failSilent;
>>>>>>> 7827a700ff8cb8b742e401e9876e86a63ae0c68a
(function rmFile(err){ (function rmFile(err){
if (err) return clbk(err); if (err) return clbk(err);
@ -333,6 +396,7 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, opts, clbk)
fs.stat(newDir, function(err, newDirStat){ fs.stat(newDir, function(err, newDirStat){
if(!err) { if(!err) {
<<<<<<< HEAD
if(typeof opts !== 'undefined' && typeof opts !== 'function' && opts.forceDelete) if(typeof opts !== 'undefined' && typeof opts !== 'function' && opts.forceDelete)
return exports.rmdirRecursive(newDir, function(err) { return exports.rmdirRecursive(newDir, function(err) {
copyDirRecursive.apply(this, originalArguments); copyDirRecursive.apply(this, originalArguments);
@ -343,6 +407,18 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, opts, clbk)
if(typeof opts === 'function') if(typeof opts === 'function')
clbk = opts; clbk = opts;
=======
if(typeof opts !== 'undefined' && typeof opts !== 'function' && opts.forceDelete)
return exports.rmdirRecursive(newDir, function(err){
copyDirRecursive.apply(this, arguments);
});
else
return clbk(new Error('You are trying to delete a directory that already exists. Specify forceDelete in an options object to override this.'));
}
if(typeof opts === 'function')
clbk = opts;
>>>>>>> 7827a700ff8cb8b742e401e9876e86a63ae0c68a
fs.stat(srcDir, function(err, srcDirStat){ fs.stat(srcDir, function(err, srcDirStat){
if (err) return clbk(err); if (err) return clbk(err);

View file

@ -66,10 +66,16 @@ wrench.copyDirSyncRecursive('directory_to_copy', 'location_where_copy_should_end
excludeHiddenUnix: bool, // Whether to copy hidden Unix files or not (preceding .) excludeHiddenUnix: bool, // Whether to copy hidden Unix files or not (preceding .)
preserveFiles: bool, // If we're overwriting something and the file already exists, keep the existing preserveFiles: bool, // If we're overwriting something and the file already exists, keep the existing
inflateSymlinks: bool, // Whether to follow symlinks or not when copying files inflateSymlinks: bool, // Whether to follow symlinks or not when copying files
filter: regexp, // A filter to match files against; if matches, do nothing (exclude). filter: regexpOrFunction, // A filter to match files against; if matches, do nothing (exclude).
whitelist: bool, // if true every file or directory which doesn't match filter will be ignored whitelist: bool, // if true every file or directory which doesn't match filter will be ignored
include: regexpOrFunction, // An include filter (either a regexp or a function)
exclude: regexpOrFunction // An exclude filter (either a regexp or a function)
}); });
// Note: If a RegExp is provided then then it will be matched against the filename. If a function is
// provided then the signature should be the following:
// function(filename, dir) { return result; }
// Read lines in from a file until you hit the end // Read lines in from a file until you hit the end
var f = new wrench.LineReader('x.txt'); var f = new wrench.LineReader('x.txt');
while(f.hasNextLine()) { while(f.hasNextLine()) {