Created
March 2, 2020 02:58
-
-
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).
This file contains 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
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