1 | "use strict";
|
2 | var _a;
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.defaultReporter = exports.envalidErrorFormatter = void 0;
|
5 |
|
6 | var errors_1 = require("./errors");
|
7 | var defaultLogger = console.error.bind(console);
|
8 |
|
9 | var isNode = !!(typeof process === 'object' && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node));
|
10 | var colorWith = function (colorCode) { return function (str) {
|
11 | return isNode ? "\u001B[".concat(colorCode, "m").concat(str, "\u001B[0m") : str;
|
12 | }; };
|
13 | var colors = {
|
14 | blue: colorWith('34'),
|
15 | white: colorWith('37'),
|
16 | yellow: colorWith('33'),
|
17 | };
|
18 | var RULE = colors.white('================================');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var 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 |
|
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 | };
|
52 | exports.envalidErrorFormatter = envalidErrorFormatter;
|
53 | var 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 | };
|
70 | exports.defaultReporter = defaultReporter;
|
71 |
|
\ | No newline at end of file |