2016-05-19 14:49:22 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
SchedulingBase = require(__dirname + '/SchedulingBase'),
|
|
|
|
errors = require(__dirname + '/../errors');
|
|
|
|
|
|
|
|
exports.createAdapter = function (options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var adapter = null,
|
|
|
|
activeAdapter = options.active,
|
2016-09-13 23:24:57 +03:00
|
|
|
internalPath = options.internalPath,
|
|
|
|
contentPath = options.contentPath;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
if (!activeAdapter) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'Please provide an active adapter.'}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CASE: active adapter is a npm module
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
adapter = new (require(activeAdapter))(options);
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({err: err}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CASE: active adapter is located in specific ghost path
|
|
|
|
*/
|
|
|
|
try {
|
2016-09-13 23:24:57 +03:00
|
|
|
adapter = adapter || new (require(contentPath + activeAdapter))(options);
|
2016-05-19 14:49:22 +03:00
|
|
|
} catch (err) {
|
2016-09-13 23:24:57 +03:00
|
|
|
// CASE: only throw error if module does exist
|
|
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({err: err}));
|
2016-09-13 23:24:57 +03:00
|
|
|
}
|
|
|
|
// CASE: if module not found it can be an error within the adapter (cannot find bluebird for example)
|
|
|
|
else if (err.code === 'MODULE_NOT_FOUND' && err.message.indexOf(contentPath + activeAdapter) === -1) {
|
2017-01-16 23:49:41 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({err: err, help: 'Please check the imports are valid in ' + contentPath + activeAdapter}));
|
2016-09-13 23:24:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CASE: active adapter is located in internal ghost path
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
adapter = adapter || new (require(internalPath + activeAdapter))(options);
|
|
|
|
} catch (err) {
|
|
|
|
// CASE: only throw error if module does exist
|
2016-05-19 14:49:22 +03:00
|
|
|
if (err.code === 'MODULE_NOT_FOUND') {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'We cannot find your adapter in: ' + contentPath + ' or: ' + internalPath}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({err: err}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(adapter instanceof SchedulingBase)) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'Your adapter does not inherit from the SchedulingBase.'}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!adapter.requiredFns) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'Your adapter does not provide the minimum required functions.'}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.xor(adapter.requiredFns, Object.keys(_.pick(Object.getPrototypeOf(adapter), adapter.requiredFns))).length) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError({message: 'Your adapter does not provide the minimum required functions.'}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(adapter);
|
|
|
|
};
|