@studiowebux/loader v1.2.3
Webux Loader
This module allow to load the configurations of the application quickly and easily.
Installation
npm i --save @studiowebux/loader
Usage
this module load all .js files present in a given directory,
Example:
The directory containing all the configuration files is : ./config, this parameter is mandatory.
The logger is a custom function and it's optional, if this parameter isn't provide, it will output using console.log() by default.
Tree view :
config
language.js
logger.js
server.js
...
index.js
./config/*.js
module.exports = {
option:value,
...
}
index.js
const webuxLoader = require('@studiowebux/loader');
const path = require('path');
const modules = webuxLoader(path.join(__dirname, './config'));
console.log(modules);
OUTPUT:
[ language: { availables: [ 'fr', 'en' ],
directory: 'locales',
default: 'en',
autoReload: true,
syncFiles: true },
logger: { application_id: 'Test01',
forceConsole: true,
logstash: { host: '127.0.0.1', port: '5000' },
filenames:
{ error: 'log/error.log',
warn: 'log/warn.log',
info: 'log/info.log',
verbose: 'log/verbose.log',
debug: 'log/debug.log',
silly: 'log/silly.log' },
blacklist: [ 'password' ] },
security: { morgan: { type: 'json' },
origin: '*',
bodyParser: { limit: '10mb', extended: true },
cookieParser: { secret: 'SHUUUT!' } },
server: { ssl: { enabled: false, key: '', crt: '' },
enterprise: 'Studio Webux S.E.N.C',
author: 'Tommy Gingras',
project: 'Webuxjs',
version: '1.0.0',
port: 1337 } ]
Specifications
if you have configuration that are required in another configuration files, you can use the 00_filename.js file naming convention to priorize which configuration will be loaded in the object first.
for example:
config
00_module.js
withDeps.js
...
index.js
it will load 00_module, then withDeps file.
In case that you did not provide the **_ the files will be sort alphabetically.
you also have to pass the configuration object to update it lives.
index.js
const loader = require("@studiowebux/loader");
let instance = {
config: {}
};
loader(path.join(__dirname, "./config"), instance.config, console);
That way the object : instance.config.withDeps
will be set to the value of instance.config.module
. otherwise, it can be set to undefined.
After that you can test the result by doing : console.log(instance.config.withDeps.dependance());
For more details, check the test1.js file in the examples/ folder.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
SEE LICENSE IN license.txt
5 years ago