Created
February 21, 2012 04:38
-
-
Save millermedeiros/1873712 to your computer and use it in GitHub Desktop.
how to filter and batch delete files on node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DIST_FOLDER = '../deploy/'; | |
// --- | |
var _minimatch = require('minimatch'), | |
_wrench = require('wrench'), | |
_fs = require('fs'), | |
_path = require('path'); | |
function purgeDeploy(){ | |
var files = _wrench.readdirSyncRecursive(DIST_FOLDER); | |
files = filterFiles(files, [ | |
'{**/,}.svn{/**,}' | |
]); | |
files.forEach(function(path){ | |
path = _path.join(DIST_FOLDER, path); | |
//don't remove folders since it will cause SVN conflict | |
if( _fs.statSync(path).isFile() ) { | |
_fs.unlinkSync(path); | |
} | |
}); | |
} | |
function filterFiles(files, excludes) { | |
var globOpts = { | |
matchBase: true, | |
dot : true | |
}; | |
excludes = excludes.map(function(val){ | |
//minimatch currently have a bug with star globs (https://github.com/isaacs/minimatch/issues/5) | |
//so use makeRe instead | |
return _minimatch.makeRe(val, globOpts); | |
}); | |
return files.filter(function(filePath){ | |
return ! excludes.some(function(glob){ | |
return glob.test(filePath); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maybe I should check if folder is empty and if so delete it as well (since it won't cause SVN conflicts)