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-smart

v1.1.2

Published

The smart way of requiring many files

Downloads

1,415

Readme

Require Smart

Build Status Dependency Status

The smart way of organizing your app's files and folders, by requiring recursivelly and automagicly parsing file names as their scope key.

Installation

$ npm install require-smart

Why

This library was created for three main reasons:

  1. Avoid requiring an obvious object tree within each file
  2. Encourage files with minimum number of business logic
  3. Allow filenames to be more clear on what they do

Usage

const RequireSmart = require('require-smart')

const myModules = RequireSmart('./myModulesFolder')

You might have this folder structure:

controllers
├─ users.js
├─ users.login.js
├─ users.create.js
├─ users.delete.js
├─ users.update.js
├─ queue.opts.default.js
└─ some_other.arbitrary-name.thing.js

Or maybe this folder structure:

controllers
├─┬ users/
│ ├─ index.js
│ ├─ login.js
│ ├─ create.js
│ ├─ delete.js
│ └─ update.js
├─┬ queue/
│ └─ opts.default.js
└─┬ some_other/
  └─┬ arbitrary-name/
    └─ thing.js

Or even an merged style:

controllers
├─┬ users/
│ ├─ index.js
│ ├─ login.js
│ ├─ create.js
│ ├─ delete.js
│ └─ update.js
├─ queue.opts.default.js
└─┬ some_other.arbitrary-name/
  └─ thing.js

You will get an object required like this:

{
  users: {
    ... require('./users/index'), [ Gets Merged ]
    login: require('./users/login'),
    create: require('./users/create'),
    delete: require('./users/delete'),
    update: require('./users/update')
  },

  queue: {
    opts: {
      default: require('./queue.opts/default')
    }
  },

  someOther: {
    arbitraryName: require('./some_other.arbitrary-name/thing')
  }
}

Options

You can customize the way RequireSmart loads your dependencies.

  • depth: [Default: true]

    If set to false, will not load folders recursivelly. If set to a Number, Will load until that depth. Set to true for infinite recursion on require

  • canMerge: [Default: true]

    If set to false, will throw exception if a object merge is needed

  • indexNames: [Default: ['index']]

    Set the array of name without extensions of 'file indexes' (usually index only)

  • extensions: [Defaults: ['.js', '.json']]

    Sets the valid extensions to be required

  • fileNameSeparator: [Defaults: .]

    What token to use as separator of file names.

  • uppercaseTokens: [Defaults: /[_\s-]]\w/g]

    A regex that maches the separation, and the first character of the next word

Know what is happening

If you desire to view what are the steps to be performed during the require, you can use the method view:

RequireSmart.view(__dirname + '/myModulesPath')

It will print out for each one of the files, the destination hash on the object, and also if it will get merged or set. npm install chalk to view output with colors.

view command

Discouragements

Use this library as close to the top of your application. This is something really usefull for loading controllers, helpers, models, modules, wales and cats. However, you should use this with care.

If you are building a Library or maybe something that the user (and not you) might have controll, be aware of the power you might be giving him. Think about security. Again.

Creator

Ivan Seidel