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