microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/v-peq/removeNode10TodoComments

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commands/installExpoGoApplication.ts

166lines · 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 assert = require("assert");
5import * as fs from "fs";
6import * as https from "https";
7import * as os from "os";
8import * as vscode from "vscode";
9import * as nls from "vscode-nls";
10import { OutputChannelLogger } from "../log/OutputChannelLogger";
11import { ErrorHelper } from "../../common/error/errorHelper";
12import { InternalErrorCode } from "../../common/error/internalErrorCode";
13import { downloadExpoGo } from "../../common/downloadHelper";
14import { installAndroidApplication, installiOSApplication } from "../../common/installHelper";
15import { Command } from "./util/command";
16
17nls.config({
18 messageFormat: nls.MessageFormat.bundle,
19 bundleFormat: nls.BundleFormat.standalone,
20})();
21const localize = nls.loadMessageBundle();
22const logger = OutputChannelLogger.getMainChannel();
23
24export class InstallExpoGoApplication extends Command {
25 codeName = "installExpoGoApplication";
26 label = "Download and install Expo Go on simulator or device";
27 error = ErrorHelper.getInternalError(InternalErrorCode.FailedToInstallExpoGo);
28
29 async baseFn(): Promise<void> {
30 assert(this.project);
31 const item = await vscode.window.showQuickPick(["Android", "iOS"], {
32 placeHolder: "Select type for mobile OS",
33 });
34 const installItem = await vscode.window.showQuickPick(["Manual", "Auto"], {
35 placeHolder: "How to install application",
36 });
37 const expoHelper = this.project.getExponentHelper();
38 logger.info(localize("CheckExpoEnvironment", "Checking Expo project environment."));
39 const isExpo = await expoHelper.isExpoManagedApp(true);
40
41 const expoGoListAPI = "https://api.expo.dev/v2/versions";
42 const apiJson = await fetchJson(expoGoListAPI);
43 const jsonContent = JSON.parse(apiJson);
44
45 if (isExpo) {
46 const currentSdkVersion = await expoHelper.exponentSdk(true);
47 const expoUrlInfo = jsonContent.sdkVersions[currentSdkVersion];
48
49 if (item == "Android") {
50 void vscode.window.showInformationMessage("Downloading Expo Go for Android.");
51 logger.logStream(
52 localize("DownloadAndroidExpoGo", "\nDownloading Expo Go for Android. \n"),
53 );
54
55 const targetUrl = expoUrlInfo.androidClientUrl;
56 const androidClientVersion = expoUrlInfo.androidClientVersion as string;
57 const fileName = `${this.project
58 .getPackager()
59 .getProjectPath()}/expogo_${androidClientVersion}.apk`;
60
61 if (!fs.existsSync(fileName)) {
62 try {
63 await downloadExpoGo(targetUrl, fileName);
64 } catch {
65 throw new Error(
66 localize("FailedToDownloadExpoGo", "Failed to download Expo Go."),
67 );
68 }
69 }
70
71 if (installItem == "Auto") {
72 try {
73 await installAndroidApplication(this.project, fileName);
74 } catch {
75 throw new Error(
76 localize("FailedToInstallExpoGo", "Failed to install Expo Go."),
77 );
78 }
79 } else {
80 logger.logStream(
81 localize(
82 "ManualInstall",
83 "Please manually install Expo Go from project root path. \n",
84 ),
85 );
86 }
87 } else if (item == "iOS") {
88 if (os.platform() != "darwin") {
89 logger.warning(
90 localize(
91 "NotDarwinPlatform",
92 "Current OS may not support iOS installer. The Expo Go may not be installed.\n",
93 ),
94 );
95 }
96 void vscode.window.showInformationMessage("Downloading Expo Go for iOS.");
97 logger.logStream(
98 localize("DownloadiOSExpoGo", "\nDownloading Expo Go for iOS. \n"),
99 );
100
101 const targetUrl = expoUrlInfo.iosClientUrl;
102 const iOSClientVersion = expoUrlInfo.iosClientVersion as string;
103
104 const tarFile = `${this.project
105 .getPackager()
106 .getProjectPath()}/expogo_${iOSClientVersion}.tar.gz`;
107
108 if (!fs.existsSync(tarFile)) {
109 try {
110 await downloadExpoGo(
111 targetUrl,
112 `${this.project
113 .getPackager()
114 .getProjectPath()}/expogo_${iOSClientVersion}.tar.gz`,
115 );
116 } catch {
117 throw new Error(
118 localize("FailedToDownloadExpoGo", "Failed to download Expo Go."),
119 );
120 }
121 }
122
123 if (installItem == "Auto" && os.platform() == "darwin") {
124 try {
125 await installiOSApplication(this.project, tarFile);
126 } catch {
127 throw new Error(
128 localize("FailedToInstallExpoGo", "Failed to install Expo Go."),
129 );
130 }
131 } else {
132 logger.logStream(
133 localize(
134 "CannotAutoInstall",
135 "Cannot auto install Expo Go, selected manual install or target machine is not MacOS. \n",
136 ),
137 );
138 }
139 } else {
140 return;
141 }
142 } else {
143 throw new Error(localize("NotExpoProject", "Current project is not Expo managed."));
144 }
145 }
146}
147
148async function fetchJson(url: string): Promise<string> {
149 return new Promise<string>((fulfill, reject) => {
150 const requestOptions: https.RequestOptions = {};
151 requestOptions.rejectUnauthorized = false; // CodeQL [js/disabling-certificate-validation] Debug extension does not need to verify certificate
152
153 const request = https.get(url, requestOptions, response => {
154 let data = "";
155 response.setEncoding("utf8");
156 response.on("data", (chunk: string) => {
157 data += chunk;
158 });
159 response.on("end", () => fulfill(data));
160 response.on("error", reject);
161 });
162
163 request.on("error", reject);
164 request.end();
165 });
166}
167