UNPKG

3 kBJavaScriptView Raw
1"use strict";
2var _a;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.defaultReporter = exports.envalidErrorFormatter = void 0;
5/* eslint-disable no-console */
6var errors_1 = require("./errors");
7var defaultLogger = console.error.bind(console);
8// Apply ANSI colors to the reporter output only if we detect that we're running in Node
9var isNode = !!(typeof process === 'object' && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node));
10var colorWith = function (colorCode) { return function (str) {
11 return isNode ? "\u001B[".concat(colorCode, "m").concat(str, "\u001B[0m") : str;
12}; };
13var colors = {
14 blue: colorWith('34'),
15 white: colorWith('37'),
16 yellow: colorWith('33'),
17};
18var RULE = colors.white('================================');
19// Takes the provided errors, formats them all to an output string, and passes that string output to the
20// provided "logger" function.
21//
22// This is exposed in the public API so third-party reporters can leverage this logic if desired
23var envalidErrorFormatter = function (errors, logger) {
24 if (logger === void 0) { logger = defaultLogger; }
25 var missingVarsOutput = [];
26 var invalidVarsOutput = [];
27 for (var _i = 0, _a = Object.entries(errors); _i < _a.length; _i++) {
28 var _b = _a[_i], k = _b[0], err = _b[1];
29 if (err instanceof errors_1.EnvMissingError) {
30 missingVarsOutput.push(" ".concat(colors.blue(k), ": ").concat(err.message || '(required)'));
31 }
32 else
33 invalidVarsOutput.push(" ".concat(colors.blue(k), ": ").concat((err === null || err === void 0 ? void 0 : err.message) || '(invalid format)'));
34 }
35 // Prepend "header" output for each section of the output:
36 if (invalidVarsOutput.length) {
37 invalidVarsOutput.unshift(" ".concat(colors.yellow('Invalid'), " environment variables:"));
38 }
39 if (missingVarsOutput.length) {
40 missingVarsOutput.unshift(" ".concat(colors.yellow('Missing'), " environment variables:"));
41 }
42 var output = [
43 RULE,
44 invalidVarsOutput.sort().join('\n'),
45 missingVarsOutput.sort().join('\n'),
46 RULE,
47 ]
48 .filter(function (x) { return !!x; })
49 .join('\n');
50 logger(output);
51};
52exports.envalidErrorFormatter = envalidErrorFormatter;
53var defaultReporter = function (_a, _b) {
54 var _c = _a.errors, errors = _c === void 0 ? {} : _c;
55 var _d = _b === void 0 ? { logger: defaultLogger } : _b, onError = _d.onError, logger = _d.logger;
56 if (!Object.keys(errors).length)
57 return;
58 (0, exports.envalidErrorFormatter)(errors, logger);
59 if (onError) {
60 onError(errors);
61 }
62 else if (isNode) {
63 logger(colors.yellow('\n Exiting with error code 1'));
64 process.exit(1);
65 }
66 else {
67 throw new TypeError('Environment validation failed');
68 }
69};
70exports.defaultReporter = defaultReporter;
71//# sourceMappingURL=reporter.js.map
\No newline at end of file