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;
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-05-28 21:30:23 +03:00
|
|
|
const {i18n} = require('../lib/common');
|
|
|
|
const logging = require('../../shared/logging');
|
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
|
2020-04-30 22:26:12 +03:00
|
|
|
errDetails.message = i18n.t(options.errMessagePath || 'warnings.helpers.helperNotAvailable', {helperName: options.helperName}),
|
|
|
|
errDetails.context = i18n.t(options.errContextPath || 'warnings.helpers.flagMustBeEnabled', {
|
2019-02-04 20:58:35 +03:00
|
|
|
helperName: options.helperName,
|
|
|
|
flagName: options.flagName
|
|
|
|
});
|
2020-04-30 22:26:12 +03:00
|
|
|
errDetails.help = i18n.t(options.errHelpPath || 'warnings.helpers.seeLink', {url: options.helpUrl});
|
2017-03-23 22:00:58 +03:00
|
|
|
|
2020-04-30 22:26:12 +03:00
|
|
|
logging.error(new 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;
|
|
|
|
};
|