microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/services/validationService/checker.ts
111lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as nls from "vscode-nls"; |
| 5 | import { PackageVersion } from "../../../common/projectVersionHelper"; |
| 6 | import { OutputChannelLogger } from "../../log/OutputChannelLogger"; |
| 7 | import { getChecks } from "./checks"; |
| 8 | import { ValidationCategoryE, IValidation, ValidationResultT } from "./checks/types"; |
| 9 | import { fromEntries } from "./util"; |
| 10 | |
| 11 | nls.config({ |
| 12 | messageFormat: nls.MessageFormat.bundle, |
| 13 | bundleFormat: nls.BundleFormat.standalone, |
| 14 | })(); |
| 15 | |
| 16 | const toLocale = nls.loadMessageBundle(); |
| 17 | const outputChannel = OutputChannelLogger.getMainChannel(); |
| 18 | |
| 19 | const evaluteChecks = async (checks: IValidation[]) => { |
| 20 | const execToEntries = (categ: ValidationCategoryE, toCheck: IValidation[]) => |
| 21 | Promise.all( |
| 22 | toCheck |
| 23 | .filter(it => it.category === categ) |
| 24 | .map( |
| 25 | async it => |
| 26 | [ |
| 27 | it, |
| 28 | await it.exec().catch(err => { |
| 29 | outputChannel.warning(`Check ${it.label} failed with error`); |
| 30 | outputChannel.warning(err); |
| 31 | |
| 32 | return { |
| 33 | status: "failure", |
| 34 | comment: "Check execution failed", |
| 35 | } as ValidationResultT; |
| 36 | }), |
| 37 | ] as const, |
| 38 | ), |
| 39 | ); |
| 40 | |
| 41 | return fromEntries( |
| 42 | await Promise.all( |
| 43 | Object.values(ValidationCategoryE).map( |
| 44 | async it => [it, new Map(await execToEntries(it, checks))] as const, |
| 45 | ), |
| 46 | ), |
| 47 | ); |
| 48 | }; |
| 49 | |
| 50 | const statusToSymbol = { |
| 51 | success: "✓", |
| 52 | failure: "✖", |
| 53 | "partial-success": "❔", |
| 54 | }; |
| 55 | |
| 56 | export const runChecks = async ( |
| 57 | options_?: Partial<Record<ValidationCategoryE, boolean>>, |
| 58 | versions?: PackageVersion[], |
| 59 | ): Promise<void> => { |
| 60 | const options = Object.assign( |
| 61 | { |
| 62 | [ValidationCategoryE.Common]: true, |
| 63 | [ValidationCategoryE.Android]: true, |
| 64 | [ValidationCategoryE.iOS]: true, |
| 65 | }, |
| 66 | options_, |
| 67 | ); |
| 68 | |
| 69 | outputChannel.setFocusOnLogChannel(); |
| 70 | outputChannel.info(toLocale("DevEnvVerificationStart", "Starting Environment check...")); |
| 71 | const checks = await evaluteChecks( |
| 72 | getChecks(versions).filter(it => options?.[it.category] === true), |
| 73 | ); |
| 74 | |
| 75 | let outStr = `<<< ${toLocale( |
| 76 | "DevEnvVerificationHeader", |
| 77 | "Dev Environment verification result", |
| 78 | )} >>>\n`; |
| 79 | |
| 80 | Object.entries(checks).forEach(async ([key, val]) => { |
| 81 | if (val.size === 0) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | outStr += `\n*** ${key} ***\n`; |
| 86 | |
| 87 | val.forEach((execResult, validation) => { |
| 88 | outStr += ` ${statusToSymbol[execResult.status]} ${validation.label}`; |
| 89 | |
| 90 | if (execResult.status !== "success") { |
| 91 | outStr += ` - ${validation.description}\n`; |
| 92 | outStr += ` ${execResult.comment || ""}`; |
| 93 | } |
| 94 | |
| 95 | outStr += "\n"; |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | const allPassed = ([] as ValidationResultT[]) |
| 100 | .concat(...Object.entries(checks).map(it => [...it[1].values()])) |
| 101 | .every(it => it.status === "success"); |
| 102 | |
| 103 | if (allPassed) { |
| 104 | outStr += `\n${toLocale( |
| 105 | "DevEnvVerificationFooter", |
| 106 | "All checks passed successfully!", |
| 107 | )}\n\n`; |
| 108 | } |
| 109 | |
| 110 | outputChannel.logStream(outStr); |
| 111 | }; |
| 112 | |