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

bulk-require

v1.0.1

Published

require a whole directory of trees in bulk

Downloads

124,050

Readme

bulk-require

require a whole directory of trees in bulk

build status

example

var bulk = require('bulk-require');
var sections = bulk(__dirname, [ 'data/**/*.js', 'render/*.js' ]);
console.log(sections);

Running this glob.js file in a directory with these contents:

data/
  cats/
    index.js
    meow/
      x.js
  dogs/
    index.js
    small/
      yip.js
    wolf/
      doge.js
  owners/
    data.js
glob.js
render/
  xyz/
    ignored.js
  x.js

Gives this output:

{ data: 
   { cats: { [Function] index: [Circular], meow: [Object] },
     dogs: { [Function] index: [Circular], small: [Object], wolf: [Object] },
     owners: { data: [Object] } },
  render: { x: { oneoneone: 111, twotwotwo: 222 } } }

bound arguments

You can also bind arguments by passing in an array instead of a glob string.

For example, if all **/data.js files have exports that take a db argument, you can bind the arguments in bulk:

data/owners/data.js:

exports.all = function (db) {
    return db.createReadStream({ start: 'owner!', end: 'owner!\uffff' });
};

exports.one = function (db, name, cb) {
    db.get('owner!' + name, cb);
};

In this instance data.js has multiple individual exports but argument binding also works if you export a single function with module.exports= assignment.

Now you can just call sections.owners.data.one() without supplying the db:

var level = require('level');
var db = level('/tmp/test.db');
var bulk = require('bulk-require');

var sections = bulk(__dirname + '/data', [
    [ '**/data.js', db ],
    '**/*.js'
]);
sections.owners.data.one('mr-jenkins', console.log);
null '{"cats":5,"dogs":3}'

warning

For applications full of real-world trade-offs and messy business logic organized into model/ and view/ directories this approach to loading modules may be justified, but most of the time you should just use the regular kind of require.

What you should absolutely never do is run this module from somewhere deep in your codebase. It should be very close to the entry point of your application.

Sometimes it's OK to break the rules. Especially if you can get away with it. Caveat npmtor.

methods

var bulk = require('bulk-require')

var modules = bulk(basedir, globs, opts={})

Return a nested object modules by expanding the string or array of strings globs rooted at the directory basedir.

Each file will be placed into the nested tree modules based on its filename with respect to basedir. Each directory becomes a new nested object.

For each item in the globs array, if the item itself is an array, the glob string will be treated as the first argument and the extra arguments will be bound to all the top-level function exports for files matching the glob pattern.

If there is an index.js module that exports a single function with module.exports=, all the sub-modules will be attached to the index reference and it will serve as the parent node at that tree level.

You can optionally pass in a custom require function with opts.require.

install

With npm do:

npm install bulk-require

license

MIT