Add Healthcheck Result type

Summary: Adds a type for the return type of runHealthchecks()

Reviewed By: passy

Differential Revision: D18474919

fbshipit-source-id: b86968088d61d9d2d31815b4c725396cc05d8ca5
This commit is contained in:
John Knox
2019-11-13 08:41:03 -08:00
committed by Facebook Github Bot
parent ad64b257f6
commit 0914deda2d

View File

@@ -34,6 +34,18 @@ type Healthcheck = {
}>; }>;
}; };
type CategoryResult = [
string,
{
label: string;
results: Array<{
label: string;
isRequired: boolean;
result: {hasProblem: boolean};
}>;
},
];
export function getHealthchecks(): Healthchecks { export function getHealthchecks(): Healthchecks {
return { return {
common: { common: {
@@ -112,32 +124,36 @@ export function getHealthchecks(): Healthchecks {
}; };
} }
export async function runHealthchecks() { export async function runHealthchecks(): Promise<Array<CategoryResult>> {
const environmentInfo = await getEnvInfo(); const environmentInfo = await getEnvInfo();
const healthchecks = getHealthchecks(); const healthchecks: Healthchecks = getHealthchecks();
const results = await Promise.all( const results: Array<CategoryResult> = (await Promise.all(
Object.entries(healthchecks).map(async ([key, category]) => [ Object.entries(healthchecks).map(async ([key, category]) => {
key, if (!category) {
category return null;
? { }
label: category.label, const categoryResult: CategoryResult = [
results: await Promise.all( key,
category.healthchecks.map(async ({label, run, isRequired}) => ({ {
label, label: category.label,
isRequired: isRequired ?? true, results: await Promise.all(
result: await run(environmentInfo).catch(e => { category.healthchecks.map(async ({label, run, isRequired}) => ({
console.error(e); label,
// TODO Improve result type to be: OK | Problem(message, fix...) isRequired: isRequired ?? true,
return { result: await run(environmentInfo).catch(e => {
hasProblem: true, console.error(e);
}; // TODO Improve result type to be: OK | Problem(message, fix...)
}), return {
})), hasProblem: true,
), };
} }),
: {}, })),
]), ),
); },
];
return categoryResult;
}),
)).filter(notNull);
return results; return results;
} }
@@ -146,3 +162,7 @@ async function commandSucceeds(command: string): Promise<boolean> {
.then(() => true) .then(() => true)
.catch(() => false); .catch(() => false);
} }
export function notNull<T>(x: T | null | undefined): x is T {
return x !== null && x !== undefined;
}