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