Added support for include and exclude filters and function filters. Also export copyFileSync method now
This commit is contained in:
parent
31cf44b999
commit
5acb4ed037
1 changed files with 89 additions and 27 deletions
|
|
@ -121,6 +121,8 @@ exports.readdirRecursive = function(baseDir, fn) {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent);
|
||||
*
|
||||
* Recursively dives through directories and obliterates everything about it. This is a
|
||||
|
|
@ -156,6 +158,62 @@ exports.rmdirSyncRecursive = function(path, failSilent) {
|
|||
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;
|
||||
}
|
||||
|
||||
exports.copyFileSync = function(srcFile, destFile, preserveFiles) {
|
||||
if (preserveFiles && fs.existsSync(destFile)) return;
|
||||
|
||||
var contents = fs.readFileSync(srcFile);
|
||||
fs.writeFileSync(destFile, contents);
|
||||
};
|
||||
|
||||
/* 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
|
||||
|
|
@ -165,6 +223,8 @@ exports.rmdirSyncRecursive = function(path, failSilent) {
|
|||
* Note: Directories should be passed to this function without a trailing slash.
|
||||
*/
|
||||
exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
|
||||
opts = opts || {};
|
||||
|
||||
try {
|
||||
if(fs.statSync(newDirLocation).isDirectory()) {
|
||||
if(typeof opts !== 'undefined' && opts.forceDelete) {
|
||||
|
|
@ -185,23 +245,25 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
|
|||
}
|
||||
|
||||
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++) {
|
||||
// ignores all files or directories which match the RegExp in opts.filter
|
||||
if(typeof opts !== 'undefined') {
|
||||
if(!opts.whitelist && opts.filter && files[i].match(opts.filter)) continue;
|
||||
// if opts.whitelist is true every file or directory which doesn't match opts.filter will be ignored
|
||||
if(opts.whitelist && opts.filter && !files[i].match(opts.filter)) continue;
|
||||
if (hasFilter) {
|
||||
if (!isFileIncluded(opts, sourceDir, files[i])) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.excludeHiddenUnix && /^\./.test(files[i])) continue;
|
||||
}
|
||||
|
||||
var currFile = fs.lstatSync(_path.join(sourceDir, files[i]));
|
||||
|
||||
var fCopyFile = function(srcFile, destFile) {
|
||||
if(typeof opts !== 'undefined' && opts.preserveFiles && fs.existsSync(destFile)) return;
|
||||
|
||||
var contents = fs.readFileSync(srcFile);
|
||||
fs.writeFileSync(destFile, contents);
|
||||
exports.copyFileSync(srcFile, destFile, preserveFiles);
|
||||
};
|
||||
|
||||
if(currFile.isDirectory()) {
|
||||
|
|
|
|||
Reference in a new issue