From f9c33f8b6fd2010d49782a25a1790a540e16f78b Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Tue, 3 Dec 2024 11:07:38 -0800 Subject: [PATCH] chore(core): remove unused `download-to-file` util (#3774) * chore: remove unused `download-to-file` util * remove types --- package.json | 2 - packages/api/core/package.json | 1 - .../api/core/src/util/download-to-file.ts | 57 ------------------- 3 files changed, 60 deletions(-) diff --git a/package.json b/package.json index 3d6e50b3c8..ab0fa6178d 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,6 @@ "mime-types": "^2.1.25", "node-fetch": "^2.6.7", "parse-author": "^2.0.0", - "progress": "^2.0.3", "rechoir": "^0.8.0", "resolve-package": "^1.0.1", "semver": "^7.2.1", @@ -103,7 +102,6 @@ "@types/mocha": "^9.0.0", "@types/node": "^18.0.3", "@types/node-fetch": "^2.5.5", - "@types/progress": "^2.0.5", "@types/proxyquire": "^1.3.28", "@types/rechoir": "^0.6.1", "@types/semver": "^7.3.4", diff --git a/packages/api/core/package.json b/packages/api/core/package.json index 153adae6dc..22f16a6717 100644 --- a/packages/api/core/package.json +++ b/packages/api/core/package.json @@ -67,7 +67,6 @@ "lodash": "^4.17.20", "log-symbols": "^4.0.0", "node-fetch": "^2.6.7", - "progress": "^2.0.3", "rechoir": "^0.8.0", "resolve-package": "^1.0.1", "semver": "^7.2.1", diff --git a/packages/api/core/src/util/download-to-file.ts b/packages/api/core/src/util/download-to-file.ts index 94b80c6b95..e69de29bb2 100644 --- a/packages/api/core/src/util/download-to-file.ts +++ b/packages/api/core/src/util/download-to-file.ts @@ -1,57 +0,0 @@ -import * as path from 'path'; - -import * as fs from 'fs-extra'; -import got, { HTTPError } from 'got'; -import ProgressBar from 'progress'; - -const PROGRESS_BAR_DELAY_IN_SECONDS = 30; - -export async function downloadToFile(targetFilePath: string, url: string): Promise { - let downloadCompleted = false; - let bar: ProgressBar | undefined; - let progressPercent: number; - await fs.mkdirp(path.dirname(targetFilePath)); - const writeStream = fs.createWriteStream(targetFilePath); - - const start = new Date(); - const timeout = setTimeout(() => { - if (!downloadCompleted) { - bar = new ProgressBar(`Downloading ${path.basename(url)}: [:bar] :percent ETA: :eta seconds `, { - curr: progressPercent, - total: 100, - }); - // https://github.com/visionmedia/node-progress/issues/159 - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (bar as any).start = start; - } - }, PROGRESS_BAR_DELAY_IN_SECONDS * 1000); - - await new Promise((resolve, reject) => { - const downloadStream = got.stream(url); - downloadStream.on('downloadProgress', async (progress) => { - progressPercent = progress.percent; - if (bar) { - bar.update(progress.percent); - } - }); - downloadStream.on('error', (error) => { - if (error instanceof HTTPError && error.response.statusCode === 404) { - error.message += ` for ${error.response.url}`; - } - if (writeStream.destroy) { - writeStream.destroy(error); - } - - reject(error); - }); - writeStream.on('error', (error) => reject(error)); - writeStream.on('close', () => resolve()); - - downloadStream.pipe(writeStream); - }); - - downloadCompleted = true; - if (timeout) { - clearTimeout(timeout); - } -}