Select Git revision
check_envs.js
Forked from
FS Info TU Dortmund / Fachschaftsrat / Tools / Protokoll-Pad-Generator
133 commits behind the upstream repository.

Jonas Zohren authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
check_envs.js 1.85 KiB
function isNonEmptyString(value) {
return typeof value === "string" && value.length > 0;
}
/**
* If targetEnv is empty/not set, set it to the first alias that has contains a value
* @param {string} targetEnv the name of the env that aliases should map to
* @param {string[]} aliases aliases for targetEnv
* @returns targetEnv's value or the first non empty alias value
*/
function aliasEnv(targetEnv, aliases) {
targetEnv = targetEnv.toUpperCase();
aliases = aliases.map((alias) => alias.toUpperCase());
if (
typeof process.env[targetEnv] === "string" &&
process.env[targetEnv].length > 0
) {
return process.env[targetEnv];
} else {
for (const alias of aliases) {
const value = process.env[alias];
if (typeof value === "string" && value.length > 0) {
process.env[targetEnv] = value;
return value;
}
}
}
// Nothing found, all empty
return undefined;
}
/**
* Check if all envs exist and print errors otherwise.
* @param {{name: string, aliases: string[], description: string, whereToGetIt: string?}[]} envsWithDescriptions
* @returns array of error messages
*/
function checkAllEnvsExist(envsWithDescriptions) {
let errorMessages = [];
for (const {
name,
aliases,
description,
whereToGetIt,
} of envsWithDescriptions) {
const value = aliasEnv(name, aliases);
if (!isNonEmptyString(value)) {
const aliasMessage =
aliases.length > 0 ? " (Aliases: " + aliases.join(", ") + ")" : "";
const whereToGetItText = whereToGetIt
? "\n└ You can request it here: " + whereToGetIt
: "";
const errrorMessage = `
Environment variable ${name}${aliasMessage} is not set.
└ Description: ${description}${whereToGetItText}
`;
errorMessages.push(errrorMessage);
}
}
return errorMessages;
}
module.exports.checkAllEnvsExist = checkAllEnvsExist;