Giter VIP home page Giter VIP logo

listdirs's Introduction

listdirs

List Directories asynchronously in node.js.

Build Status codecov Dependency Status npm version Node.js Version

Why?

We needed an easy way of listing all the directories in a project so we could watch them for changes.
We reviewed many available options and found them lacking in one of the following areas:

  1. Untested (or incomplete tests)
  2. Patchy documentation (often none)
  3. Unmaintained or Abandoned (many open/unaddressed issues on GitHub)
  4. Unclear code (written without shoshin)
  5. Too Many Features trying to do too much. (we only need one thing a list of the directories)

too many features

What?

Given an initial directory (e.g. the Current Working Directory) give me a list of all the "child" directories.

How? (Usage)

Install from NPM

npm install listdirs --save

In your code:

var listdirs = require('listdirs');
var basedir = __dirname; // or which ever base directory you prefer
listdirs(basedir, function callback(err, list){
    if(err){
      console.log(err); // handle errors in your preferred way.
    }
    else {
      console.log(list); // use the array of directories as required.
    }
});

(Optional) Supply a List of Files/Directories to Ignore

If you have a large project and want to ignore the files in your .gitignore file (e.g. node_modules), there's an easy way to do this:

var listdirs = require('listdirs');
var ignored  = require('ignored')('./.gitignore'); // https://github.com/nelsonic/ignored
var basedir  = __dirname; // or which ever base directory you prefer
listdirs(basedir, function callback(err, list){
    if(err){
      console.log(err); // handle errors in your preferred way.
    }
    else {
      console.log(list); // use the array of directories as required.
    }
}, ignored); // include ignored list as 3rd Parameter (after callback)

Note: This example uses our ignored module: https://www.npmjs.com/package/ignored as an optional helper to list the entries in .gitignore file
but you can supply your list of ignored files as a simple array e.g: var ignored = ['node_modules', '.git', '.vagrant', 'etc.'];


Research

Asynchronous (non-blocking) without Async (the module)

The async (module) is good (as evidenced by its popularity!)
But way too many people use it as a crutch instead of understanding how to write their own asynchronous code.
We have deliberately avoided using async (the module) here, and as a result, listdirs is faster (we benchmarked it!) and includes less bloat.

We have included one dependency on isdir for the sake of splitting out code into "does-only-one-thing" (micro-modules) but isdir has zero dependencies so we know the stack!

Existing Options

As usual, a search on NPM (for list directories) returns many results:

npm-search-list-directories

A few of the modules we looked at before deciding to write our own:

Background Reading

Highly recommend reading the Unix Philosophy if you haven't already.

listdirs's People

Contributors

iteles avatar nelsonic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

listdirs's Issues

[Question] Listing the first set of subdirectories in a working directory

Is it possible to get/list the first set of subdirectories in a working directory using listdirs so we are able to do something with them later on??

So for example let's say we have a working directory stored in a variable like so

var workDir = 'C:/Users/user/just/some/folder/location';

Would I be able to list the first set of subdirectories inside this workDir and nothing else?? Something like the example below (apologies if the example contains incorrect structure for folder hierarchy)

<workDir>
├── folder # <— contains more subfolders but don't list them
├── folder2 # <— contains more subfolders but don't list them
├── folder3 # <— contains more subfolders but don't list them
├── folder4 # <— contains more subfolders but don't list them
├── folder5 # <— contains more subfolders but don't list them
├── TestFile.xml
├── TestFile.yml
└── TestFile.txt

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.