From 0914deda2d82863f0ff95f1c34255e1395d4fd44 Mon Sep 17 00:00:00 2001 From: John Knox Date: Wed, 13 Nov 2019 08:41:03 -0800 Subject: [PATCH] Add Healthcheck Result type Summary: Adds a type for the return type of runHealthchecks() Reviewed By: passy Differential Revision: D18474919 fbshipit-source-id: b86968088d61d9d2d31815b4c725396cc05d8ca5 --- doctor/src/index.ts | 70 +++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/doctor/src/index.ts b/doctor/src/index.ts index 51eacd43f..ba6903bf2 100644 --- a/doctor/src/index.ts +++ b/doctor/src/index.ts @@ -34,6 +34,18 @@ type Healthcheck = { }>; }; +type CategoryResult = [ + string, + { + label: string; + results: Array<{ + label: string; + isRequired: boolean; + result: {hasProblem: boolean}; + }>; + }, +]; + export function getHealthchecks(): Healthchecks { return { common: { @@ -112,32 +124,36 @@ export function getHealthchecks(): Healthchecks { }; } -export async function runHealthchecks() { +export async function runHealthchecks(): Promise> { 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, - }; - }), - })), - ), - } - : {}, - ]), - ); + const healthchecks: Healthchecks = getHealthchecks(); + const results: Array = (await Promise.all( + Object.entries(healthchecks).map(async ([key, category]) => { + if (!category) { + return null; + } + const categoryResult: CategoryResult = [ + key, + { + 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 categoryResult; + }), + )).filter(notNull); return results; } @@ -146,3 +162,7 @@ async function commandSucceeds(command: string): Promise { .then(() => true) .catch(() => false); } + +export function notNull(x: T | null | undefined): x is T { + return x !== null && x !== undefined; +}