const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const dotenvExpand = require('dotenv-expand');

const NODE_ENV = process.env.NODE_ENV || 'test';
const base = path.resolve(__dirname, '..', '..', '.env');

const dotenvFiles = [
  `${base}.${NODE_ENV}.local`,
  NODE_ENV !== 'test' && `${base}.local`,
  `${base}.${NODE_ENV}`,
  base,
].filter(Boolean);

dotenvFiles.forEach((file) => {
  if (fs.existsSync(file)) {
    dotenvExpand.expand(dotenv.config({ path: file }));
  }
});
