Skip to content

Instantly share code, notes, and snippets.

@millermedeiros
Created February 21, 2012 04:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millermedeiros/1873712 to your computer and use it in GitHub Desktop.
Save millermedeiros/1873712 to your computer and use it in GitHub Desktop.
how to filter and batch delete files on node.js
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);
});
});
}
@millermedeiros
Copy link
Author

maybe I should check if folder is empty and if so delete it as well (since it won't cause SVN conflicts)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment