Created
June 19, 2014 13:14
-
-
Save b-ma/990db2262dcc009f1e5f to your computer and use it in GitHub Desktop.
filesystem node utilities
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
/** | |
* find recursively in a given folder for a given pattern into file names | |
* returns an array of matching files | |
*/ | |
var fs = require('fs'), | |
path = require('path'), | |
util = require('util'); | |
/** | |
* @param root path to the dir to search in | |
* @param regexp regexp to test filenames against | |
* @return array of matched files (path are relative to dirname) | |
*/ | |
var find = function(root, regexp) { | |
if (!fs.existsSync(root) || !fs.lstatSync(root).isDirectory()) { | |
throw new Error(util.format('%s must be a valid directory', root)); | |
} | |
var results = []; | |
(function _find(dirName, regexp) { | |
var content = fs.readdirSync(dirName); | |
content.forEach(function(item, index) { | |
var itemPath = path.join(dirName, item); | |
var isDir = fs.lstatSync(itemPath).isDirectory(); | |
// find recursively | |
if (!isDir) { | |
if (regexp.test(item)) { | |
relativePath = itemPath.replace(root, ''); | |
results.push(relativePath); | |
} | |
} else { | |
_find(itemPath, regexp); | |
} | |
}); | |
}(root, regexp)); | |
return results; | |
} | |
// https://gist.github.com/tkihira/2367067 | |
var rmdir = function(dir) { | |
var list = fs.readdirSync(dir); | |
list.forEach(function(currenPath, index) { | |
var filename = path.join(dir, currenPath); | |
var stat = fs.statSync(filename); | |
if (stat.isDirectory()) { | |
rmdir(filename); | |
} else { | |
fs.unlinkSync(filename); | |
} | |
}); | |
fs.rmdirSync(dir); | |
}; | |
module.exports.find = find; | |
module.exports.rmdir = rmdir; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment