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

require-folder-tree

v1.4.7

Published

Utility to require multiple files in a folder tree with flexible options

Downloads

16,040

Readme

require-folder-tree.js

Utility to require multiple files in a folder tree with flexible options

Current status

NPM version Build Status Dependency Status Dev dependency Status Coverage Status

API is stable and tests cover all options. No known issues.

Basic usage

requireFolderTree( path [, options] )

To require all the .js and .json files in a folder and any sub-folders:

var requireFolderTree = require('require-folder-tree');
var modules = requireFolderTree('/path/to/folder');

If the file structure is:

/
  a.js
  b.js
  c/
    d.js
    e.js

The returned value is:

{
  a: ...,
  b: ...,
  c: {
    d: ...,
    e: ...
  }
}

Advanced usage

Options can be passed to alter the behaviour.

recurse

When true, recurses through subfolders and sub-subfolders requiring an entire tree. Set to false to ignore subfolders. Defaults to true.

// Require only files in base folder
requireFolderTree('/path/to/folder', { recurse: false });
// returns { a: ..., b: ... }

filterFiles

A regular expression for what files to include. First matching group defines the key used in returned object. Defaults to /^(.+)\.js(on)?$/ (i.e. include all .js and .json files)

// Include all .js files except those starting with `_`
requireFolderTree('/path/to/folder', { filterFiles: /^([^_].*)\.js$/ });

filterFolders

A regular expression for what folders to iterate into. First matching group defines the key used in returned object. Defaults to /^([^\.].*)$/ (i.e. process all folders except those beginning with .)

// Process all folders except those starting with `.` or `_`
requireFolderTree('/path/to/folder', { filterFolders: /^([^\._].*)$/ });

fileNameTransform

Function for transforming object keys for files.

// Transform file names to lower case
requireFolderTree('/path/to/folder', {
	fileNameTransform: function(str) { return str.toLowerCase(); }
});

folderNameTransform

Like fileNameTransform, but for folder names.

By default, inherits value of options.fileNameTransform. If you want to define a fileNameTransform function but leave folder names unchanged, set options.folderNameTransform = null.

fileNameAttribute

When set, saves the file name (after fileNameTransform and flatten have been applied) as an attribute of each require-d file. If true, uses value 'name'. Defaults to undefined.

requireFolderTree('/path/to/folder', { fileNameAttribute: true, recurse: false });
// returns { a: { name: 'a', ... }, b: { name: 'b', ... } }

folderNameAttribute

Like fileNameAttribute, but for folder names.

By default, inherits value of options.fileNameAttribute. If you want to save file names only, set options.folderNameAttribute = null.

fileParentAttribute

When set, saves the parent folder as an attribute of each require-d file. If true, uses value 'parent'. Defaults to undefined.

requireFolderTree('/path/to/folder', { fileParentAttribute: true, recurse: false });
// returns { a: { parent: <reference to root object>, ... }, b: { parent: <reference to root object>, ... } }

folderParentAttribute

Like fileParentAttribute, but for folders.

By default, inherits value of options.fileParentAttribute. If you want to save file parents only, set options.folderParentAttribute = null.

flatten

Flattens the folder structure when true. Defaults to false.

requireFolderTree('/path/to/folder', { flatten: true });
// returns { a: ..., b: ..., d: ..., e: ... }

flattenPrefix

Controls how the object keys are created for files nested in folders when flattening. true concatenates the folder name and file name. Defaults to false

requireFolderTree('/path/to/folder', { flatten: true, flattenPrefix: true });
// returns { a: ..., b: ..., cd: ..., ce: ... }

flattenCamel

When true, camel-cases the keys when concatenating the folder name and file name. Defaults to false

requireFolderTree('/path/to/folder', { flatten: true, flattenCamel: true });
// returns { a: ..., b: ..., cD: ..., cE: ... }

flattenSeparator

Sets separator between folder name and file name when concatenating. Defaults to undefined.

requireFolderTree('/path/to/folder', { flatten: true, flattenSeparator: '_' });
// returns { a: ..., b: ..., c_d: ..., c_e: ... }

flattenCustom

Sets a custom function for combining folder name and file name when concatenating. Defaults to undefined.

requireFolderTree('/path/to/folder', {
	flatten: true,
	flattenCustom: function(a, b) { return a + 'x' + b; }
});
// returns { a: ..., b: ..., cxd: ..., cxe: ... }

indexFile

If set, looks for the named file in each folder, and if found uses it as a base for the returned object. Defaults to undefined.

// if index.js contains code `module.exports = { x: 1 }`:

requireFolderTree('/path/to/folder');
// returns { a: ..., b: ..., c: { d: ..., e: ... }, index: { x: 1 } }

requireFolderTree('/path/to/folder', { indexFile: 'index.js' });
// returns { x: 1, a: ..., b: ..., c: { d: ..., e: ... } }

foldersKey

If set, puts all retrieved sub-folders inside the key specified. Defaults to undefined.

requireFolderTree('/path/to/folder', { foldersKey: 'folders' });
// returns { a: ..., b: ..., folders: { c: { d: ..., e: ... } } }

filesKey

If set, puts all retrieved files inside the key specified. Defaults to undefined.

requireFolderTree('/path/to/folder', { filesKey: 'files' });
// returns { files: { a: ..., b: ... }, c: { files: { d: ..., e: ... } } }

Other usage notes

This module will never alter the contents of the require cache. i.e. if you require() any of the files again, they will not have been altered by e.g. using fileNameAttribute option.

Tests

Use npm test to run the tests. Use npm run cover to check coverage.

Changelog

See changelog.md

Issues

If you discover a bug, please raise an issue on Github. https://github.com/overlookmotel/require-folder-tree/issues

Contribution

Pull requests are very welcome. Please:

  • ensure all tests pass before submitting PR
  • add an entry to changelog
  • add tests for new features
  • document new functionality/API additions in README