2018-11-09 14:42:21 +03:00
|
|
|
const settingsCache = require('./settings/cache');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
2019-06-19 12:30:28 +03:00
|
|
|
const SafeString = require('../../frontend/services/themes/engine').SafeString;
|
2018-11-09 14:42:21 +03:00
|
|
|
const common = require('../lib/common');
|
2019-11-06 10:42:39 +03:00
|
|
|
const deprecatedFeatures = ['subscribers', 'publicAPI'];
|
|
|
|
|
2019-11-06 10:53:53 +03:00
|
|
|
module.exports.getAll = () => {
|
2019-11-06 10:42:39 +03:00
|
|
|
let labs = _.cloneDeep(settingsCache.get('labs')) || {};
|
|
|
|
// Remove old labs flags that should always be false now
|
|
|
|
deprecatedFeatures.forEach((feature) => {
|
|
|
|
delete labs[feature];
|
|
|
|
});
|
|
|
|
|
|
|
|
return labs;
|
2015-11-03 22:17:24 +03:00
|
|
|
};
|
|
|
|
|
2019-11-06 10:53:53 +03:00
|
|
|
module.exports.isSet = function isSet(flag) {
|
|
|
|
const labsConfig = module.exports.getAll();
|
2019-11-06 10:42:39 +03:00
|
|
|
|
|
|
|
return !!(labsConfig && labsConfig[flag] && labsConfig[flag] === true);
|
2019-02-24 20:37:11 +03:00
|
|
|
};
|
|
|
|
|
2019-11-06 10:53:53 +03:00
|
|
|
module.exports.enabledHelper = function enabledHelper(options, callback) {
|
2019-02-04 20:58:35 +03:00
|
|
|
const errDetails = {};
|
|
|
|
let errString;
|
2017-03-23 22:00:58 +03:00
|
|
|
|
2019-11-06 10:53:53 +03:00
|
|
|
if (module.exports.isSet(options.flagKey) === true) {
|
2017-03-23 22:00:58 +03:00
|
|
|
// helper is active, use the callback
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Else, the helper is not active and we need to handle this as an error
|
2019-02-04 20:58:35 +03:00
|
|
|
errDetails.message = common.i18n.t(options.errMessagePath || 'warnings.helpers.helperNotAvailable', {helperName: options.helperName}),
|
|
|
|
errDetails.context = common.i18n.t(options.errContextPath || 'warnings.helpers.flagMustBeEnabled', {
|
|
|
|
helperName: options.helperName,
|
|
|
|
flagName: options.flagName
|
|
|
|
});
|
|
|
|
errDetails.help = common.i18n.t(options.errHelpPath || 'warnings.helpers.seeLink', {url: options.helpUrl});
|
2017-03-23 22:00:58 +03:00
|
|
|
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.error(new common.errors.DisabledFeatureError(errDetails));
|
2017-03-23 22:00:58 +03:00
|
|
|
|
2019-07-15 09:18:58 +03:00
|
|
|
errString = new SafeString(`<script>console.error("${_.values(errDetails).join(' ')}");</script>`);
|
2017-03-23 22:00:58 +03:00
|
|
|
|
|
|
|
if (options.async) {
|
2019-07-15 09:18:58 +03:00
|
|
|
return Promise.resolve(errString);
|
2017-03-23 22:00:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return errString;
|
|
|
|
};
|