Skip to content

Instantly share code, notes, and snippets.

@dypsilon
Last active August 29, 2015 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dypsilon/58e7bf737f716b57b2a5 to your computer and use it in GitHub Desktop.
Save dypsilon/58e7bf737f716b57b2a5 to your computer and use it in GitHub Desktop.
Recursive readdir using highland streams.
/**
* This function takes a directory path and returns a flat stream
* with stat objects + full file path in the .path property.
*/
var path = require('path');
var h = require('highland');
var readdir = h.wrapCallback(require('fs').readdir);
var stat = h.wrapCallback(require('fs').stat);
/**
* In this usage example we scan the / directory,
* then filter the stream to leave only directories (no files),
* then extract only the path property
* and finally log the resulting items to the console.
* This code is completely asynchronous.
*/
readdirp('/')
.filter(function(x) { return x.isDirectory(); })
.pluck('path')
.each(h.log);
// implementation
function readdirp(base) {
return readdir(base)
.flatten()
.map(function(x) { return path.join(base, x); })
.flatMap(function(filepath) {
return stat(filepath).map(function(x) {
x.path = filepath;
return x;
});
})
.map(function(file) {
if (file.isDirectory()) {
return [file, readdirp(file.path)];
} else {
return [file];
}
})
.flatten();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment