microsoft/vscode-react-native

Public

mirrored fromhttps://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/common/downloadHelper.ts

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