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-12-12 00:47:46 +03:00
|
|
|
common = require('../lib/common'),
|
2017-03-23 22:00:58 +03:00
|
|
|
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 = {
|
2017-12-12 00:47:46 +03:00
|
|
|
message: common.i18n.t('warnings.helpers.helperNotAvailable', {helperName: options.helperName}),
|
|
|
|
context: common.i18n.t('warnings.helpers.flagMustBeEnabled', {
|
2017-03-23 22:00:58 +03:00
|
|
|
helperName: options.helperName,
|
|
|
|
flagName: options.flagName
|
|
|
|
}),
|
2017-12-12 00:47:46 +03:00
|
|
|
help: common.i18n.t('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
|
|
|
|
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;
|
|
|
|
};
|