microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
test-microbuild1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commands/installExpoGoApplication.ts

166lines · modeblame

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