Skip to content

Commit

Permalink
replace download with alternate package
Browse files Browse the repository at this point in the history
  • Loading branch information
itsspriyansh committed May 17, 2024
1 parent 387f5af commit e0a663b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
"axios": "^1.1.2",
"boxen": "5.1.2",
"cli-progress": "^3.11.2",
"decompress": "^4.2.1",
"dotenv": "^16.0.3",
"download": "^8.0.0",
"inquirer": "^8.2.4",
"minimist": "^1.2.6",
"node-downloader-helper": "^2.1.6",
"untildify": "^4.0.0",
"which": "^2.0.2"
}
Expand Down
46 changes: 29 additions & 17 deletions src/commands/android/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import colors from 'ansi-colors';
import axios, {AxiosResponse} from 'axios';
import cliProgress from 'cli-progress';
import download from 'download';
import decompress from 'decompress';
import {DownloaderHelper} from 'node-downloader-helper';
import fs from 'fs';
import os from 'os';
import path from 'path';
Expand Down Expand Up @@ -92,28 +93,39 @@ export const getBinaryLocation = (sdkRoot: string, platform: Platform, binaryNam
return '';
};

export const downloadWithProgressBar = async (url: string, dest: string, extract = false) => {
export const downloadWithProgressBar = async (url: string, dest: string, extract=false) => {
const absoluteFolderPath = path.resolve(dest);

if (!fs.existsSync(absoluteFolderPath)) {
fs.mkdirSync(absoluteFolderPath, { recursive: true });
}

const progressBar = new cliProgress.Bar({
format: ' [{bar}] {percentage}% | ETA: {eta}s'
}, cliProgress.Presets.shades_classic);

try {
const stream = download(url, dest, {
extract
});
progressBar.start(100, 0);

await stream.on('downloadProgress', function(progress) {
progressBar.update(progress.percent*100);
});
progressBar.stop();

return true;
} catch {
const downloader = new DownloaderHelper(url, dest, {override: {skip: true}});

downloader.on('start', () => progressBar.start(100, 0));
downloader.on('progress', (stats) => {
progressBar.update(stats.progress);
});
downloader.on('end', (downloadInfo) => {
console.log(downloadInfo);
if (extract) {
decompress(downloadInfo.filePath, dest).then(() => {
fs.unlink(downloadInfo.filePath, (err) => {
if (err) {
console.log(`Please delete the zip file at: ${downloadInfo.filePath}.`);
}
})
});
}
progressBar.stop();
});
downloader.on('error', () => progressBar.stop());

return false;
}
return await downloader.start();
};

export const getLatestVersion = async (browser: 'firefox' | 'chrome'): Promise<string> => {
Expand Down

0 comments on commit e0a663b

Please sign in to comment.