microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/services/validationService/checks/env.ts
78lines · modeblame
c7856462Heniker4 years ago | 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 fs from "fs"; | |
| 5 | import * as nls from "vscode-nls"; | |
09f6024fHeniker4 years ago | 6 | import { fromEntries } from "../util"; |
| 7 | import { ValidationCategoryE, IValidation, ValidationResultT } from "./types"; | |
c7856462Heniker4 years ago | 8 | |
| 9 | nls.config({ | |
| 10 | messageFormat: nls.MessageFormat.bundle, | |
| 11 | bundleFormat: nls.BundleFormat.standalone, | |
| 12 | })(); | |
| 13 | | |
| 14 | const toLocale = nls.loadMessageBundle(); | |
| 15 | | |
| 16 | // convert windows variables in string to actual values | |
| 17 | const convertPathWithVars = (str: string) => | |
| 18 | str.replace(/%([^%]+)%/g, (_, n) => process.env[n] || _); | |
| 19 | | |
8609d701AndreiLobanovich4 years ago | 20 | async function test(androidHomeVariableName: string = "ANDROID_HOME"): Promise<ValidationResultT> { |
| 21 | const envVars: Record<string, string | undefined> = {}; | |
| 22 | envVars[androidHomeVariableName] = process.env[androidHomeVariableName]; | |
948a0d44Heniker4 years ago | 23 | |
c7856462Heniker4 years ago | 24 | const resolvedEnv = fromEntries( |
| 25 | Object.entries(envVars).map(([key, val]) => [ | |
| 26 | key, | |
| 27 | { original: val, resolved: val && convertPathWithVars(val) }, | |
| 28 | ]), | |
| 29 | ); | |
| 30 | | |
| 31 | const notFoundVariable = Object.entries(resolvedEnv).find(([, val]) => !val.original)?.[0]; | |
| 32 | | |
| 33 | if (notFoundVariable) { | |
| 34 | return { | |
| 35 | status: "failure", | |
| 36 | comment: | |
| 37 | `"${notFoundVariable}" not found in path. ` + | |
| 38 | `Ensure all variables are set up accroding to this guide - https://reactnative.dev/docs/environment-setup`, | |
| 39 | }; | |
| 40 | } | |
| 41 | | |
| 42 | const notFoundPath = Object.entries(resolvedEnv).find( | |
| 43 | ([, val]) => val.resolved && !fs.existsSync(val.resolved), | |
| 44 | )?.[0]; | |
| 45 | if (notFoundPath) { | |
| 46 | return { | |
| 47 | status: "failure", | |
| 48 | comment: `"${notFoundPath}" does not point to an existing path`, | |
| 49 | }; | |
| 50 | } | |
| 51 | | |
| 52 | const valUsesEnv = Object.entries(resolvedEnv).find( | |
| 53 | ([key, val]) => val.original !== val.resolved, | |
| 54 | )?.[0]; | |
| 55 | | |
| 56 | if (valUsesEnv) { | |
| 57 | return { | |
| 58 | status: "partial-success", | |
| 59 | comment: `"${valUsesEnv}" uses environment variable in its path. This may cause errors`, | |
| 60 | }; | |
| 61 | } | |
| 62 | | |
| 63 | return { | |
| 64 | status: "success", | |
| 65 | }; | |
| 66 | } | |
| 67 | | |
| 68 | const androidHome: IValidation = { | |
8609d701AndreiLobanovich4 years ago | 69 | label: "Android Env", |
c7856462Heniker4 years ago | 70 | description: toLocale( |
| 71 | "AndroidHomeEnvCheckDescription", | |
| 72 | "Required for launching React Native apps", | |
| 73 | ), | |
| 74 | category: ValidationCategoryE.Android, | |
| 75 | exec: test, | |
| 76 | }; | |
| 77 | | |
| 78 | export { androidHome }; |