Skip to content

Instantly share code, notes, and snippets.

@gabrielsaints
Created February 27, 2020 11:54
Show Gist options
  • Save gabrielsaints/b2e827ec8ec53ac513a5c6c9075dc934 to your computer and use it in GitHub Desktop.
Save gabrielsaints/b2e827ec8ec53ac513a5c6c9075dc934 to your computer and use it in GitHub Desktop.
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