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');
|
|
|
|
const config = require('../config');
|
|
|
|
let labs = module.exports = {};
|
2015-11-03 22:17:24 +03:00
|
|
|
|
2017-03-23 22:00:58 +03:00
|
|
|
labs.isSet = function isSet(flag) {
|
2019-02-26 06:09:16 +03:00
|
|
|
var labsConfig = settingsCache.get('labs');
|
2018-11-09 14:42:21 +03:00
|
|
|
/**
|
|
|
|
* TODO: Uses hard-check for members prototype, removed here when added to settings
|
|
|
|
*/
|
2019-02-26 06:09:16 +03:00
|
|
|
if (flag === 'members') {
|
|
|
|
return config.get('enableDeveloperExperiments') && labsConfig && labsConfig[flag] && labsConfig[flag] === true;
|
2018-11-09 14:42:21 +03:00
|
|
|
}
|
2015-12-01 19:13:05 +03:00
|
|
|
return labsConfig && labsConfig[flag] && labsConfig[flag] === true;
|
2015-11-03 22:17:24 +03:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:37:11 +03:00
|
|
|
labs.getAll = () => {
|
|
|
|
return settingsCache.get('labs');
|
|
|
|
};
|
|
|
|
|
2017-03-23 22:00:58 +03:00
|
|
|
labs.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
|
|
|
|
|
|
|
if (labs.isSet(options.flagKey) === true) {
|
|
|
|
// 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;
|
|
|
|
};
|