Skip to content

Instantly share code, notes, and snippets.

@lucasltv
Created March 2, 2020 02:58
Show Gist options
  • Save lucasltv/16a0e105f78d70a62ae46915eba78a31 to your computer and use it in GitHub Desktop.
Save lucasltv/16a0e105f78d70a62ae46915eba78a31 to your computer and use it in GitHub Desktop.
Function to validate if all of env keys are present in your .env file (use dotenv dependency for parse data).
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
const envSampleFilename = ".env.example"; //Create your own .env.example at root with dummy data and commit this file.
const envSample = fs.readFileSync(path.join(process.cwd(), envSampleFilename)).toString();
const buf = Buffer.from(envSample)
const sampleConfig = dotenv.parse(buf); // will return an object
const envsToValidade = Object.keys(sampleConfig);
module.exports = {
validateAll: () => {
envsToValidade.map(key => {
if (!process.env[key]) {
console.error(`ENV KEY '${key}' NOT FOUND! Check your .env file.`);
process.exit(1);
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment