UNPKG

envalid

Version:

Validation for your environment variables

63 lines 2.48 kB
"use strict"; var _a; exports.__esModule = true; exports.defaultReporter = void 0; /* eslint-disable no-console */ var errors_1 = require("./errors"); var defaultLogger = console.error.bind(console); // Apply ANSI colors to the reporter output only if we detect that we're running in Node var isNode = !!(typeof process === 'object' && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node)); var colorWith = function (colorCode) { return function (str) { return isNode ? "\u001B[".concat(colorCode, "m").concat(str, "\u001B[0m") : str; }; }; var colors = { blue: colorWith('34'), white: colorWith('37'), yellow: colorWith('33') }; var RULE = colors.white('================================'); var defaultReporter = function (_a, _b) { var _c; var _d = _a.errors, errors = _d === void 0 ? {} : _d; var _e = _b === void 0 ? { logger: defaultLogger } : _b, onError = _e.onError, logger = _e.logger; if (!Object.keys(errors).length) return; var missingVarsOutput = []; var invalidVarsOutput = []; for (var _i = 0, _f = Object.entries(errors); _i < _f.length; _i++) { var _g = _f[_i], k = _g[0], err = _g[1]; if (err instanceof errors_1.EnvMissingError) { missingVarsOutput.push(" ".concat(colors.blue(k), ": ").concat(err.message || '(required)')); } else invalidVarsOutput.push(" ".concat(colors.blue(k), ": ").concat(((_c = err) === null || _c === void 0 ? void 0 : _c.message) || '(invalid format)')); } // Prepend "header" output for each section of the output: if (invalidVarsOutput.length) { invalidVarsOutput.unshift(" ".concat(colors.yellow('Invalid'), " environment variables:")); } if (missingVarsOutput.length) { missingVarsOutput.unshift(" ".concat(colors.yellow('Missing'), " environment variables:")); } var output = [ RULE, invalidVarsOutput.sort().join('\n'), missingVarsOutput.sort().join('\n'), colors.yellow('\n Exiting with error code 1'), RULE, ] .filter(function (x) { return !!x; }) .join('\n'); logger(output); if (onError) { onError(errors); } else if (isNode) { process.exit(1); } else { throw new TypeError('Environment validation failed'); } }; exports.defaultReporter = defaultReporter; //# sourceMappingURL=reporter.js.map