npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fs-walker

v1.0.0

Published

Recursively walk through the filesystem, searching for files and directories, either async or synchronous while optionally filtering.

Downloads

160

Readme

node-fs-walker

Recursively walk through the filesystem, searching for files and directories, either async or synchronous while optionally filtering.

The walker will always respond with an fs_stats instances, retrieved using lstat and decorated with extra properties:

  • directory
  • name
  • fullname (which is a concatenation of directory and name)

#Installation Install using npm:

npm install --save fs-walker

Then require:

var walk = require('fs-walker');

#Usage

node-fs-walker comes in three flavours:

  • Async
  • Sync
  • Events

The first two flavours both can walk through three options:

  • files
  • directories
  • both

The third flavour has the following events defined, depending on the type found:

  • file
  • dir
  • block
  • character
  • symbolic
  • fifo
  • socket
  • unknown

##Initial

The following samples all use this setup:

var dir = process.cwd(),
    walk = require('fs-walker');

##Async

###Files

walk(dir, function(stats) {
  console.log(stats.fullname);
});

note: walk is a shortcut for walk.files, so the following works as well. This will make more sense in the other samples.

walk.files(dir, function(stats) {
  console.log(stats.fullname);
});

###Directories

walk.directories(dir, function(stats) {
  console.log(stats.fullname);
});

###Both

walk.all(dir, function(stats) {
  console.log(stats.fullname);
});

##Sync

All the above handlers have a synchronous variant, which maps with the async callers.

  • walk.sync or walk.files.sync
  • walk.directories.sync
  • walk.all.sync

These return an array of fs_stats instances instead.

##Events

note: Under the hood, the async walker will be used.

var walker = new walk.Walker(dir);

walker.on('file', function(stats) {
        console.log('file event: %s', stats.fullname);
      })
      .on('dir', function(stats) {
        console.log('dir event: %s', stats.fullname);
      })
      .walk();

##Filters

It is possible to filter both files and directories. The filter also receives an fs_stats instance.

The filter looks as follows:

var filter = { 
  file: function(stats) {
    return /\.js$/i.test(stats.name);
  }, 
  directory: function(stats) {
    return stats.name !== 'node_modules';
  }
};

This filter will make it so that only files are returned that end in .js and will not be looking in the node_modules folder while walking the file-system.

It is possible to omit the file, directory or both filters, e.g.:

var filter = { 
  directory: function(stats) {
    return stats.name !== 'node_modules';
  }
};

The filter handler is picked based on fs_stats's isDirectory(). Using the filter is done by passing it as the second argument, as follows:

###Async

walk(dir, filter, function(stats) {
    console.log(stats.fullname);
});

###Sync

walk.sync(dir, filter).forEach(function(stats) {
    console.log(stats.fullname);
});

###Events

var walker = new walk.Walker(dir);

walker.on('file', function(stats) {
        console.log('file event: %s', stats.fullname);
      })
      .on('dir', function(stats) {
        console.log('dir event: %s', stats.fullname);
      })
      .walk(filter);