microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
smoke-actionbar-timeout-assertion

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/commands/installExpoGoApplication.ts

174lines · 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
176f99c8ConnorQi014 months ago4import assert = require("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;
83ddcba7Lucy Gramley1 months ago56const androidClientVersion = validateVersion(
57expoUrlInfo.androidClientVersion as string,
58);
2a485fd5Ezio Li2 years ago59const fileName = `${this.project
60.getPackager()
61.getProjectPath()}/expogo_${androidClientVersion}.apk`;
62
63if (!fs.existsSync(fileName)) {
64try {
65await downloadExpoGo(targetUrl, fileName);
66} catch {
67throw new Error(
68localize("FailedToDownloadExpoGo", "Failed to download Expo Go."),
69);
70}
71}
72
73if (installItem == "Auto") {
74try {
75await installAndroidApplication(this.project, fileName);
76} catch {
77throw new Error(
78localize("FailedToInstallExpoGo", "Failed to install Expo Go."),
79);
80}
81} else {
82logger.logStream(
83localize(
84"ManualInstall",
85"Please manually install Expo Go from project root path. \n",
86),
9c71977dEzio Li2 years ago87);
88}
89} else if (item == "iOS") {
90if (os.platform() != "darwin") {
91logger.warning(
92localize(
93"NotDarwinPlatform",
94"Current OS may not support iOS installer. The Expo Go may not be installed.\n",
95),
96);
97}
98void vscode.window.showInformationMessage("Downloading Expo Go for iOS.");
99logger.logStream(
100localize("DownloadiOSExpoGo", "\nDownloading Expo Go for iOS. \n"),
101);
102
103const targetUrl = expoUrlInfo.iosClientUrl;
fb07eda4lucygramley1 months ago104const iOSClientVersion = validateVersion(expoUrlInfo.iosClientVersion as string);
2a485fd5Ezio Li2 years ago105
106const tarFile = `${this.project
107.getPackager()
108.getProjectPath()}/expogo_${iOSClientVersion}.tar.gz`;
109
110if (!fs.existsSync(tarFile)) {
111try {
112await downloadExpoGo(
113targetUrl,
114`${this.project
115.getPackager()
116.getProjectPath()}/expogo_${iOSClientVersion}.tar.gz`,
117);
118} catch {
119throw new Error(
120localize("FailedToDownloadExpoGo", "Failed to download Expo Go."),
121);
122}
123}
124
125if (installItem == "Auto" && os.platform() == "darwin") {
126try {
127await installiOSApplication(this.project, tarFile);
128} catch {
129throw new Error(
130localize("FailedToInstallExpoGo", "Failed to install Expo Go."),
131);
132}
133} else {
134logger.logStream(
135localize(
136"CannotAutoInstall",
137"Cannot auto install Expo Go, selected manual install or target machine is not MacOS. \n",
138),
9c71977dEzio Li2 years ago139);
140}
141} else {
142return;
143}
144} else {
145throw new Error(localize("NotExpoProject", "Current project is not Expo managed."));
146}
147}
148}
149
150async function fetchJson(url: string): Promise<string> {
151return new Promise<string>((fulfill, reject) => {
83ddcba7Lucy Gramley1 months ago152const request = https.get(url, response => {
9c71977dEzio Li2 years ago153let data = "";
154response.setEncoding("utf8");
155response.on("data", (chunk: string) => {
156data += chunk;
157});
158response.on("end", () => fulfill(data));
159response.on("error", reject);
160});
161
162request.on("error", reject);
163request.end();
164});
165}
83ddcba7Lucy Gramley1 months ago166
167const versionPattern = /^[0-9][0-9.]*$/;
168
169function validateVersion(version: string): string {
170if (!versionPattern.test(version)) {
171throw new Error(`Invalid Expo Go version string: ${version}`);
172}
173return version;
174}