Make healthchecks async

Summary:
Rework the healthchecks.

* Makes them healthcheck run api async so they can do more.
* Adds isRequired to the top level categories - this specifies if the category itself is required. The isRequired attributes of its constituent checks specifies if they are required within that category.
* Move isRequired outside the result of the healthcheck functions, because its static.

Reviewed By: mweststrate

Differential Revision: D18425189

fbshipit-source-id: 9d5c136a426732375d32cd27a758d9c92d123214
This commit is contained in:
John Knox
2019-11-12 06:53:57 -08:00
committed by Facebook Github Bot
parent 5bdba4935a
commit fc1e4098ed
4 changed files with 46 additions and 26 deletions

1
doctor/.ignore Symbolic link
View File

@@ -0,0 +1 @@
.gitignore

View File

@@ -1,6 +1,6 @@
{
"name": "flipper-doctor",
"version": "0.1.0",
"version": "0.2.0",
"description": "Utility for checking for issues with a flipper installation",
"main": "lib/index.js",
"types": "lib/index.d.ts",

View File

@@ -7,24 +7,10 @@
* @format
*/
import {getEnvInfo} from './environmentInfo';
import {getHealthchecks} from './index';
import {runHealthchecks} from './index';
(async () => {
const environmentInfo = await getEnvInfo();
const healthchecks = getHealthchecks();
const results = Object.entries(healthchecks).map(([key, category]) => [
key,
category
? {
label: category.label,
results: category.healthchecks.map(({label, run}) => ({
label,
result: run(environmentInfo),
})),
}
: {},
]);
const results = await runHealthchecks();
console.log(JSON.stringify(results, null, 2));
})();

View File

@@ -7,10 +7,12 @@
* @format
*/
import {EnvironmentInfo} from './environmentInfo';
import {getEnvInfo, EnvironmentInfo} from './environmentInfo';
export {getEnvInfo} from './environmentInfo';
type HealthcheckCategory = {
label: string;
isRequired: boolean;
healthchecks: Healthcheck[];
};
@@ -21,24 +23,25 @@ type Healthchecks = {
type Healthcheck = {
label: string;
isRequired?: boolean;
run: (
env: EnvironmentInfo,
) => {
) => Promise<{
hasProblem: boolean;
isRequired?: boolean;
};
}>;
};
export function getHealthchecks(): Healthchecks {
return {
android: {
label: 'Android',
isRequired: false,
healthchecks: [
{
label: 'SDK Installed',
run: (e: EnvironmentInfo) => ({
hasProblem: e.SDKs['Android SDK'] != 'Not Found',
isRequired: false,
isRequired: true,
run: async (e: EnvironmentInfo) => ({
hasProblem: e.SDKs['Android SDK'] === 'Not Found',
}),
},
],
@@ -47,12 +50,13 @@ export function getHealthchecks(): Healthchecks {
? {
ios: {
label: 'iOS',
isRequired: false,
healthchecks: [
{
label: 'SDK Installed',
run: (e: EnvironmentInfo) => ({
isRequired: true,
run: async (e: EnvironmentInfo) => ({
hasProblem: e.SDKs['iOS SDK'].Platforms.length === 0,
isRequired: false,
}),
},
],
@@ -61,3 +65,32 @@ export function getHealthchecks(): Healthchecks {
: {}),
};
}
export async function runHealthchecks() {
const environmentInfo = await getEnvInfo();
const healthchecks = getHealthchecks();
const results = await Promise.all(
Object.entries(healthchecks).map(async ([key, category]) => [
key,
category
? {
label: category.label,
results: await Promise.all(
category.healthchecks.map(async ({label, run, isRequired}) => ({
label,
isRequired: isRequired ?? true,
result: await run(environmentInfo).catch(e => {
console.error(e);
// TODO Improve result type to be: OK | Problem(message, fix...)
return {
hasProblem: true,
};
}),
})),
),
}
: {},
]),
);
return results;
}