envalid
Version:
Validation for your environment variables
61 lines • 2.34 kB
JavaScript
import { EnvMissingError } from './errors.js';
// Lazy: importing envalid must not require console.error
const defaultLogger = (data, ...args) => {
console.error(data, ...args);
};
// Apply ANSI colors to the reporter output only if we detect that we're running in Node
const isNode = !!(typeof process === 'object' && process?.versions?.node);
const colorWith = (colorCode) => (str) => isNode ? `\x1b[${colorCode}m${str}\x1b[0m` : str;
const colors = {
blue: colorWith('34'),
white: colorWith('37'),
yellow: colorWith('33'),
};
const RULE = colors.white('================================');
// Takes the provided errors, formats them all to an output string, and passes that string output to the
// provided "logger" function.
//
// This is exposed in the public API so third-party reporters can leverage this logic if desired
export const envalidErrorFormatter = (errors, logger = defaultLogger) => {
const missingVarsOutput = [];
const invalidVarsOutput = [];
for (const [k, err] of Object.entries(errors)) {
if (err instanceof EnvMissingError) {
missingVarsOutput.push(` ${colors.blue(k)}: ${err.message || '(required)'}`);
}
else
invalidVarsOutput.push(` ${colors.blue(k)}: ${err?.message || '(invalid format)'}`);
}
// Prepend "header" output for each section of the output:
if (invalidVarsOutput.length) {
invalidVarsOutput.unshift(` ${colors.yellow('Invalid')} environment variables:`);
}
if (missingVarsOutput.length) {
missingVarsOutput.unshift(` ${colors.yellow('Missing')} environment variables:`);
}
const output = [
RULE,
invalidVarsOutput.sort().join('\n'),
missingVarsOutput.sort().join('\n'),
RULE,
]
.filter((x) => !!x)
.join('\n');
logger(output);
};
export const defaultReporter = ({ errors = {} }, { onError, logger } = { logger: defaultLogger }) => {
if (!Object.keys(errors).length)
return;
envalidErrorFormatter(errors, logger);
if (onError) {
onError(errors);
}
else if (isNode) {
logger(colors.yellow('\n Exiting with error code 1'));
process.exit(1);
}
else {
throw new TypeError('Environment validation failed');
}
};
//# sourceMappingURL=reporter.js.map