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 🙏

© 2025 – Pkg Stats / Ryan Hefner

dirtree

v0.0.2

Published

Translate a directory structure into a tree object.

Readme

DirTree

NPM version Build Status Coverage Status Dependencies

Translate a directory structure into a tree object.

This module translates a directory structure into a tree object and provides an interface to search and load leaves (files) within that tree.

Installation

$ npm install dirtree

Usage

To create a new tree generator,

var createTree = require( 'dirtree' );

var tree = createTree();

A tree is configurable and has the following methods...

tree.root( [root] )

This method is a setter/getter. If no root directory is provided, returns the tree root. To set the tree root,

tree.root( __dirname );

The root should be an absolute path.

tree.include( type[, filter] )

This method is a setter/getter for regular expression filters to include particular files and directories. The method accepts two types: files and dirs. If no filter is provided, returns the inclusion filter for the specified type. To set an inclusion filter,

// Only include CSS files from build or styles directories:
tree.include( 'dirs', /build|styles/ ); // /build, /styles, /styles/build, /build/styles
tree.include( 'files', /.+\.css/ );

tree.exclude( type[, filter] )

This method is a setter/getter for regular expression filters to exclude particular files and directories. The method accepts two types: files and dirs. If no filter is provided, returns the exclusion filter for the specified type. To set an exclusion filter,

// Exclude any hidden directories and dot files:
tree.exclude( 'dirs', /^\./ );
tree.exclude( 'files', /^\./ );

tree.create()

This method creates a directory tree. To create a tree,

tree.create();

You must first set a root directory before running this method.

tree.leaves()

This method returns all tree leaves. If a tree has not been created, leaves will be an empty array. To return all tree leaves,

tree.leaves();

Note: the array elements will be relative paths from the root directory.

tree.search( include[, exclude] )

This method searches a tree for leaves matching the provided regular expression filters. Either an include or exclude or both filters are required. To only specify an exclude filter, set the include filter to null. To perform a search,

// Search inclusively for `*.md` files:
tree.search( /+.\.md$/ );

// Search for any files which are not `*.txt` files:
tree.search( null, /.+\.txt$/ );

// Search both inclusively and exclusively for all files having `foo` but not `bar` in their relative paths:
tree.search( /foo/, /bar/ );

tree.read( [options,] clbk )

This method searches a tree for leaves matching provided filters and reads the leaves ( files), returning the file content. The options object may have one or more of the following fields:

var options = {
		'include': /foo/,
		'exclude': /bar/,
		'encoding': 'utf8',
		'concat': true
	};

The filters are the same as for tree.search(). The encoding option is the file encoding. The concat flag indicates whether the file content should be concatenated and returned as a string. If set to false, the file content is returned as an object, where each field is the absolute file path and each value is the corresponding file content.

To read leaves and concatentate the file content into a single string,

var options = {
		'include': /.+\.css$/,
		'exclude': /src/,
		'encoding': 'utf8',
		'concat': true
	};

// Read and concatenate all CSS files not in a `src` directory:
tree.read( options, onRead );

function onRead( error, content ) {
	if ( error ) {
		console.error( error );
		return;
	}
	console.log( content );
}

tree.toJSON()

This method serializes a directory tree as JSON. To get the JSON tree,

tree.toJSON();

Examples

var createTree = require( 'dirtree' );

// Create a new tree generator:
var tree = createTree();

// Configure and create a tree:
tree
	.root( __dirname )
	.exclude( 'dirs', /^\./ )
	.exclude( 'files', /^\./ )
	.create();

// Serialize the tree:
console.log( tree.toJSON() );

// List the leaves:
console.log( tree.leaves() );

// Search the leaves:
console.log( tree.search( /index.js/ ) );

// Read the leaves:
console.log(
	tree.read({
		'include': /\.txt$/,
		'encoding': 'utf8',
		'concat': true
	},
	function onRead( text ) {
		console.log( text );
	})
);

To run example code from the top-level application directory,

$ node ./examples/index.js

Notes

This module currently only supports directories and files and does not support symlinks, devices, FIFO, or sockets.


Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ open reports/coverage/lcov-report/index.html

Prior Art

Event-emitters:

Filesystem:


License

MIT license.


Copyright

Copyright © 2014. Athan Reines.