Created
November 25, 2010 18:26
-
-
Save aconbere/715763 to your computer and use it in GitHub Desktop.
Non-closured-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 handleReadDirFactory = function (start, callback) { | |
| var handleReadDir = 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); | |
| }; | |
| return handleReadDir; | |
| }; | |
| var handleStartStatFactory = function (start, callback) { | |
| var handleStartStat = function (err, stat) { | |
| if (err) return callback(err) | |
| if (stat.isDirectory() && !stat.isSymbolicLink()) { | |
| fs.readdir(start, handleReadDirFactory(start, callback)); | |
| } else { | |
| return callback(new Error("path: " + start + " is not a directory")); | |
| } | |
| }; | |
| return handleStartStat; | |
| }; | |
| var walk = function (start, callback) { | |
| fs.lstat(start, handleStartStatFactory(start, callback)); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment