2017-02-27 18:53:04 +03:00
|
|
|
var settingsCache = require('../settings/cache'),
|
2017-03-23 22:00:58 +03:00
|
|
|
_ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
2017-04-04 19:07:35 +03:00
|
|
|
SafeString = require('../themes/engine').SafeString,
|
2017-03-23 22:00:58 +03:00
|
|
|
errors = require('../errors'),
|
|
|
|
logging = require('../logging'),
|
|
|
|
i18n = require('../i18n'),
|
|
|
|
labs = module.exports = {};
|
2015-11-03 22:17:24 +03:00
|
|
|
|
2017-03-23 22:00:58 +03:00
|
|
|
labs.isSet = function isSet(flag) {
|
2017-02-03 16:15:11 +03:00
|
|
|
var labsConfig = settingsCache.get('labs');
|
2015-12-01 19:13:05 +03:00
|
|
|
return labsConfig && labsConfig[flag] && labsConfig[flag] === true;
|
2015-11-03 22:17:24 +03:00
|
|
|
};
|
|
|
|
|
2017-03-23 22:00:58 +03:00
|
|
|
labs.enabledHelper = function enabledHelper(options, callback) {
|
|
|
|
var errDetails, errString;
|
|
|
|
|
|
|
|
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
|
|
|
|
errDetails = {
|
|
|
|
message: i18n.t('warnings.helpers.helperNotAvailable', {helperName: options.helperName}),
|
|
|
|
context: i18n.t('warnings.helpers.flagMustBeEnabled', {
|
|
|
|
helperName: options.helperName,
|
|
|
|
flagName: options.flagName
|
|
|
|
}),
|
|
|
|
help: i18n.t('warnings.helpers.seeLink', {url: options.helpUrl})
|
|
|
|
};
|
|
|
|
|
2017-08-15 11:50:36 +03:00
|
|
|
logging.error(new errors.DisabledFeatureError(errDetails));
|
2017-03-23 22:00:58 +03:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
errString = new SafeString(
|
2017-03-23 22:00:58 +03:00
|
|
|
'<script>console.error("' + _.values(errDetails).join(' ') + '");</script>'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (options.async) {
|
|
|
|
return Promise.resolve(function asyncError() {
|
|
|
|
return errString;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return errString;
|
|
|
|
};
|