Move flipper-doctor check running to flipper-server-core

Summary: Per title. Two new server API's: get-healthchecks, and run-healtcheck. Types have all been moved to flipper-common, so that they can be used by doctor, server-core and ui-core packages. Since it were quite some, moved them into a FlipperDoctor namespace.

Reviewed By: nikoant

Differential Revision: D32720510

fbshipit-source-id: 37aa35cde6ebd58479cf0dffec5b7b2da6d22198
This commit is contained in:
Michel Weststrate
2021-12-08 04:25:28 -08:00
committed by Facebook GitHub Bot
parent 2a4fe77404
commit 2480ed30c5
19 changed files with 373 additions and 276 deletions

View File

@@ -0,0 +1,76 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {getHealthchecks, getEnvInfo} from 'flipper-doctor';
import {FlipperDoctor} from 'flipper-common';
import produce from 'immer';
export async function getHealthChecks(
options: FlipperDoctor.HealthcheckSettings,
) {
return produce(getHealthchecks(), (healthchecks) => {
if (!options.settings.enableAndroid) {
healthchecks.android = {
label: healthchecks.android.label,
isSkipped: true,
skipReason:
'Healthcheck is skipped, because "Android Development" option is disabled in the Flipper settings',
};
}
if (!options.settings.enableIOS) {
healthchecks.ios = {
label: healthchecks.ios.label,
isSkipped: true,
skipReason:
'Healthcheck is skipped, because "iOS Development" option is disabled in the Flipper settings',
};
}
Object.keys(healthchecks).forEach((cat) => {
const category = healthchecks[cat as keyof typeof healthchecks];
if ('healthchecks' in category) {
category.healthchecks.forEach((h) => {
delete h.run;
});
}
});
});
}
export async function runHealthcheck(
options: FlipperDoctor.HealthcheckSettings,
categoryName: keyof FlipperDoctor.Healthchecks,
ruleName: string,
): Promise<FlipperDoctor.HealthcheckResult> {
const healthchecks = getHealthchecks();
const category = healthchecks[categoryName];
if (!category) {
throw new Error('Unknown category: ' + categoryName);
}
if (!('healthchecks' in category)) {
throw new Error('Skipped category: ' + categoryName);
}
const check = category.healthchecks.find((h) => h.key === ruleName);
if (!check) {
throw new Error('Unknown healthcheck: ' + ruleName);
}
const environmentInfo = await getEnvInfo();
const checkResult = await check.run!(environmentInfo, options.settings);
return checkResult.hasProblem && check.isRequired
? {
status: 'FAILED',
message: checkResult.message,
}
: checkResult.hasProblem && !check.isRequired
? {
status: 'WARNING',
message: checkResult.message,
}
: {status: 'SUCCESS', message: checkResult.message};
}