2022-03-06 16:40:15 +07:00
|
|
|
import * as path from "path";
|
|
|
|
|
import * as dotenv from "dotenv";
|
|
|
|
|
|
|
|
|
|
import Logger from "../libs/Logger";
|
|
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
const requiredENV = ["NODE_ENV"];
|
2022-03-06 16:40:15 +07:00
|
|
|
|
|
|
|
|
class Environment {
|
2026-07-01 12:36:05 +07:00
|
|
|
public init(): void {
|
|
|
|
|
dotenv.config({ path: path.resolve(__dirname, "../../.env") });
|
2022-03-06 16:40:15 +07:00
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
for (let param of requiredENV) {
|
|
|
|
|
if (this.isUndefinedOrEmpty(process.env[param]))
|
|
|
|
|
throw new Error(`.env ${param} is undefined`);
|
2022-03-06 16:40:15 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
// NODE_ENV Checks
|
|
|
|
|
if (this.get().NODE_ENV != "production" && this.get().NODE_ENV != "development")
|
|
|
|
|
throw new Error('.env NODE_ENV must be either "production" or "development"');
|
2022-03-06 16:40:15 +07:00
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
Logger.log("info", `Running in ${process.env.NODE_ENV} environment`);
|
|
|
|
|
}
|
2022-03-06 16:40:15 +07:00
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
public get(): any {
|
|
|
|
|
const NODE_ENV = process.env.NODE_ENV;
|
2022-03-06 16:40:15 +07:00
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
return {
|
|
|
|
|
NODE_ENV,
|
|
|
|
|
};
|
|
|
|
|
}
|
2022-03-06 16:40:15 +07:00
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
private isUndefinedOrEmpty(value: String | undefined): boolean {
|
|
|
|
|
if (typeof value === "undefined") return true;
|
2022-03-06 16:40:15 +07:00
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
if (value === undefined) return true;
|
2022-03-06 16:40:15 +07:00
|
|
|
|
2026-07-01 12:36:05 +07:00
|
|
|
if (value === "") return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-03-06 16:40:15 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default new Environment();
|