From fc1e4098ed72f4c208abd978123600fb7aff323f Mon Sep 17 00:00:00 2001 From: John Knox Date: Tue, 12 Nov 2019 06:53:57 -0800 Subject: [PATCH] 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 --- doctor/.ignore | 1 + doctor/package.json | 2 +- doctor/src/cli.ts | 18 ++-------------- doctor/src/index.ts | 51 +++++++++++++++++++++++++++++++++++++-------- 4 files changed, 46 insertions(+), 26 deletions(-) create mode 120000 doctor/.ignore diff --git a/doctor/.ignore b/doctor/.ignore new file mode 120000 index 000000000..3e4e48b0b --- /dev/null +++ b/doctor/.ignore @@ -0,0 +1 @@ +.gitignore \ No newline at end of file diff --git a/doctor/package.json b/doctor/package.json index 4877beda7..df36ab13a 100644 --- a/doctor/package.json +++ b/doctor/package.json @@ -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", diff --git a/doctor/src/cli.ts b/doctor/src/cli.ts index 7c9ddb3c9..457c353db 100644 --- a/doctor/src/cli.ts +++ b/doctor/src/cli.ts @@ -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)); })(); diff --git a/doctor/src/index.ts b/doctor/src/index.ts index ddc24afc7..ad58a05ba 100644 --- a/doctor/src/index.ts +++ b/doctor/src/index.ts @@ -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; +}