microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.9.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

80lines · modeblame

c7856462Heniker4 years ago1// 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";
09f6024fHeniker4 years ago6import { fromEntries } from "../util";
7import { ValidationCategoryE, IValidation, ValidationResultT } from "./types";
c7856462Heniker4 years ago8
9nls.config({
10messageFormat: nls.MessageFormat.bundle,
11bundleFormat: nls.BundleFormat.standalone,
12})();
13
14const toLocale = nls.loadMessageBundle();
15
16// convert windows variables in string to actual values
17const convertPathWithVars = (str: string) =>
18str.replace(/%([^%]+)%/g, (_, n) => process.env[n] || _);
19
20async function test(): Promise<ValidationResultT> {
948a0d44Heniker4 years ago21const envVars = {
22ANDROID_HOME: process.env.ANDROID_HOME,
23};
24
c7856462Heniker4 years ago25const resolvedEnv = fromEntries(
26Object.entries(envVars).map(([key, val]) => [
27key,
28{ original: val, resolved: val && convertPathWithVars(val) },
29]),
30);
31
32const notFoundVariable = Object.entries(resolvedEnv).find(([, val]) => !val.original)?.[0];
33
34if (notFoundVariable) {
35return {
36status: "failure",
37comment:
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
43const notFoundPath = Object.entries(resolvedEnv).find(
44([, val]) => val.resolved && !fs.existsSync(val.resolved),
45)?.[0];
46
47if (notFoundPath) {
48return {
49status: "failure",
50comment: `"${notFoundPath}" does not point to an existing path`,
51};
52}
53
54const valUsesEnv = Object.entries(resolvedEnv).find(
55([key, val]) => val.original !== val.resolved,
56)?.[0];
57
58if (valUsesEnv) {
59return {
60status: "partial-success",
61comment: `"${valUsesEnv}" uses environment variable in its path. This may cause errors`,
62};
63}
64
65return {
66status: "success",
67};
68}
69
70const androidHome: IValidation = {
71label: "ANDROID_HOME Env",
72description: toLocale(
73"AndroidHomeEnvCheckDescription",
74"Required for launching React Native apps",
75),
76category: ValidationCategoryE.Android,
77exec: test,
78};
79
80export { androidHome };