From 05e1398c36519454ccd23c35ee8ad4fdf0740523 Mon Sep 17 00:00:00 2001 From: Vitaly Gashkov Date: Thu, 12 Dec 2024 20:56:52 +0500 Subject: [PATCH] refactor: use molnia for shaka downloading --- apps/cli | 2 +- package-lock.json | 3 ++- packages/core/lib/bin.ts | 30 ++++++++++-------------------- packages/core/package.json | 1 + 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/apps/cli b/apps/cli index ce8929b..b34b028 160000 --- a/apps/cli +++ b/apps/cli @@ -1 +1 @@ -Subproject commit ce8929b6a8ad2833e6eca774bd0627cf47282541 +Subproject commit b34b028d8f8aa1c94ae3a31e56d740a0174c7113 diff --git a/package-lock.json b/package-lock.json index 5d8ad45..422538a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,7 +25,7 @@ }, "apps/cli": { "name": "@streamyx/cli", - "version": "4.2.1", + "version": "4.2.2", "hasInstallScript": true, "dependencies": { "@inquirer/prompts": "^7.2.0", @@ -18674,6 +18674,7 @@ "@streamyx/logger": "^0.0.2", "dasha": "^3.1.1", "got-scraping": "^3.2.15", + "molnia": "^0.0.15", "undici": "^6.21.0" }, "devDependencies": { diff --git a/packages/core/lib/bin.ts b/packages/core/lib/bin.ts index 92a0f6b..6230260 100644 --- a/packages/core/lib/bin.ts +++ b/packages/core/lib/bin.ts @@ -3,6 +3,7 @@ import { ChildProcessWithoutNullStreams, spawn } from 'node:child_process'; import { chmodSync } from 'node:fs'; import { delimiter } from 'node:path'; import { stat } from 'node:fs/promises'; +import { download } from 'molnia'; import { logger } from './log'; import { fs } from './fs'; import { getSettings } from './settings'; @@ -93,25 +94,6 @@ const getGitHubAssetUrl = ({ repo, version, asset }: GitHubUrlOptions) => { return `https://github.com/${repo}/releases/download/${version}/${asset}`; }; -export const download = async (url: string, outputPath: string) => { - // Curl args: - const args = [ - '-L', // follow redirects - '-f', // fail if the request fails - // output destination: - '-o', - outputPath, - '--show-error', // show errors - '--silent', // but no progress bar - url, - ]; - // Now fetch the binary and fail the script if that fails: - logger.debug(`Downloading ${url} to ${outputPath}`); - const process = spawn('curl', args, { stdio: 'inherit' }); - const code = await new Promise((resolve) => process.on('exit', resolve)); - if (code != 0) throw new Error('Download of ' + url + ' failed: ' + code); -}; - interface DownloadBinaryOptions extends Omit { id: string; assets: BinaryNamesByPlatform; @@ -136,7 +118,15 @@ export const fetchGitHubAsset = async ({ id, assets, repo, version }: DownloadBi const output = fs.join(getSettings().binariesDir, name); downloads.state.set(id, 'downloading'); const url = getGitHubAssetUrl({ repo, version, asset: name }); - await download(url, output); + logger.debug(`Downloading ${url} to ${output}`); + await download(url, { + output, + onError: (e) => logger.error(e.message), + onProgress: (progress) => { + logger.debug(progress.toString()); + }, + }); + logger.debug(`Downloaded: ${output}`); chmodSync(output, 0o755); downloads.paths.set(id, output); downloads.state.set(id, 'finished'); diff --git a/packages/core/package.json b/packages/core/package.json index faba349..5b99155 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -27,6 +27,7 @@ "@streamyx/logger": "^0.0.2", "dasha": "^3.1.1", "got-scraping": "^3.2.15", + "molnia": "^0.0.15", "undici": "^6.21.0" }, "devDependencies": {