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

node-readdir

v1.0.1

Published

A lightweight Node.js module to recursively read files in a directory

Downloads

76

Readme

readdir

A lightweight node.js module to recursively read files in a directory.

Installation

npm install readfiles

Usage

readfiles(dir, [options], callback, [doneCallback])

Asynchronusly read the files in a directory

dir

A relative or absolute path of the directory to read files.

options

An optional object parameter with the following properties:

  • reverse: a bollean value that reverses the order of the list of files before traversing them (defaults to false)
  • filenameFormat: one of readdir.FULL_PATH, readdir.RELATIVE, or readdir.FILENAME, wether the callback's returns the full-path, relative-path or only the filenames of the traversed files. (default is readdir.RELATIVE)
  • doneOnError: a bollean value wether to stop and trigger the "doneCallback" when an error occurs (defaults to true)
  • filter: a string, or an array of strings of path expression that match the files being read (defaults to '**')
    • ? matches one character
    • * matches zero or more characters
    • ** matches zero or more 'directories' in a path
  • readContents: a boolean value wether to read the file contents when traversing the files [1] (defaults to true)
  • encoding: a string with the encoding used when reading a file (defaults to 'utf8')
  • depth: an integer value which limits the number sub-directories levels to traverse for the given path where -1 is infinte, and 0 is none (defaults to -1)
  • hidden: a boolean value wether to exclude hidden files prefixed with a . (defaults to true)

callback(err, filename, content)

The callback function that is triggered everytime a file is read. If there's an error while reading the file the err parameter will contain the error that occured, otherwise the if readContents is true, the contents parameter will be populated with the contents of the file encoded using the encoding option.

[1] The contents parameter will be null when the readContents option is false.

doneCallback(err, files, count)

The callback function that is triggered once all the files have been read, passing the number of files read and an array with the full path of all files.

Examples

The default behavior, is to recursively list all files in a directory. By default readdir will exclude all dot files.

var readfiles = require('readfiles');

readfiles('/path/to/dir/', function (err, filename, contents) {
  if (err) throw err;
  console.log('File ' + filename + ':');
  console.log(content);
}, function (err, count, files) {
  console.log('Read ' + count + ' file(s)');
  console.log(files.join('\n'));
});

Read all files in a directory, excluding sub-directories.

var readfiles = require('readfiles');

readfiles('/path/to/dir/', {
  depth: 0
}, function (err, content, filename) {
  if (err) throw err;
  console.log('File ' + filename + ':');
  console.log(content);
}, function (err, count, files) {
  console.log('Read ' + count + ' file(s)');
  console.log(files.join('\n'));
});

The above can also be accomplished using filter.

var readfiles = require('readfiles');

readfiles('/path/to/dir/', {
  filter: '*' // instead of the default '**'
}, function (err, content, filename) {
  if (err) throw err;
  console.log('File ' + filename + ':');
  console.log(content);
}, function (err, count, files) {
  console.log('Read ' + count + ' file(s)');
  console.log(files.join('\n'));
});

Recursively read all files with "txt" extension in a directory and display the contents.

var readfiles = require('readfiles');

readfiles('/path/to/dir/', {
  filter: '*.txt'
}, function (err, content, filename) {
  if (err) throw err;
  console.log('File ' + filename + ':');
  console.log(content);
}, function (err, count, files) {
  console.log('Read ' + count + ' text file(s)');
});

Recursively read all files with that match "t?t" in a directory and display the contents.

var readfiles = require('readfiles');

readfiles('/path/to/dir/', {
  filter: '*.t?t'
}, function (err, content, filename) {
  if (err) throw err;
  console.log('File ' + filename + ':');
  console.log(content);
}, function (err, count, files) {
  console.log('Read ' + count + ' text file(s)');
});

Recursively list all json files in a directory including all sub-directories, without reading the files.

var readfiles = require('readfiles');

readfiles('/path/to/dir/', {
  filter: '*.json',
  readContents: false
}, function (err, content, filename) {
  if (err) throw err;
  console.log('File ' + filename);
});

License

MIT licensed (See LICENSE.txt)