From 8edc7fd93c0b7fe215e5dbfa494da70bc266dacf Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 26 Oct 2026 23:14:36 +0300 Subject: [PATCH] refactor(download): refactor download module --- example/gulpfile.ts | 2 +- src/common/download.ts | 127 ++++++++++++++++++----------------------- src/common/output.ts | 4 +- src/core/sdk.ts | 4 +- src/impl/android.ts | 4 +- src/index.ts | 4 +- test/download.test.js | 4 +- 7 files changed, 68 insertions(+), 81 deletions(-) diff --git a/example/gulpfile.ts b/example/gulpfile.ts index 965e3b6..d87c55c 100644 --- a/example/gulpfile.ts +++ b/example/gulpfile.ts @@ -3,5 +3,5 @@ import { Preset } from "gulp-haxetool"; import presetData from "./haxetool.json" with { type: "json" }; import packageInfo from "./package.json" with { type: "json" }; -const preset = new Preset(presetData, null, packageInfo); +const preset = new Preset(presetData, { UseRuffle: true }, packageInfo); export default preset.register(gulp); diff --git a/src/common/download.ts b/src/common/download.ts index 2643e77..5b6e6b7 100644 --- a/src/common/download.ts +++ b/src/common/download.ts @@ -1,84 +1,67 @@ import progress from "cli-progress"; import fs from "fs"; import fse from "fs-extra"; -import got from "got"; +import got, { type Progress, type Request } from "got"; import path from "path"; import ps from "promise-streams"; import type { Stream, Writable } from "stream"; -import * as tar from "tar"; +import { x as tarX } from "tar"; import unzip from "unzip-stream"; -export class Downloader { - static bar: progress.MultiBar | null; +const progressBar: progress.MultiBar = new progress.MultiBar( + { + stopOnComplete: true, + }, + progress.Presets.shades_classic, +); - static createProgressBar() { - if (Downloader.bar == null) { - Downloader.bar = new progress.MultiBar( - { - clearOnComplete: true, - }, - progress.Presets.shades_grey, - ); - } - return Downloader.bar.create(0, 0); - } - - static releaseProgressBar(bar: progress.SingleBar) { - bar.stop(); - if (Downloader.bar != null) { - Downloader.bar.remove(bar); - if (!Downloader.bar.isActive) { - Downloader.bar.stop(); - Downloader.bar = null; +export const download = async (url: string, dest: string, strip = 1, executable = false): Promise => { + fse.ensureDirSync(dest); + const bar = progressBar.create(0, 0); + let stream: Stream = got.stream(url); + stream = stream + .on("request", (r: Request) => { + bar.start(1, 0); + }) + .on("downloadProgress", (p: Progress) => { + if (p.total) { + bar.setTotal(p.total); } - } + bar.update(p.transferred); + if (p.total === p.transferred) { + bar.stop(); + progressBar.remove(bar); + } + }); + if (url.endsWith(".zip")) { + stream = stream.pipe(unzip.Parse()).on("entry", (entry: any) => { + const filePath = entry.path.split("/").slice(strip).join(path.sep); + if (filePath.length > 0) { + if (entry.type === "Directory") { + fse.ensureDirSync(path.join(dest, path.normalize(filePath))); + } else if (entry.type === "File") { + const fullFilePath = path.join(dest, path.normalize(filePath)); + const dirPath = path.dirname(fullFilePath); + if (!fs.existsSync(dirPath)) { + fse.ensureDirSync(dirPath); + } + entry.pipe( + fs.createWriteStream(path.join(dest, path.normalize(filePath)), { + // ToDo: zip entry file mode? + mode: executable ? 0o755 : undefined, + }), + ); + } + } else { + entry.autodrain(); + } + }); + } else if (url.endsWith("tar.gz")) { + stream = stream.pipe(tarX({ C: dest, strip }) as unknown as Writable); + } else { + stream = stream.pipe(fs.createWriteStream(path.join(dest, url.split("/").pop()!))); } + return ps.wait(stream); +}; - static download(url: string, dest: string, strip = 1, executable = false) { - fse.ensureDirSync(dest); - const bar = Downloader.createProgressBar(); - let stream: Stream = got.stream(url); - stream = stream - .on("request", (r: any) => { - if (bar) bar.start(1, 0); - }) - .on("downloadProgress", (p: any) => { - if (bar) { - bar.setTotal(p.total); - bar.update(p.transferred); - if (p.total === p.transferred) { - Downloader.releaseProgressBar(bar); - } - } - }); - if (url.endsWith(".zip")) { - stream = stream.pipe(unzip.Parse()).on("entry", (entry: any) => { - const filePath = entry.path.split("/").slice(strip).join(path.sep); - if (filePath.length > 0) { - if (entry.type === "Directory") { - fse.ensureDirSync(path.join(dest, path.normalize(filePath))); - } else if (entry.type === "File") { - const fullFilePath = path.join(dest, path.normalize(filePath)); - const dirPath = path.dirname(fullFilePath); - if (!fs.existsSync(dirPath)) { - fse.ensureDirSync(dirPath); - } - entry.pipe( - fs.createWriteStream(path.join(dest, path.normalize(filePath)), { - // ToDo: zip entry file mode? - mode: executable ? 0o755 : undefined, - }), - ); - } - } else { - entry.autodrain(); - } - }); - } else if (url.endsWith("tar.gz")) { - stream = stream.pipe(tar.x({ C: dest, strip }) as unknown as Writable); - } else { - stream = stream.pipe(fs.createWriteStream(path.join(dest, url.split("/").pop()!))); - } - return ps.wait(stream); - } -} +export default download; diff --git a/src/common/output.ts b/src/common/output.ts index 096df06..206e751 100644 --- a/src/common/output.ts +++ b/src/common/output.ts @@ -52,4 +52,6 @@ export class OutputLogger extends Logger { export const OUTPUT_LOGGER = new OutputLogger(); -export default (line: string) => OUTPUT_LOGGER.log(line); +export const output = (line: string) => OUTPUT_LOGGER.log(line); + +export default output; diff --git a/src/core/sdk.ts b/src/core/sdk.ts index 47951f6..83dd550 100755 --- a/src/core/sdk.ts +++ b/src/core/sdk.ts @@ -1,6 +1,6 @@ import os from "os"; import path from "path"; -import { Downloader } from "../common/download"; +import download from "../common/download"; import { Logger } from "../common/log"; export class Sdk { @@ -55,7 +55,7 @@ export class Sdk { return Promise.resolve(); } else { this.log.i("download: *%s*", this.link); - return Downloader.download(this.link, this.path, strip); + return download(this.link, this.path, strip); } } } diff --git a/src/impl/android.ts b/src/impl/android.ts index 88dc38a..76886a6 100755 --- a/src/impl/android.ts +++ b/src/impl/android.ts @@ -2,7 +2,7 @@ import fs from "fs"; import os from "os"; import path from "path"; import through from "through2"; -import { Downloader } from "../common/download"; +import download from "../common/download"; import { Env } from "../common/env"; import exec from "../common/exec"; import { System } from "../common/system"; @@ -65,7 +65,7 @@ export class Android extends Sdk { // ToDo: support windows const url = `${this.repository}/android-ndk-${this.ndk_version}-${System.os}-x86_64.zip`; this.log.d("download: *%s*", url); - return Downloader.download(url, this.ndk_home, 1, true); + return download(url, this.ndk_home, 1, true); } sdkmanager(packages: string[]) { diff --git a/src/index.ts b/src/index.ts index b1efbd4..a2daaf5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,8 @@ +export * from "./common/download"; +export * from "./common/log"; +export * from "./common/output"; export * from "./common/system"; export * from "./core/config"; -export * from "./common/download"; export * from "./core/sdk"; export * from "./core/type"; export * from "./impl/android"; diff --git a/test/download.test.js b/test/download.test.js index 35b97de..9fdaf3d 100644 --- a/test/download.test.js +++ b/test/download.test.js @@ -3,7 +3,7 @@ import { existsSync, mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test from "node:test"; -import { Downloader } from "../dist/index.mjs"; +import { download } from "../dist/index.mjs"; const resolveTargetDir = () => mkdtempSync(join(tmpdir(), "test-")); @@ -16,7 +16,7 @@ const TEST_DATA = { for (const [url, file] of Object.entries(TEST_DATA)) { test(`Test download: ${url}`, async () => { const target = resolveTargetDir(); - await Downloader.download(url, target); + await download(url, target); assert(existsSync(join(target, file))); }); }