Module Installation Checks
Module Installer provides a mechanism allowing you to perform checks on installation of any module, together with final check on installation of all modules from a given path.
To use the check, in module's metadata, specify an additional property namedon
whose value exposes three methods as in following snippet:
// meta.js
module.exports = {
...
on: {
installing: (container, name, path) => {
// will be called BEFORE installing module.
},
installed: (container, name, path) => {
// will be called right AFTER module installed.
},
allInstalled: (container, name, path) => {
// will be called right AFTER ALL modules installed.
}
}
}
In which, every method accepts the same following parameters:
container: The instance of 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.
Method Usage
installing
(optional)
installing
(optional)This method is called when the corresponding module is about to be installed. It is suitable for a pre-install check and MUST return a boolean indicating if the installer should install the module or not. If afalse
value returned, the module will not be installed.
If this method is not specified, pre-install check for the corresponding module will be skipped and the module will be installed by default.
// meta.js
module.exports = {
...
on: {
installing: ($, name, path) => {
var shouldInstall = doSomePreinstallCheck();
if (shouldInstall) {
console.log(`Module will be installed`);
}
else {
console.log(`Module will NOT be installed`);
}
return shouldInstall;
}
}
}
By returning afalse
, the module will be skipped from installation while the installer will continue installing the rests. To terminate the entire installation process, instead of returningfalse
, you throw anError
from inside this method. By doing this, you should handle error at application-level where the installer gets called:
// meta.js
module.exports = {
...
on: {
installing: ($, name, path) => {
var shouldInstall = doPreinstallCheck();
if (!shouldInstall)
throw new Error(`Pre-install Check for ${name} MUST be passed`);
return shouldInstall;
}
}
}
And at application-level:
try {
installer.install(__dirname + '/modules');
} catch (ex) {
// notify error and exit application here.
}
installed
(optional)
installed
(optional)This method is called right after the corresponding module installed. It is suitable for a post-install check and does not need to return any values.
Similar toinstalling
, you can throw an error from inside this method to terminate entire installation process.
// meta.js
module.exports = {
...
on: {
installed: ($, name, path) => {
var successfullyInstalled = doPostinstallCheck();
if (!successfullyInstalled)
throw new Error(`Module ${name} was improperly installed.`)
}
}
}
If this method is not specified, post-install check for the corresponding module will be skipped.
allInstalled
(optional)
allInstalled
(optional)This method is called after ALL modules installed. It's suitable for a final check and does not need to return any values.
Similar toinstalling
andinstalled
, you can throw an error from inside this function to terminate entire installation process.
// meta.js
module.exports = {
...
on: {
allInstalled: ($, name, path) => {
var finalCheckPassed = doFinalCheck();
if (!finalCheckPassed)
throw new Error(`Some modules were improperly installed.`)
}
}
}
Last updated