Created
October 11, 2012 08:01
-
-
Save wondger/3870883 to your computer and use it in GitHub Desktop.
recursive read directory and return all files and directories.
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 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