README
PROJECT DEPRECATED/ABANDONED
indexgen
Statically generates index.js for every directory in a node.js project, so you don't have to add them manually in order to be able to load them with a single 'require'.
It is an alternative tool to dynamic module loaders, which force you to use other functions than the standard require
to load your modules, additionally freeing some resources :)
Install
sudo npm install -g indexgen
Use
Open a terminal, go to you project root directory and type/run indexgen-cli
.
To delete the generated index.js files, go to your project root directory and run indexgen -undo
. Keep reading for more information about this command.
Purpose
The main purpose of this tool, is to load all of your modules in a given directory by doing a simple require as described below and then get the modules in that directory grouped in a single variable. It is a common practice in node.js world to group modules in a single variable (as you can see below) and to do that, you have to write an index.js file in every directory you have modules to load. This tool automates the index.js writing process. Grouping variables avoids requiring every module one by one.
Lets suppose you have the following project directory structure:
- yourProject
-- routes
---- auth.js
---- home.js
If you need to load auth.js and home.js in your main module, one way is to require them individually as follows:
var auth = require('./routes/auth.js');
var home = require('./routes/home.js');
//... and so on
If you wish to load them all by once doing routes = require('./routes')
, you would have to create an index.js
in ./routes
directory like this (the following code is exactly what indexgen
produces):
yourProject/routes/index.js:
module.exports.auth = require('./auth.js');
module.exports.home = require('./home.js');
The next code shows how to reference auth.js and home.js via a single require pointing to './routes' folder.
var routes = require('./routes');
// routes
app.all('/api/*', routes.auth.authenticate);
app.get('/', routes.auth.authenticate, routes.home.index); // as you can see you can access routes.home and routes.auth via required routes module.
// authentication
app.get('/login', routes.auth.login);
app.get('/logout', routes.auth.logout);
About undo parameter
indexgen
supports undo
since version 0.2, which allows you to undo the index.js
generated by this tool, but have in mind that it will also remove every index.js
you could have added manually, so be careful with it, you must be sure you want to delete every index.js
in your proyect since indexgen
is not "smart" enough to know what files were generated by itself or by you (this could be a feature to implement in the future).
To perform an undo, you have to provide the -undo
parameter to indexgen
command as follows:
indexgen -undo
# Remember, be careful with it! It will remove every index.js it finds.
Additionally, indexgen
has the ability to parse a file with directories to be ignore directories where you don't want to create index.json files. Examples of directories to be discarded in a normal project are node_modules
o .git
.
A simple igignore.json
file example could be as simple as the next line:
[
"public", // Excludes ./public directory
"tests", // Excludes ./tests directory
"db", // Excludes ./db directory
"routes/auth" // Excludes only the directory auth under routes ./routes/auth
]
Caveats: Every index.js
file you could have created manually will be overwritten (or deleted in case you run indexgen
with -undo parameter), so be careful, use it at your own risk. I would recommend you to run this tool in a toy/test project in order to make sure it works as you expect.
Roadmap
- Grunt plugin.
- Add support for a more expressive
igignore.json
format. It would be nice to fully support the json format so you can what directories to exclude in a more natural way.
Suggestions
Please leave me any suggestion you may have in this project github repo. They are very welcome!