GithubHelp home page GithubHelp logo

dirco's Introduction

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

dirco's People

Contributors

miramac avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.