microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.12.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/downloadHelper.ts

61lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3import * as fs from "fs";
4import * as https from "https";
5import * as vscode from "vscode";
6import { OutputChannelLogger } from "../extension/log/OutputChannelLogger";
7
8export async function downloadFile(url: any, targetFile: any) {
9 const logger = OutputChannelLogger.getMainChannel();
10 let progress = 0;
11 let newProgress = 0;
12 return await new Promise((resolve, reject) => {
13 const request = https
14 .get(url, response => {
15 const code = response.statusCode ?? 0;
16
17 if (code >= 400) {
18 return reject(new Error(response.statusMessage));
19 }
20
21 const file = fs.createWriteStream(targetFile);
22 const totalLength = parseInt(response.headers["content-length"] as string, 10);
23
24 response.pipe(file);
25 response.on("data", async function (chunk) {
26 newProgress += chunk.length;
27 const currentProgress =
28 parseFloat(getDownloadProgress(newProgress, totalLength)) * 100;
29 if (currentProgress - progress >= 5) {
30 progress = currentProgress;
31 logger.logStream(
32 `Current progress: ${currentProgress}%, please wait... \n`,
33 );
34 }
35 });
36
37 file.on("finish", async () => {
38 file.close();
39 logger.logStream(`Download Expo Go Completed: ${targetFile as string}`);
40 void vscode.window.showInformationMessage("Download Expo Go Completed.");
41 });
42
43 response.on("end", function () {
44 console.log("Progress end.");
45 });
46 })
47 .on("error", error => {
48 reject(error);
49 });
50
51 request.end();
52 });
53}
54
55export async function downloadExpoGo(url: string, targetFile: string) {
56 await downloadFile(url, targetFile);
57}
58
59function getDownloadProgress(currentLength: number, totalLength: number): string {
60 return (currentLength / totalLength).toFixed(2);
61}
62