"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readEnv = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const read_env_exception_1 = require("../../exceptions/read-env.exception");
const dotenv = require("dotenv");
const doesEnvFileExist = (envPath) => {
    return (0, fs_1.existsSync)(envPath);
};
const readEnv = (envPath = (0, path_1.join)(process.cwd(), '.env')) => __awaiter(void 0, void 0, void 0, function* () {
    let finalEnv = process.env;
    if (doesEnvFileExist(envPath)) {
        try {
            const envFileBuffer = yield fs_1.promises.readFile(envPath);
            const envFileContent = dotenv.parse(envFileBuffer);
            finalEnv = Object.assign(Object.assign({}, finalEnv), envFileContent);
        }
        catch (error) {
            throw new read_env_exception_1.ReadEnvException();
        }
    }
    return finalEnv;
});
exports.readEnv = readEnv;
