dirco
Lists directories and files recursively using ES 6 generators.
Last updated 3 years ago by miramac .
ISC · Repository · Bugs · Original npm · Tarball
$ cnpm install dirco 
SYNC missed versions from official npm registry.

dirco

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.

Install

  npm install dirco

Usage

//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}));
});

Options

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": {
          //...
        }
      }
    ]
  }]

Examples

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);
});

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 13
Dependencies (1)
  • co ^3.1.0
Dev Dependencies (9)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |