microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix-ts-error1

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

78lines · 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
8609d701AndreiLobanovich4 years ago20async function test(androidHomeVariableName: string = "ANDROID_HOME"): Promise<ValidationResultT> {
21const envVars: Record<string, string | undefined> = {};
22envVars[androidHomeVariableName] = process.env[androidHomeVariableName];
948a0d44Heniker4 years ago23
c7856462Heniker4 years ago24const resolvedEnv = fromEntries(
25Object.entries(envVars).map(([key, val]) => [
26key,
27{ original: val, resolved: val && convertPathWithVars(val) },
28]),
29);
30
31const notFoundVariable = Object.entries(resolvedEnv).find(([, val]) => !val.original)?.[0];
32
33if (notFoundVariable) {
34return {
35status: "failure",
36comment:
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
42const notFoundPath = Object.entries(resolvedEnv).find(
43([, val]) => val.resolved && !fs.existsSync(val.resolved),
44)?.[0];
45if (notFoundPath) {
46return {
47status: "failure",
48comment: `"${notFoundPath}" does not point to an existing path`,
49};
50}
51
52const valUsesEnv = Object.entries(resolvedEnv).find(
53([key, val]) => val.original !== val.resolved,
54)?.[0];
55
56if (valUsesEnv) {
57return {
58status: "partial-success",
59comment: `"${valUsesEnv}" uses environment variable in its path. This may cause errors`,
60};
61}
62
63return {
64status: "success",
65};
66}
67
68const androidHome: IValidation = {
8609d701AndreiLobanovich4 years ago69label: "Android Env",
c7856462Heniker4 years ago70description: toLocale(
71"AndroidHomeEnvCheckDescription",
72"Required for launching React Native apps",
73),
74category: ValidationCategoryE.Android,
75exec: test,
76};
77
78export { androidHome };