Skip to content

Instantly share code, notes, and snippets.

@wondger
Created October 11, 2012 08:01
Show Gist options
  • Save wondger/3870883 to your computer and use it in GitHub Desktop.
Save wondger/3870883 to your computer and use it in GitHub Desktop.
recursive read directory and return all files and directories.
var utils = {
isFile: function(path) {
var stat = fs.statSync(path);
return stat ? stat.isFile() : undefined;
},
isDir: function(path) {
var stat = fs.statSync(path);
return stat ? stat.isDirectory() : undefined;
},
readDir: function(dir, callback) {
var files = [],
dirs = [];
_readDir(dir);
callback(null, {
files: files,
dirs: dirs
});
function _readDir(dir) {
var dir = dir.match(/\/$/) ? dir : dir + "/",
fd = [];
try {
fd = fs.readdirSync(dir);
fd.forEach(function(file){
if (utils.isDir(dir + file)) {
dirs.push(dir + file);
_getMds(dir + file);
}
else if (utils.isFile(dir + file) {
files.push(dir + file);
}
})
}
catch (err) {
callback(err);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment