Skip to content

Instantly share code, notes, and snippets.

@akumbhani66
Created August 26, 2019 05:21
Show Gist options
  • Save akumbhani66/50df0e346fe61ab0af6418c855587183 to your computer and use it in GitHub Desktop.
Save akumbhani66/50df0e346fe61ab0af6418c855587183 to your computer and use it in GitHub Desktop.
A small script to check missing envs from application.
/**
* Tool to check all the environment files for the missing variables.
credit: Marko Markovich
*/
const fs = require('fs');
const dotenv = require('dotenv');
const glob = require('glob');
// eslint-disable-next-line import/no-extraneous-dependencies
const chalk = require('chalk');
const envFiles = glob.sync('.env*');
const maxLen = envFiles.reduce((acc, cur) => Math.max(acc, cur.length), 0);
const envs = envFiles.map(f => {
const config = dotenv.parse(fs.readFileSync(f));
const keys = Object.keys(config);
return { file: f, keys, config };
});
const allKeys = envs
.reduce((acc, cur) => acc.concat(cur.keys.filter(f => !acc.includes(f))), [])
.sort((a, b) => (a.toUpperCase() < b.toUpperCase() ? -1 : 1));
envs.forEach(env => {
console.log(
chalk.green.bold(`
Checking ${env.file} ... `),
);
allKeys.forEach(key => {
if (!env.keys.includes(key)) {
const foundIn = envs
.map(env2 =>
env2.keys.includes(key)
? `${env2.file.padEnd(maxLen, ' ')}: ${env2.config[key]}`
: false,
)
.filter(ff => ff);
console.log(`
${chalk.yellow(env.file)} is missing ${chalk.red(key)} found in`);
foundIn.forEach(found => console.log(` ${found}`));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment