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 2a62bb0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 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
56 changes: 42 additions & 14 deletions src/commands/android/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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';
import which from 'which';
import fsP from 'fs/promises';

import {symbols} from '../../../utils';
import {ABI, AVAILABLE_OPTIONS, DEFAULT_CHROME_VERSIONS, DEFAULT_FIREFOX_VERSION, SDK_BINARY_LOCATIONS} from '../constants';
Expand Down Expand Up @@ -93,27 +95,53 @@ export const getBinaryLocation = (sdkRoot: string, platform: Platform, binaryNam
};

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

// Check if the destination directory exists, if not, create it
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
const downloader = new DownloaderHelper(url, dest, { override: { skip: true } });

downloader.on('start', () => progressBar.start(100, 0));
downloader.on('progress', (stats) => {
progressBar.update(stats.progress);
});

// Return a new promise to handle the asynchronous operation of decompressing the installed zip file.
return new Promise((resolve, reject) => {
downloader.on('end', async (downloadInfo) => {
progressBar.stop();
if (extract) {
try {
await decompress(downloadInfo.filePath, dest);
// remove the zip file after extraction
await fsP.unlink(downloadInfo.filePath);
resolve(true);
} catch (error) {
console.error(`Error during decompression: ${error}`);
reject(error);
}
} else {
resolve(true);
}
});
progressBar.start(100, 0);

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

return true;
} catch {
progressBar.stop();

return false;
}
downloader.start().catch((error) => {
progressBar.stop();
reject(error);
});
});
};

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

0 comments on commit 2a62bb0

Please sign in to comment.