mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-25 22:37:18 +10:00
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
/**
|
|
Implements .env file loading that mimicks the way create-react-app
|
|
does it. We want this to get consistent configuration handling
|
|
between client and node server.
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
|
|
const NODE_ENV = process.env.NODE_ENV;
|
|
|
|
if (!NODE_ENV) {
|
|
throw new Error('The NODE_ENV environment variable is required but was not specified.');
|
|
}
|
|
|
|
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
|
|
var dotenvFiles = [
|
|
`.env.${NODE_ENV}.local`,
|
|
`.env.${NODE_ENV}`,
|
|
// Don't include `.env.local` for `test` environment
|
|
// since normally you expect tests to produce the same
|
|
// results for everyone
|
|
NODE_ENV !== 'test' && `env.local`,
|
|
'.env',
|
|
].filter(Boolean);
|
|
|
|
const configureEnv = () => {
|
|
// Load environment variables from .env* files. Suppress warnings using silent
|
|
// if this file is missing. dotenv will never modify any environment variables
|
|
// that have already been set.
|
|
// https://github.com/motdotla/dotenv
|
|
dotenvFiles.forEach(dotenvFile => {
|
|
if (fs.existsSync(dotenvFile)) {
|
|
console.log('Loading env from file:' + dotenvFile);
|
|
require('dotenv-expand')(
|
|
require('dotenv').config({
|
|
path: dotenvFile,
|
|
})
|
|
);
|
|
}
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
configureEnv: configureEnv,
|
|
};
|