microsoft/vscode-react-native

Public

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