Created
February 27, 2020 11:54
-
-
Save gabrielsaints/b2e827ec8ec53ac513a5c6c9075dc934 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dotenv from "dotenv"; | |
import { readFileSync } from "fs"; | |
const compareKeys = (sample: object, config: object) => { | |
const configKeys = Object.keys(config); | |
return Object.keys(sample).every(key => configKeys.includes(key)); | |
}; | |
const loadEnv = () => { | |
const buffer = readFileSync(".env.example"); | |
const sample = dotenv.parse(buffer); | |
const config = dotenv.config(); | |
const match = compareKeys(sample, config.parsed || {}); | |
if (!match) { | |
// tslint:disable-next-line: no-console | |
console.error("Failed to load .env, .env.example and .env differs."); | |
process.exit(1); | |
} | |
return config; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment