Skip Android health-checks when the "Android Developer" option is disabled in Flipper settings

Summary:
Skip Android health-checks when the "Android Developer" option is disabled in Flipper settings.

Also made some refactoring to use immer for healthcheck reducer.

Reviewed By: mweststrate

Differential Revision: D19088322

fbshipit-source-id: 801d874b6e7e5af80802b4bf4313d98f1cee13f6
This commit is contained in:
Anton Nikolaev
2019-12-16 16:35:49 -08:00
committed by Facebook Github Bot
parent be53990613
commit d32774f439
6 changed files with 256 additions and 188 deletions

View File

@@ -86,6 +86,7 @@ const ICONS = {
'life-event-major': [16],
target: [12, 16],
tools: [12, 20],
question: [16],
underline: [12],
'washing-machine': [12],
'watch-tv': [12],

View File

@@ -10,7 +10,6 @@
import {
HealthcheckResult,
HealthcheckReport,
HealthcheckReportItem,
HealthcheckReportCategory,
} from '../reducers/healthchecks';
import {getHealthchecks, getEnvInfo} from 'flipper-doctor';
@@ -20,37 +19,69 @@ let runningHealthcheck: Promise<boolean>;
export type HealthcheckEventsHandler = {
initHealthcheckReport: (report: HealthcheckReport) => void;
updateHealthcheckReportItem: (
updateHealthcheckReportItemStatus: (
categoryIdx: number,
itemIdx: number,
item: HealthcheckReportItem,
status: HealthcheckResult,
) => void;
updateHealthcheckReportCategoryStatus: (
categoryIdx: number,
status: HealthcheckResult,
) => void;
startHealthchecks: () => void;
finishHealthchecks: () => void;
};
export type HealthcheckSettings = {
enableAndroid: boolean;
};
export type HealthcheckOptions = HealthcheckEventsHandler & HealthcheckSettings;
async function launchHealthchecks(
dispatch: HealthcheckEventsHandler,
options: HealthcheckOptions,
): Promise<boolean> {
let hasProblems: boolean = true;
dispatch.startHealthchecks();
let healthchecksResult: boolean = true;
options.startHealthchecks();
try {
const initialState: HealthcheckResult = {
const inProgressResult: HealthcheckResult = {
status: 'IN_PROGRESS',
message: 'The healthcheck is in progress',
};
const androidSkippedResult: HealthcheckResult = {
status: 'SKIPPED',
message:
'The healthcheck was skipped because Android development is disabled in the Flipper settings',
};
const failedResult: HealthcheckResult = {
status: 'FAILED',
message: 'The healthcheck failed',
};
const warningResult: HealthcheckResult = {
status: 'WARNING',
message: 'The optional healthcheck failed',
};
const succeededResult: HealthcheckResult = {
status: 'SUCCESS',
message: undefined,
};
const healthchecks = getHealthchecks();
const hcState: HealthcheckReport = {
isHealthcheckInProgress: true,
categories: Object.values(getHealthchecks())
.map(category => {
categories: Object.entries(healthchecks)
.map(([categoryKey, category]) => {
if (!category) {
return null;
}
const state: HealthcheckResult =
categoryKey === 'android' && !options.enableAndroid
? androidSkippedResult
: inProgressResult;
return {
...initialState,
...state,
label: category.label,
checks: category.healthchecks.map(x => ({
...initialState,
...state,
label: x.label,
})),
};
@@ -58,54 +89,84 @@ async function launchHealthchecks(
.filter(x => !!x)
.map(x => x as HealthcheckReportCategory),
};
dispatch.initHealthcheckReport(hcState);
options.initHealthcheckReport(hcState);
const environmentInfo = await getEnvInfo();
const categories = Object.values(getHealthchecks());
for (let cIdx = 0; cIdx < categories.length; cIdx++) {
const c = categories[cIdx];
if (!c) {
const categories = Object.entries(healthchecks);
for (const [categoryIdx, [categoryKey, category]] of categories.entries()) {
if (!category) {
continue;
}
for (let hIdx = 0; hIdx < c.healthchecks.length; hIdx++) {
const h = c.healthchecks[hIdx];
const result = await h.run(environmentInfo);
if (result.hasProblem) {
hasProblems = false;
const isSkippedAndroidCategory =
categoryKey === 'android' && !options.enableAndroid;
const allResults: HealthcheckResult[] = [];
for (
let healthcheckIdx = 0;
healthcheckIdx < category.healthchecks.length;
healthcheckIdx++
) {
const h = category.healthchecks[healthcheckIdx];
if (isSkippedAndroidCategory) {
options.updateHealthcheckReportItemStatus(
categoryIdx,
healthcheckIdx,
androidSkippedResult,
);
allResults.push(androidSkippedResult);
} else {
const result = await h.run(environmentInfo);
if (result.hasProblem && h.isRequired) {
healthchecksResult = false;
}
const status: HealthcheckResult =
result.hasProblem && h.isRequired
? {
...failedResult,
helpUrl: result.helpUrl,
}
: result.hasProblem && !h.isRequired
? {
...warningResult,
helpUrl: result.helpUrl,
}
: succeededResult;
options.updateHealthcheckReportItemStatus(
categoryIdx,
healthcheckIdx,
status,
);
allResults.push(status);
}
dispatch.updateHealthcheckReportItem(cIdx, hIdx, {
...h,
...(result.hasProblem && h.isRequired
? {
status: 'FAILED',
message: 'The healthcheck failed',
helpUrl: result.helpUrl,
}
: result.hasProblem && !h.isRequired
? {
status: 'WARNING',
message: 'Doctor discovered a problem during the healthcheck',
helpUrl: result.helpUrl,
}
: {
status: 'SUCCESS',
message: 'The healthcheck completed succesfully',
}),
});
}
const categoryStatus = {
label: category.label,
...(allResults.some(c => c.status === 'IN_PROGRESS')
? inProgressResult
: allResults.every(c => c.status === 'SUCCESS')
? succeededResult
: allResults.every(c => c.status === 'SKIPPED')
? androidSkippedResult
: allResults.some(c => c.status === 'FAILED')
? failedResult
: warningResult),
};
options.updateHealthcheckReportCategoryStatus(
categoryIdx,
categoryStatus,
);
}
} catch {
} finally {
dispatch.finishHealthchecks();
options.finishHealthchecks();
}
return hasProblems;
return healthchecksResult;
}
export default async function runHealthchecks(
dispatch: HealthcheckEventsHandler,
options: HealthcheckOptions,
): Promise<boolean> {
if (healthcheckIsRunning) {
return runningHealthcheck;
}
runningHealthcheck = launchHealthchecks(dispatch);
runningHealthcheck = launchHealthchecks(options);
return runningHealthcheck;
}