2018-11-09 17:12:21 +05:30
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
2021-04-19 15:09:35 +01:00
|
|
|
const SafeString = require('../../frontend/services/theme-engine/engine').SafeString;
|
2020-04-30 20:26:12 +01:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-05-03 17:29:44 +01:00
|
|
|
const i18n = require('../../shared/i18n');
|
2021-06-15 15:36:27 +01:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-04-19 18:28:51 +01:00
|
|
|
const settingsCache = require('../services/settings/cache');
|
2021-06-09 19:30:24 +04:00
|
|
|
const config = require('../../shared/config');
|
2019-11-06 14:42:39 +07:00
|
|
|
|
2021-06-04 19:56:16 +04:00
|
|
|
// NOTE: this allowlist is meant to be used to filter out any unexpected
|
|
|
|
// input for the "labs" setting value
|
2021-06-09 19:30:24 +04:00
|
|
|
const BETA_FEATURES = [
|
2021-06-07 20:22:06 +01:00
|
|
|
'activitypub',
|
2021-06-09 19:30:24 +04:00
|
|
|
'matchHelper'
|
|
|
|
];
|
|
|
|
|
|
|
|
const ALPHA_FEATURES = [
|
2021-06-10 20:35:56 +01:00
|
|
|
'multipleProducts',
|
2021-06-17 10:47:04 +01:00
|
|
|
'featureImageMeta',
|
|
|
|
'psmRedesign'
|
2021-06-04 19:56:16 +04:00
|
|
|
];
|
|
|
|
|
2021-06-09 19:30:24 +04:00
|
|
|
module.exports.WRITABLE_KEYS_ALLOWLIST = [...BETA_FEATURES, ...ALPHA_FEATURES];
|
2021-06-04 19:56:16 +04:00
|
|
|
|
2021-06-07 20:51:37 +04:00
|
|
|
module.exports.getAll = () => {
|
|
|
|
const labs = _.cloneDeep(settingsCache.get('labs')) || {};
|
|
|
|
|
2021-06-09 19:30:24 +04:00
|
|
|
ALPHA_FEATURES.forEach((alphaKey) => {
|
2021-06-10 12:54:34 +01:00
|
|
|
if (labs[alphaKey] && !(config.get('enableDeveloperExperiments') || process.env.NODE_ENV.match(/^testing/))) {
|
2021-06-09 19:30:24 +04:00
|
|
|
delete labs[alphaKey];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-07 20:51:37 +04:00
|
|
|
labs.members = settingsCache.get('members_signup_access') !== 'none';
|
|
|
|
|
|
|
|
return labs;
|
|
|
|
};
|
2015-11-03 19:17:24 +00:00
|
|
|
|
2021-06-18 16:03:58 +01:00
|
|
|
/**
|
|
|
|
* @param {string} flag
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2019-11-06 14:53:53 +07:00
|
|
|
module.exports.isSet = function isSet(flag) {
|
|
|
|
const labsConfig = module.exports.getAll();
|
2019-11-06 14:42:39 +07:00
|
|
|
|
|
|
|
return !!(labsConfig && labsConfig[flag] && labsConfig[flag] === true);
|
2019-02-24 18:37:11 +01:00
|
|
|
};
|
|
|
|
|
2019-11-06 14:53:53 +07:00
|
|
|
module.exports.enabledHelper = function enabledHelper(options, callback) {
|
2019-02-04 17:58:35 +00:00
|
|
|
const errDetails = {};
|
|
|
|
let errString;
|
2017-03-23 19:00:58 +00:00
|
|
|
|
2019-11-06 14:53:53 +07:00
|
|
|
if (module.exports.isSet(options.flagKey) === true) {
|
2017-03-23 19:00:58 +00: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 20:26:12 +01: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 17:58:35 +00:00
|
|
|
helperName: options.helperName,
|
|
|
|
flagName: options.flagName
|
|
|
|
});
|
2020-04-30 20:26:12 +01:00
|
|
|
errDetails.help = i18n.t(options.errHelpPath || 'warnings.helpers.seeLink', {url: options.helpUrl});
|
2017-03-23 19:00:58 +00:00
|
|
|
|
2020-04-30 20:26:12 +01:00
|
|
|
logging.error(new errors.DisabledFeatureError(errDetails));
|
2017-03-23 19:00:58 +00:00
|
|
|
|
2019-07-15 01:18:58 -05:00
|
|
|
errString = new SafeString(`<script>console.error("${_.values(errDetails).join(' ')}");</script>`);
|
2017-03-23 19:00:58 +00:00
|
|
|
|
|
|
|
if (options.async) {
|
2019-07-15 01:18:58 -05:00
|
|
|
return Promise.resolve(errString);
|
2017-03-23 19:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errString;
|
|
|
|
};
|