dirquire
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 = 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> "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> "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-tokenjs: 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-modulejs: 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 = ; // Load the private routes not exposed var filters = extension: "*_task.js" depth: 1 ; // Setup each task var Tasks = ; Tasks;
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/.
- Fork it
- 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.
- 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.
- Push to the branch (
git push feature/issue-XYZ
) - Create new Pull Request