Customizing Module Installation

Besides auto-installing modules with dependencies and life cycle specified inmeta.js, Robo Installer also gives you ability to customize installation of any particular module. To manually install a module, specify functioninstallin itsmeta.js.

// meta.js
module.exports = {
    install: function(container, name, path) {
        // your custom installation logic here.
    }
}

The functioninstallaccepts following parameters:

  • container: The instance of Robo-Container used to bind module to.

  • name: Name of the module recognized by the Module Installer, e.g 'config', 'config.development'.

  • path: Path to the directory that contains the module.

Once specified, only two optionsinstallandonwill take effects and all other includinguse,setandsingletonwill be skipped.

Let's say yourpricingmodule has more than one payload files:gateway.jsandconnector.jsand you want to install them all at once:

app
|- index.js
|- modules
|  |- pricing
|  |  |- meta.js
|  |  |- gateway.js
|  |  |- connector.js

Your installation will look like:

// meta.js
module.exports = {
    install: function($, name, path) {
        $.bind(`${name}.connector`).to(`${path}/connector.js`); // $('pricing.connector') will return a connector.
        $.bind(name).to(`${path}/gateway.js`).use(`${name}.connector`); // $('pricing') will return a gateway.        
    }

    on: {
        installed: ($, name, path) => {
            // do some post-install checks here.
        }
    }
}

You can also use custom installation to install anynode_modulesto the container. For example:

app
|- index.js
|- modules
|  |- core
|  |  |- meta.js

Installation statements:

// meta.js
module.exports = {
    install: function ($, name, path) {
        $.bind('express').to(() => require('express')).asSingleton();
        $.bind('app').to((express) => {
            var app = express();
            var bodyParser = require('body-parser');
            app.use(bodyParser.json());
            app.use(bodyParser.urlencoded({ extended: true }));
            return app;
        }).use('express').asSingleton();
        $.bind('http').to((app) => require('http').Server(app)).use('app').asSingleton();        
    }
}

$('core')will throw anErrorin this case as it's not bounded to container.

Do not install any utility library as module, e.g ImmutableJS. Otherwise you will make your application structure unnecessarily complex and you will also loose IDE intelli-sense supports as well.

Last updated