roc
Version:
Build modern web applications easily
107 lines (86 loc) • 3.27 kB
JavaScript
;
exports.__esModule = true;
exports.merge = merge;
exports.getConfig = getConfig;
exports.getSettings = getSettings;
exports.appendSettings = appendSettings;
exports.appendConfig = appendConfig;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
require('source-map-support/register');
var _deepExtend = require('deep-extend');
var _deepExtend2 = _interopRequireDefault(_deepExtend);
var _helpersStyle = require('../helpers/style');
/* Make sure that we only print some feedback once */
var onceSettings = true;
/* Using global variables here to make sure that we can access the values set from different projects.
* This guarantees that the variables will live outside the require cache, something that we need for stability.
*/
global.rocConfig = global.rocConfig || {};
/**
* Merges two configuration objects.
*
* @param {!Object} a - Configuration object to base the merge on.
* @param {!Object} b - Configuration object that is merged into the first, overwriting the first one.
*
* @returns {Object} - The merged configuration object
*/
function merge(a, b) {
return _deepExtend2['default']({}, a, b);
}
/**
* Gets the current configuration object.
*
* @returns {rocConfig} - The application configuration object.
*/
function getConfig() {
if (onceSettings && process.env.ROC_CONFIG_SETTINGS) {
onceSettings = false;
if (global.rocConfig.settings && Object.keys(global.rocConfig.settings).length > 0 && process.env.ROC_CONFIG_SETTINGS) {
console.log(_helpersStyle.warning('You have settings defined on the environment variable ROC_CONFIG_SETTINGS ' + 'and they will be appended to the settings. Will append the following:\n' + JSON.stringify(process.env.ROC_CONFIG_SETTINGS, null, 2)), '\n');
}
appendSettings(JSON.parse(process.env.ROC_CONFIG_SETTINGS));
}
return global.rocConfig;
}
/**
* Gets the settings in the configuration object.
*
* Will by default get all settings.
*
* @param {string} [key] - The settings key to fetch.
*
* @returns {rocSettings|Object} - The application settings object.
*/
function getSettings(key) {
var settings = getConfig().settings;
return key ? settings[key] : settings;
}
/**
* Appends settings to the configuration object.
*
* Will merge with the already existing settings object meaning that this function can be called multiple times and
* the settings will be a merge of all those calls.
*
* @param {!rocSettings} settingsObject - A settings object.
*
* @returns {rocSettings} - The settings object.
*/
function appendSettings(settingsObject) {
global.rocConfig = merge(getConfig(), { settings: settingsObject });
return getSettings();
}
/**
* Appends to the configuration object.
*
* Will merge with the already existing configuration object meaning that this function can be called multiple times and
* the configuration will be a merge of all those calls.
*
* @param {!rocConfig} configObject - A configuration object.
*
* @returns {rocConfig} - The configuration object.
*/
function appendConfig(configObject) {
global.rocConfig = merge(getConfig(), configObject);
return getConfig();
}
//# sourceMappingURL=index.js.map