lmm

lz-mod-manager

JSON module manager/loader for node.js npm packages

Showing:

Popularity

Downloads/wk

14

Maintenance

No Maintenance Data Available

Package

Dependencies

0

Size (min+gzip)

1.1KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Module Manager

Module Manager reads a JSON file and loads your Node.js modules into a single variable

Use

The module manager will create a folder called mod-loader in your projects root directory (same folder as node_modules). Inside are two files, mm-modules.json and schema.json

schema.json is only a reference to formatting mm-modules.json correctly.

mm-modules.json is read by Module Manager and loads the corresponding modules in order.

Configuring the JSON file

schema.json:

{
    "<module_var>": {
        "pkg": "<module-pkg-name>",
        "args": [],
        "data": [],
        "init": [],
        "last": []
    }
}

Property Explanation

  • <module_var>: The name of the variable used to access the module
var <module_var> = require('some-package')
  • pkg: The module name used for require
require('pkg')
  • args: Arguments to be appeneded to require
require('pkg')(args[0]).args[1]()...(args[n])
  • data: Optional data to be passed with constructor

  • init: JavaScript to be ran after this module has been loaded

  • last: JavaScript to be ran after all modules have been loaded. Executes in bottom-up order.

Accessors

Accessors are keywords used in the init (and last, but only mods) property of the JSON file to make changes to and read from objects that have been created.

  • mods: Access to modules that have been loaded (i.e. modules higher up the list) and ability to create new variables here. The module returns this object on completion as the result of the require().

  • data: Access to the data variable (if it exists) of the module being loaded.

  • pkg: The result of the require() function for the module being loaded.

Modifiers

Modifiers change the way the data is read from the JSON file.

  • &: Do not try to evalue JavaScript for this field.

Example mm-modules.json

{
    "express": {
        "pkg": "express",
        "init": [
            "mods.app = pkg();"
        ],
        "last": [
            "mods.app.listen(8000, (err) => { err ? console.log(err) : console.log('Listening on port 8000') })"
        ]
    },
    "session": {
        "pkg": "express-session",
        "data": [
            {
            "name": "session",
            "secret": "&blowfish",
            "resave": "&true",
            "saveUninitialized": "&false",
            "cookie": {
                "domain": "localhost",
                "httpOnly": "true",
                "secure": "false"
            }
        }],
        "init": [
            "mods.seshOpts = data"
        ],
        "last": [
            "mods.app.use(pkg(mods.seshOpts));"
        ]
    },
    "mySQLStore": {
        "pkg": "express-mysql-session",
        "args": ["(mods.session)"],
        "data": [{
            "host": "192.168.1.200",
            "port": "3306",
            "user": "test",
            "password": "password",
            "database": "db"
        }],
        "init": [
            "mods.sqlStore = new pkg(data);",
            "mods.seshOpts.store = mods.sqlStore"
        ]
    },
    "bodyParser": {
        "pkg": "body-parser",
        "init": [
            "mods.app.use(pkg.urlencoded({extended: true}));"
        ]
    },
    "util": {
        "pkg": "util"
    },
    "pug": {
        "pkg": "pug",
        "init": [
            "mods.app.set('view engine', 'pug');",
            "mods.app.set('views', './views');"
        ]
    },
    "pm": {
        "pkg": "./pm",
        "args": [
            "(mods)"
        ]
    }
}

Result

If you included the module using the standard method:

const modManager = require('lz-mod-manager');

The variable modManager becomes an object whose child elements are the modules and any objects attached to the mods modifier through the init or last properties.

Another method for easier access to modules is to wrap the require() in a with() statement like so:

with(modManager = require('lz-mod-manager')){
    app.get('/' function (req, res){
        res.sendStatus(200);
    });
}

Now all modules can be access by the <module_var> given in the JSON file

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100