Skip to content

Instantly share code, notes, and snippets.

@aconbere
Created November 25, 2010 18:26
Show Gist options
  • Select an option

  • Save aconbere/715763 to your computer and use it in GitHub Desktop.

Select an option

Save aconbere/715763 to your computer and use it in GitHub Desktop.
Non-closured-walk
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