A simple way to manage your dependencies and use "require" with no problems with relative paths. It is very useful to complex project structures and when it is needed to change some file from it's place.
[FEATURE] Migration to ES6 sintax!
[FEATURE] Refactoring (finally I had time to)!
[FEATURE] Unit tests!
[FEATURE] Add example folder
[FIX] Some fixes in Readme
EDL requires Node.js v6+ to run.
$ npm install --save easy-dependency-loader
Make sure you have a dependencies list in the following format:
{
...
"dependency1": "path/to/dependency1",
"dependency2": "path/to/dependency2",
"dependency3": "path/to/dependency3",
...
}
It can be described in a JSON file or in a object. The EDL works exactly in the same way of require, so, every load that works with require also works with EDL.
EDL.setDependencies(dependencies) //As an object descriptor
EDL.setDependencies('./path/from/project/root/to/dependencies.json') //Path to dependencies file
const myDependencies = require('./path/to/dependencies.json')
EDL.setDependencies(myDependencies) //Using an JSON module with require
//Then, you just need to call the name of module using the load() method. Is no necessary to use the relative path.
const Module = EDL.load('moduleName')
Module.doSomething()
const EDL = require('easy-dependency-loader')
//Passing a JSON path
EDL.setDependencies('./path/to/jsonfile/dependencies.json')
//Or passing a JSON module with require
const myDependencies = require('./configs/confgis.json')
EDL.setDependencies(myDependencies)
//Or passing an object
EDL.setDependencies({
"Dog": "./models/Dog.js",
"Falcon": "./models/birds/Falcon.js",
"Chicken": "./models/birds/Chicken.js",
"Worm": "./models/birds/insects/Worm.js"
})
//Finally, you can call the module in other place of project
const EDL = require('easy-dependency-loader')
const Dog = EDL.load('Dog')
Dog.bark()
//Lets suppose that async module is already installed
const EDL = require('easy-depenency-loader')
const async = EDL.load('async') // the same of const async = require('async');
const os = EDL.load('os') // the same of const os = require('os');
async.eachOf(os.cpus(), (cpu, key, next) => {
console.log('CPU [%s] PROPS: %j', key, cpu)
return next()
})
/*
Logs something like
CPU [0] PROPS: {"model":"Intel(R) Core(TM) ... }}
CPU [1] PROPS: {"model":"Intel(R) Core(TM) ... }}
...
*/
Pull requests are welcome!
In general, problems with using this module maybe are problems with require, such:
- Wrong relative path
- Non-requireble file (empty, not exported, not supported extension, wrong path, wrong file name...);
- "Cyclic" requires (module1 requires module2, that requires module1).
- Using other types of config files, such xml (does make sense?)
- Feature sugestions also are welcome
MIT