kued
Version:
Extensions for the Kue library (Daemonization, Checkpointing, etc.)
51 lines (34 loc) • 1.27 kB
JavaScript
;
const _ = require('lodash');
const assert = require('assert');
const Path = require('path');
module.exports.decode = (configString) => {
return JSON.parse((new Buffer(configString, 'base64')).toString('utf8'));
};
module.exports.loadModule = (module) => {
if (!module) return null;
// Used for testing by allowing implementations to be passed instead of paths.
if (_.isFunction(module) || _.isObject(module)) return module;
if (module.startsWith('kued/')){
module = Path.resolve(__dirname, '../', module.slice(5));
}
else {
module = Path.resolve(process.cwd(), module);
}
try {
return require(module);
}
catch (e){
return null;
}
};
module.exports.loadComponent = (dependencyManager, modulePath, options) => {
const Component = exports.loadModule(modulePath);
assert.ok(!!Component, `Could not instantiate module: ${modulePath}`);
const dependencies = Component.dependencies || [];
const services = dependencies.map((dependency) => dependencyManager.getInstance(dependency));
const constructorArgs = _.flatten([ 'ignore', options || {}, services]);
const component = new (Component.bind.apply(Component, constructorArgs));
if(component.init) component.init();
return component;
};