UNPKG

7.18 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function _chalk() {
9 const data = _interopRequireDefault(require("chalk"));
10
11 _chalk = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _cliTools() {
19 const data = require("@react-native-community/cli-tools");
20
21 _cliTools = function () {
22 return data;
23 };
24
25 return data;
26}
27
28var _healthchecks = require("./healthchecks");
29
30var _loader = require("../../tools/loader");
31
32var _printFixOptions = _interopRequireWildcard(require("./printFixOptions"));
33
34var _runAutomaticFix = _interopRequireWildcard(require("./runAutomaticFix"));
35
36var _envinfo = _interopRequireDefault(require("../../tools/envinfo"));
37
38var _common = require("./healthchecks/common");
39
40function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
41
42function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43
44function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
45
46function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
47
48const printCategory = ({
49 label,
50 key
51}) => {
52 if (key > 0) {
53 _cliTools().logger.log();
54 }
55
56 _cliTools().logger.log(_chalk().default.dim(label));
57};
58
59const printIssue = ({
60 label,
61 needsToBeFixed,
62 version,
63 versionRange,
64 isRequired,
65 description
66}) => {
67 const symbol = needsToBeFixed ? isRequired ? _chalk().default.red('✖') : _chalk().default.yellow('●') : _chalk().default.green('✓');
68 const descriptionToShow = description ? ` - ${description}` : '';
69
70 _cliTools().logger.log(` ${symbol} ${label}${descriptionToShow}`);
71
72 if (needsToBeFixed && versionRange) {
73 const versionToShow = version && version !== 'Not Found' ? version : 'N/A';
74 (0, _common.logMessage)(`- Version found: ${_chalk().default.red(versionToShow)}`);
75 (0, _common.logMessage)(`- Version supported: ${_chalk().default.green(versionRange)}`);
76 return;
77 }
78};
79
80const printOverallStats = ({
81 errors,
82 warnings
83}) => {
84 _cliTools().logger.log(`\n${_chalk().default.bold('Errors:')} ${errors}`);
85
86 _cliTools().logger.log(`${_chalk().default.bold('Warnings:')} ${warnings}`);
87};
88
89var _default = async (_, __, options) => {
90 const Loader = (0, _loader.getLoader)();
91 const loader = new Loader();
92 loader.start('Running diagnostics...');
93 const environmentInfo = await (0, _envinfo.default)();
94
95 const iterateOverHealthChecks = async ({
96 label,
97 healthchecks
98 }) => ({
99 label,
100 healthchecks: (await Promise.all(healthchecks.map(async healthcheck => {
101 if (healthcheck.visible === false) {
102 return;
103 }
104
105 const {
106 needsToBeFixed,
107 version,
108 versionRange
109 } = await healthcheck.getDiagnostics(environmentInfo); // Assume that it's required unless specified otherwise
110
111 const isRequired = healthcheck.isRequired !== false;
112 const isWarning = needsToBeFixed && !isRequired;
113 return {
114 label: healthcheck.label,
115 needsToBeFixed: Boolean(needsToBeFixed),
116 version,
117 versionRange,
118 description: healthcheck.description,
119 runAutomaticFix: healthcheck.runAutomaticFix,
120 isRequired,
121 type: needsToBeFixed ? isWarning ? _healthchecks.HEALTHCHECK_TYPES.WARNING : _healthchecks.HEALTHCHECK_TYPES.ERROR : undefined
122 };
123 }))).filter(healthcheck => healthcheck !== undefined)
124 }); // Remove all the categories that don't have any healthcheck with
125 // `needsToBeFixed` so they don't show when the user taps to fix encountered
126 // issues
127
128
129 const removeFixedCategories = categories => categories.filter(category => category.healthchecks.some(healthcheck => healthcheck.needsToBeFixed));
130
131 const iterateOverCategories = categories => Promise.all(categories.map(iterateOverHealthChecks));
132
133 const healthchecksPerCategory = await iterateOverCategories(Object.values((0, _healthchecks.getHealthchecks)(options)).filter(category => category !== undefined));
134 loader.stop();
135 const stats = {
136 errors: 0,
137 warnings: 0
138 };
139 healthchecksPerCategory.forEach((issueCategory, key) => {
140 printCategory(_objectSpread({}, issueCategory, {
141 key
142 }));
143 issueCategory.healthchecks.forEach(healthcheck => {
144 printIssue(healthcheck);
145
146 if (healthcheck.type === _healthchecks.HEALTHCHECK_TYPES.WARNING) {
147 stats.warnings++;
148 return;
149 }
150
151 if (healthcheck.type === _healthchecks.HEALTHCHECK_TYPES.ERROR) {
152 stats.errors++;
153 return;
154 }
155 });
156 });
157 printOverallStats(stats);
158
159 if (options.fix) {
160 return await (0, _runAutomaticFix.default)({
161 healthchecks: removeFixedCategories(healthchecksPerCategory),
162 automaticFixLevel: _runAutomaticFix.AUTOMATIC_FIX_LEVELS.ALL_ISSUES,
163 stats,
164 loader,
165 environmentInfo
166 });
167 }
168
169 const removeKeyPressListener = () => {
170 if (typeof process.stdin.setRawMode === 'function') {
171 process.stdin.setRawMode(false);
172 }
173
174 process.stdin.removeAllListeners('data');
175 };
176
177 const onKeyPress = async key => {
178 if (key === _printFixOptions.KEYS.EXIT || key === '\u0003') {
179 removeKeyPressListener();
180 process.exit(0);
181 return;
182 }
183
184 if ([_printFixOptions.KEYS.FIX_ALL_ISSUES, _printFixOptions.KEYS.FIX_ERRORS, _printFixOptions.KEYS.FIX_WARNINGS].includes(key)) {
185 removeKeyPressListener();
186
187 try {
188 const automaticFixLevel = {
189 [_printFixOptions.KEYS.FIX_ALL_ISSUES]: _runAutomaticFix.AUTOMATIC_FIX_LEVELS.ALL_ISSUES,
190 [_printFixOptions.KEYS.FIX_ERRORS]: _runAutomaticFix.AUTOMATIC_FIX_LEVELS.ERRORS,
191 [_printFixOptions.KEYS.FIX_WARNINGS]: _runAutomaticFix.AUTOMATIC_FIX_LEVELS.WARNINGS
192 };
193 await (0, _runAutomaticFix.default)({
194 healthchecks: removeFixedCategories(healthchecksPerCategory),
195 automaticFixLevel: automaticFixLevel[key],
196 stats,
197 loader,
198 environmentInfo
199 });
200 process.exit(0);
201 } catch (err) {
202 // TODO: log error
203 process.exit(1);
204 }
205 }
206 };
207
208 if (stats.errors || stats.warnings) {
209 (0, _printFixOptions.default)({
210 onKeyPress
211 });
212 }
213};
214
215exports.default = _default;
\No newline at end of file