Skip to content

Instantly share code, notes, and snippets.

@qrg
Created August 2, 2020 08:33
Show Gist options
  • Save qrg/4f2f3af88bd81f20ea8e0f2801ed0e26 to your computer and use it in GitHub Desktop.
Save qrg/4f2f3af88bd81f20ea8e0f2801ed0e26 to your computer and use it in GitHub Desktop.
const { ENV_VAR_1, ENV_VAR_2, ENV_VAR_3 } = process.env;
const filterUndefinedVars = (vars: {
[variableName: string]: any;
}): string[] => {
return Object.entries(vars)
.filter(([_k, v]) => typeof v === 'undefined')
.map(([variableName]) => variableName);
};
const undefinedVars = filterUndefinedVars({
ENV_VAR_1,
ENV_VAR_2,
ENV_VAR_3
});
if (undefinedVars.length >= 1) {
undefinedVars.forEach((varName) => {
console.error(`An environment variable ${varName} is not set.`);
});
throw new Error('One or more required environment variables are missing.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment