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

dirquire

v1.0.0

Published

Load multiple modules from a given directory with filtering capabilities and exception handling decreasing the number of manually-loaded modules.

Downloads

6

Readme

dirquire

Travis npm version Codacy Badge Code Climate Dependency Status devDependency Status Coverage Status License

NPM

Features

Helps loading multiple modules from a given directory, avoiding multiple manual require statements in the module, allowing:

  • Interface-based loading: Load multiple modules from a given directory;
  • Filtering: Filtering which modules to load from a directory by file path;
  • API Result: Properly return the response as an API.

API

require("dirquire")(dir, [filters]): array

  • dir: a valid path to a directory according to fs.statSync(path).isDirectory().
  • filters.extension: optional parameter to filter file names.
  • filters.depth: loads files in a given depth.

Loads all the files of a given

  var loadedModules = require("dirquire")(dir, [filters]);

The result is an arry of the following api:

  [{
    fileName: "The name of the file without the path.",
    filePath: "The full path to the loaded file.",
    module: "The instance of the loaded module. If an error occurs, it is undefined",
    error: "The instance of the Error captured while loading the module."
  }];

Take a look at the fixtures directory.

Example: Load modules without errors

Using the node cli, you can run the following fixtures used by the test cases.

$ node
> require("dirquire")("fixtures/all-modules-correct")
[ { fileName: 'hello.js',
    filePath: '/home/mdesales/dev/github/marcellodesales/node-dirquire/fixtures/all-modules-correct/hello.js',
    module: 
     { endpoint: '/hello',
       contentType: 'text/plain',
       init: [Function: decorate] } },
  { fileName: 'secure.js',
    filePath: '/home/mdesales/dev/github/marcellodesales/node-dirquire/fixtures/all-modules-correct/secure.js',
    module: 
     { endpoint: '/secure',
       contentType: 'text/plain',
       init: [Function: decorate] } } ]

Example: Load modules with errors

  • Files with syntax errors are not loaded.
  • Files that requires a module that is not located in the node_modules.
$ node
> require("dirquire")("fixtures/modules-with-error")
[ { fileName: 'illegal-token.js',
    filePath: '/home/mdesales/dev/github/marcellodesales/node-dirquire/fixtures/modules-with-error/illegal-token.js',
    error: [Error: Cannot load the module /home/mdesales/dev/github/marcellodesales/node-dirquire/fixtures/modules-with-error/illegal-token.js: Unexpected token ILLEGAL] },
  { fileName: 'module-requiring-non-existent-module.js',
    filePath: '/home/mdesales/dev/github/marcellodesales/node-dirquire/fixtures/modules-with-error/module-requiring-non-existent-module.js',
    error: [Error: Cannot load the module /home/mdesales/dev/github/marcellodesales/node-dirquire/fixtures/modules-with-error/module-requiring-non-existent-module.js: Cannot find module 'passport-restify'] } ]

Use

Loading multiple modules with a given interface, without requiring all the modules from the given directory manually. Considering the directory is as follows:

$ tree tasks
./tasks/
├── checkdeps
│   └── checkdeps_tasks.js
├── doc
│   └── doc_tasks.js
├── test
│   └── test_tasks.js
├── todo
│   ├── todo_tasks.js
│   └── xml-todos-serializer.js
└── versioning
    └── versioning_tasks.js

The following example loads all the _tasks.js files, but not the xml-todos-serializer.js. The depth filter helps navigating to directories that contains more than modules required to be loaded.

  var loadModules = require("dirquire");

  // Load the private routes not exposed
  var filters = {
    extension: "*_task.js",
    depth: 1
  };

  // Setup each task
  var Tasks = dirquire(dir, filters);
  Tasks.forEach(function(Task) {
    // Report that the task was loaded...
    log.info("Verifying the task at " + Task.filePath);

    if (Task.error) {
      // Report the error for instance...
      log.error(Task.error.message);

    } else {
      // Execute the module
      new Task.module().setup();
    }
  });

The only observation is that all the returned objects must implement the same interface. In the case above, all the tasks are classes with the method setup(). That is a good application of the Visitor and Iterator Design-Patterns.

Contributing

We use the GitFlow branching mechanics, http://nvie.com/posts/a-successful-git-branching-model/.

  1. Fork it
  2. Create your feature branch (git checkout -b feature/issue-XYZ origin/master --track)
  • Adding Ids helps communicating where this feature is coming from.
  • You can also solve any open Issue in the issues tab.
  1. Commit your changes (git commit -am 'Issue #XYZ: Add some feature to fix #444')
  • Adding "fix #444" will trigger a link to the GitHub issue #444.
  1. Push to the branch (git push feature/issue-XYZ)
  2. Create new Pull Request