Skip to content

Instantly share code, notes, and snippets.

@mikeal
Created February 3, 2010 19:23
Show Gist options
  • Save mikeal/293922 to your computer and use it in GitHub Desktop.
Save mikeal/293922 to your computer and use it in GitHub Desktop.
walk(pathname,callback) for node.js
var http = require('http'),
sys = require('sys'),
path = require('path'),
posix = require('posix'),
events = require('events');
var listdir = function (pathname) {
var p = new events.Promise();
var ls = process.createChildProcess("ls", [pathname]);
ls.addListener("output", function (data) {
if (data) {
var files = [];
data.split('\n').forEach(function(f) {if (f){files.push(f)}});
p.emitSuccess(files)
}
});
return p;
}
var walk = function (pathname, callback) {
listdir(pathname).addCallback(function(files) {
files.forEach(function(f) {
var abspath = path.join(pathname, f)
posix.stat(abspath).addCallback(function(stat) {
if (stat.isDirectory()) {
walk(abspath, callback);
} else {
callback(abspath);
}
}).wait();
})
}).wait();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment