1.1.7 • Published 6 years ago

lz-mod-manager v1.1.7

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

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

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago