2018-06-14 20:15:12 +03:00
|
|
|
const _ = require('lodash'),
|
2016-05-19 14:49:22 +03:00
|
|
|
Promise = require('bluebird'),
|
2017-12-11 22:27:09 +03:00
|
|
|
SchedulingBase = require('./SchedulingBase'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common'),
|
2017-08-31 10:12:44 +03:00
|
|
|
cache = {};
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
exports.createAdapter = function (options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2018-06-14 20:15:12 +03:00
|
|
|
let adapter = null;
|
|
|
|
const {active: activeAdapter, internalPath, contentPath} = options;
|
2016-05-19 14:49:22 +03:00
|
|
|
|
|
|
|
if (!activeAdapter) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({
|
|
|
|
message: 'Please provide an active adapter.'
|
|
|
|
}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
2017-08-31 10:12:44 +03:00
|
|
|
if (cache.hasOwnProperty(activeAdapter)) {
|
|
|
|
return cache[activeAdapter];
|
|
|
|
}
|
|
|
|
|
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') {
|
2018-06-14 20:15:12 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({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') {
|
2018-06-14 20:15:12 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({err}));
|
2017-12-12 00:47:46 +03:00
|
|
|
// CASE: if module not found it can be an error within the adapter (cannot find bluebird for example)
|
2017-11-01 16:44:54 +03:00
|
|
|
} else if (err.code === 'MODULE_NOT_FOUND' && err.message.indexOf(contentPath + activeAdapter) === -1) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({
|
2018-06-14 20:15:12 +03:00
|
|
|
err,
|
|
|
|
help: `Please check the imports are valid in ${contentPath}${activeAdapter}`
|
2017-12-12 00:47:46 +03:00
|
|
|
}));
|
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') {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({
|
2018-06-14 20:15:12 +03:00
|
|
|
message: `We cannot find your adapter in: ${contentPath} or: ${internalPath}`
|
2017-12-12 00:47:46 +03:00
|
|
|
}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 20:15:12 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({err}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(adapter instanceof SchedulingBase)) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({
|
|
|
|
message: 'Your adapter does not inherit from the SchedulingBase.'
|
|
|
|
}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!adapter.requiredFns) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.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) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.IncorrectUsageError({
|
|
|
|
message: 'Your adapter does not provide the minimum required functions.'
|
|
|
|
}));
|
2016-05-19 14:49:22 +03:00
|
|
|
}
|
|
|
|
|
2017-08-31 10:12:44 +03:00
|
|
|
cache[activeAdapter] = adapter;
|
|
|
|
|
2016-05-19 14:49:22 +03:00
|
|
|
return Promise.resolve(adapter);
|
|
|
|
};
|