Created
November 24, 2010 20:26
-
-
Save aconbere/714339 to your computer and use it in GitHub Desktop.
JS async walk
This file contains hidden or 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 fs = require("fs"); | |
| var path = require("path"); | |
| var walk = function (start, callback) { | |
| fs.lstat(start, function (err, stat) { | |
| if (err) return callback(err) | |
| if (stat.isDirectory() && !stat.isSymbolicLink()) { | |
| fs.readdir(start, function (err, files) { | |
| if (err) return callback(err) | |
| var dirs = [] | |
| , notDirs = [] | |
| , IL = files.length | |
| for (i = 0; i < IL; i++) { | |
| var abspath = path.join(start, files[i]); | |
| var _stat = fs.lstatSync(abspath); | |
| if (_stat.isDirectory() && !_stat.isSymbolicLink()) { | |
| dirs.push(abspath); | |
| walk(abspath, callback); | |
| } else { | |
| notDirs.push(abspath); | |
| } | |
| } | |
| return callback(null, start, dirs, notDirs); | |
| }); | |
| } else { | |
| return callback(new Error("path: " + start + " is not a directory")); | |
| } | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment