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

directory-contents

v2.0.0

Published

Recursively loads directory contents as an object.

Downloads

1

Readme

directory-contents

npm install directory-contents --save

Recursively loads directory contents as an object.

Usage

var directoryContents = require('directory-contents');

directoryContents(path [, options], callback)

Asynchronously loads the directory contents, passed to callback.

options is an optional object which can contain the following properties:

  • extensions (default: see below) maps file extensions to loader functions.
  • recursive (default: true) whether to browse subdirectories.
  • stripExtensions (default: true) whether the extensions should be omitted in the contents' properties.

callback takes as first argument an Error object if an error has occured, otherwise undefined, and as second argument the contents object.

Extensions

The options.extensions object specifies how the files are loaded. Keys are lowercase file extensions ("js", "txt"...) and values are asynchronous loader functions with the signature (path: string [, fileStats: fs.Stats], callback: (err|null, contents)). "*" is a special key which matches unspecified extensions.

Convenient loaders are provided:

  • directoryContents.readFile: reads the file contents as a Buffer (no encoding is presupposed)
  • directoryContents.readText: reads the file contents as a String (encoding is UTF8)
  • directoryContents.require: requires the file (works on JS and JSON files)

The default extensions are:

{
	"js": directoryContents.require,
	"json": directoryContents.require,
	"txt": directoryContents.readText,
	"*": directoryContents.readFile
}

When specifying a custom extensions object, it will fully override these default extensions.

Example: only parse YAML files

var directoryContents = require('directory-contents');
var fs = require('fs');
var yaml = require('js-yaml');

directoryContents('test/dir', {
	extensions: {
		yaml: function(filename, callback) {
			return fs.readFile(filename, {
				encoding: 'utf8'
			}, function(err, data) {
				if (err) return callback(err);
				
				var content;
				try {
					content = yaml.load(data);
				} catch (err) {
					return callback(err);
				}
				
				return callback(null, content);
			});
		}
	}
}, function(err, contents) { ... });

Synchronous version

In some cases, such as the start-up initialization, you may require to synchronously load the directory contents. For that, use directoryContents.sync. The explanations above stay true, except that the functions return the values instead of passing them to a callback.

There are also convenient synchronous loaders with the same purpose as the asynchronous ones: directoryContents.sync.readFile, directoryContents.sync.readText, and directoryContents.sync.require.

Example: synchronously parse YAML files

var directoryContents = require('directory-contents');
var fs = require('fs');
var yaml = require('js-yaml');

var contents = directoryContents.sync('test/dir', {
	extensions: {
		yaml: function(filename) {
			var data = fs.readFileSync(filename, {
				encoding: 'utf8'
			});
			return yaml.load(data);
		}
	}
});
...

Although this example is smaller than the previous one, don't let this fool you. In most cases, you should use the asynchronous version, as it implies IO operations.

Test example

Here is the structure of the directory dir (located in the directory test):

dir
 |- config
 |   |- development.js
 |- math.js
 |- recipe.txt

The following code loads the directory contents:

directoryContents('test/dir', function(err, contents) {
	if (err) throw err;
	console.log(contents);
});

Output:

{
	config: {
		development: {
			port: 3000
		}
	},
	math: {
		add: [Function]
	},
	recipe: 'Choux à la crème',
	token: <Buffer 34 32>
}

License

Copyright (c) 2015 Bloutiouf aka Jonathan Giroux

MIT License