Node.js module for listing directories and files recursively. It's using ES 6 generators and co.
It comes also with the regenerator runtime so it can be used with ES 5 engines like node 0.10 or below.
npm install dirco
//ES6
var dirco = require('dirco');
//ES5:
var dirco = require('dirco/es5');
dirco(path [, options] , callback);
//using ES5:
var dirco = require('dirco/es5')
, util = require('util');
dirco('./node_modules', {depth:2}, function(err, result) {
console.log(util.inspect((result), {showHidden: false, depth: null}));
});
depth: number (-1) // level of search depth, -1 is infinity
flat: bool (false) // list items in one array without hierarchy
stats: bool|array|string (true) // append fs.Stats object
Returns directories and files with name, path and fs.Stats info as a tree.
[{
"name": "co",
"path": "node_modules/co",
"type": "directory",
"stats": {
//...
},
"children": [
{
"name": "index.js",
"path": "node_modules/co/index.js",
"type": "file",
"stats": {
//...
}
},
{
"name": "package.json",
"path": "node_modules/co/package.json",
"type": "file",
"stats": {
//...
}
},
{
"name": "Readme.md",
"path": "node_modules/co/Readme.md",
"type": "file",
"stats": {
//...
}
}
]
}]
Get total size of the directory (using flat option)
//using ES5:
var dirco = require('dirco/es5')
dirco('./', {flat:true},function(err, result) {
var i, totalSize = 0;
for(i=0; i<result.length; i++) {
totalSize += result[i].stats.size;
}
console.log(Math.round((totalSize / 1024), 10) + ' KB') ; // 142 KB
});
Searching in directory and file names using the filter option
dirco(__dirname+'/../', {filters:[/^.+\.js$/i], flat:true, stats:false}, function(err, result) {
console.log(result);
});