Created
February 3, 2010 19:23
-
-
Save mikeal/293922 to your computer and use it in GitHub Desktop.
walk(pathname,callback) for node.js
This file contains 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 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