Skip to content

Instantly share code, notes, and snippets.

@aconbere
Created April 23, 2010 21:43
Show Gist options
  • Save aconbere/377225 to your computer and use it in GitHub Desktop.
Save aconbere/377225 to your computer and use it in GitHub Desktop.
path.walk = function (start, callback) {
fs.stat(start, function (err, stat) {
if (stat.isDirectory()) {
fs.readdir(start, function (err, files) {
var coll = files.reduce(function (acc, i) {
var abspath = path.join(start, i);
if (fs.statSync(abspath).isDirectory()) {
path.walk(abspath, callback);
acc.dirs.push(abspath);
} else {
acc.names.push(abspath);
}
return acc;
}, {"names": [], "dirs": []});
callback(start, coll.dirs, coll.names);
});
} else {
return null;
}
});
};
path.walkSync = function (start, callback) {
var stat = fs.statSync(start);
if (stat.isDirectory()) {
var coll = fs.readdirSync(start).reduce(function (acc, i) {
var abspath = path.join(start, i);
if (fs.statSync(abspath).isDirectory()) {
path.walk(abspath, callback);
acc.dirs.push(abspath);
} else {
acc.names.push(abspath);
}
return acc;
}, {"names": [], "dirs": []});
return callback(start, coll.dirs, coll.names);
} else {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment