Skip to content

Instantly share code, notes, and snippets.

@zenlor
Created December 26, 2011 15:35
Show Gist options
  • Save zenlor/1521429 to your computer and use it in GitHub Desktop.
Save zenlor/1521429 to your computer and use it in GitHub Desktop.
walk(dirname, callback, finished) - asynchronous directory traversal in node.js
var fs = require('fs')
, path = require("path")
, cp = require('child_process')
, Step = require('step')
, colors = require('colors')
, start = Date.now();
walk = (function() {
var counter = 0;
var walk = function(dirname, callback, finished) {
counter += 1;
fs.readdir(dirname, function(err, relnames) {
if(err) {
finished(err);
return;
}
relnames.forEach(function(relname, index, relnames) {
var name = path.join(dirname, relname);
counter += 1;
fs.lstat(name, function(err, stat) {
if(err) {
finished(err);
return;
}
if(stat.isDirectory()) {
walk(name, callback, finished);
} else {
callback(name);
}
counter -= 1;
if(index === relnames.length - 1) counter -= 1;
if(counter === 0) {
finished(null);
}
});
});
});
};
return walk;
})();
var js = ex = sp = '';
Step(
function () {
var self = this;
walk(
"./node_modules"
, function(data) {
js += (data + '\n');
}
, function(err) {
if(err) throw err;
console.log("walk() Finished in: %d ms".red, (Date.now()) - start);
return self();
}
)
}
, function () {
var self = this;
start = Date.now();
cp.exec('find ./node_modules -type f', function(error, stdout, stderr) {
if (error) throw error;
console.log("exec('find ...') Finished in: %d ms".red, (Date.now()) - start);
ex = stdout;
self();
});
}
, function () {
var self = this;
start = Date.now();
var cmd = cp.spawn('find', ['./node_modules', '-type', 'f']);
cmd.stdout.on('data', function (data) {
sp += data;
});
cmd.on('exit', function () {
console.log("spawn('find ...') Finished in: %d ms".red, (Date.now()) - start);
self();
});
}
, function () {
process.exit(); }
);
in my node_modules folder ...
walk() Finished in: 30 ms
exec('find ...') Finished in: 14 ms
spawn('find ...') Finished in: 6 ms
using node 0.8.1
walk() Finished in: 17 ms
exec('find ...') Finished in: 8 ms
spawn('find ...') Finished in: 5 ms
results may vary
{
"name": "gist-1521429",
"version": "0.0.0",
"main": "Benchmark-ish.js",
"dependencies": {},
"devDependencies": {
"colors": "~0.6.0-1",
"express": "~2.5.11",
"step": "~0.0.5"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://gist.github.com/1521429.git"
},
"license": "BSD"
}
require("./walk").walk(
"/path/to/a/directory",
function(file) {
console.log(file);
},
function(err) {
if(err) throw err;
console.log("Finished.");
}
);
var fs = require("fs");
var path = require("path");
exports.walk = (function() {
var counter = 0;
var walk = function(dirname, callback, finished) {
counter += 1;
fs.readdir(dirname, function(err, relnames) {
if(err) {
finished(err);
return;
}
relnames.forEach(function(relname, index, relnames) {
var name = path.join(dirname, relname);
counter += 1;
fs.stat(name, function(err, stat) {
if(err) {
finished(err);
return;
}
if(stat.isDirectory()) {
exports.walk(name, callback, finished);
} else {
callback(name);
}
counter -= 1;
if(index === relnames.length - 1) counter -= 1;
if(counter === 0) {
finished(null);
}
});
});
});
};
return walk;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment