bit-bin
Version:
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b
98 lines (76 loc) • 2.85 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isFeatureEnabled = isFeatureEnabled;
exports.addFeature = addFeature;
exports.HARMONY_FEATURE = exports.LEGACY_SHARED_DIR_FEATURE = exports.ENV_VAR_FEATURE_TOGGLE = void 0;
function _defineProperty2() {
const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
_defineProperty2 = function () {
return data;
};
return data;
}
function _globalConfig() {
const data = require("./global-config");
_globalConfig = function () {
return data;
};
return data;
}
function _constants() {
const data = require("../../../constants");
_constants = function () {
return data;
};
return data;
}
/**
* provides an option to enable/disable experimental features before releasing them.
* two ways to enable a feature:
* 1) from the CLI. prefix your command with BIT_FEATURES and the features list separated by a comma.
* 2) from the config. run `bit config set features=<features-list>` and the features list separated by a comma.
* if the environment variable was provided, it'll skip the config.
* the results are cached so no penalty calling it multiple times in the same process.
*
* to use it in the code, simply call `isFeatureEnabled('your-feature')`
*
* for the e2e-tests, there is a mechanism built around it, to enable/disable features per file or
* per command. see the docs of CommandHelper class for more info.
*/
const ENV_VAR_FEATURE_TOGGLE = 'BIT_FEATURES';
exports.ENV_VAR_FEATURE_TOGGLE = ENV_VAR_FEATURE_TOGGLE;
class FeatureToggle {
constructor() {
(0, _defineProperty2().default)(this, "features", void 0);
}
areFeaturesPopulated() {
return this.features !== undefined;
}
setFeatures() {
if (this.areFeaturesPopulated()) return;
const enabledFeatures = process.env[ENV_VAR_FEATURE_TOGGLE] || (0, _globalConfig().getSync)(_constants().CFG_FEATURE_TOGGLE);
this.features = enabledFeatures ? enabledFeatures.split(',').map(f => f.trim()) : null;
}
isFeatureEnabled(featureName) {
this.setFeatures();
return this.features ? this.features.includes(featureName) : false;
}
addFeature(featureName) {
this.setFeatures();
if (this.features) this.features.push(featureName);else this.features = [featureName];
}
}
const featureToggle = new FeatureToggle();
function isFeatureEnabled(featureName) {
return featureToggle.isFeatureEnabled(featureName);
}
function addFeature(featureName) {
featureToggle.addFeature(featureName);
}
const LEGACY_SHARED_DIR_FEATURE = 'legacy-shared-dir';
exports.LEGACY_SHARED_DIR_FEATURE = LEGACY_SHARED_DIR_FEATURE;
const HARMONY_FEATURE = 'harmony';
exports.HARMONY_FEATURE = HARMONY_FEATURE;
;