microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/v-peq/add-network-inspector-server-tests

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/services/validationService/checks/env.ts

78lines · 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
4import * as fs from "fs";
5import * as nls from "vscode-nls";
6import { fromEntries } from "../util";
7import { ValidationCategoryE, IValidation, ValidationResultT } from "./types";
8
9nls.config({
10 messageFormat: nls.MessageFormat.bundle,
11 bundleFormat: nls.BundleFormat.standalone,
12})();
13
14const toLocale = nls.loadMessageBundle();
15
16// convert windows variables in string to actual values
17const convertPathWithVars = (str: string) =>
18 str.replace(/%([^%]+)%/g, (_, n) => process.env[n] || _);
19
20async function test(androidHomeVariableName: string = "ANDROID_HOME"): Promise<ValidationResultT> {
21 const envVars: Record<string, string | undefined> = {};
22 envVars[androidHomeVariableName] = process.env[androidHomeVariableName];
23
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
68const androidHome: IValidation = {
69 label: "Android Env",
70 description: toLocale(
71 "AndroidHomeEnvCheckDescription",
72 "Required for launching React Native apps",
73 ),
74 category: ValidationCategoryE.Android,
75 exec: test,
76};
77
78export { androidHome };
79