Last active
August 29, 2015 14:24
-
-
Save dypsilon/58e7bf737f716b57b2a5 to your computer and use it in GitHub Desktop.
Recursive readdir using highland streams.
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
/** | |
* 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