2020-06-13 10:51:26 +00:00
|
|
|
function isTestEnvironment() {
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
return getConfig('NODE_ENV', { warn: false }) === 'test';
|
|
|
|
}
|
|
|
|
|
2020-06-08 09:46:38 +00:00
|
|
|
/**
|
|
|
|
* Returns config item from environment
|
|
|
|
*/
|
2020-06-13 10:51:26 +00:00
|
|
|
function getConfig(key, options = { warn: !isTestEnvironment() }) {
|
2020-06-08 09:46:38 +00:00
|
|
|
if (key == null) {
|
|
|
|
throw new Error('"key" must be provided to getConfig()');
|
|
|
|
}
|
|
|
|
|
2020-06-13 10:51:26 +00:00
|
|
|
const env = (typeof global !== 'undefined' ? global : window)?.process?.env || {};
|
|
|
|
const value = env[key];
|
2020-06-08 09:46:38 +00:00
|
|
|
|
2020-06-13 10:51:26 +00:00
|
|
|
if (value == null && options?.warn !== false) {
|
2020-06-08 09:46:38 +00:00
|
|
|
console.warn(`getConfig("${key}") returned null`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
2020-06-13 10:51:26 +00:00
|
|
|
|
|
|
|
export default getConfig;
|