Skip to content

Commit 43f08fb

Browse files
committed
replace download with alternate package
1 parent 387f5af commit 43f08fb

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
"axios": "^1.1.2",
4747
"boxen": "5.1.2",
4848
"cli-progress": "^3.11.2",
49+
"decompress": "^4.2.1",
4950
"dotenv": "^16.0.3",
5051
"download": "^8.0.0",
5152
"inquirer": "^8.2.4",
5253
"minimist": "^1.2.6",
54+
"node-downloader-helper": "^2.1.6",
5355
"untildify": "^4.0.0",
5456
"which": "^2.0.2"
5557
}

src/commands/android/utils/common.ts

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import colors from 'ansi-colors';
22
import axios, {AxiosResponse} from 'axios';
33
import cliProgress from 'cli-progress';
4-
import download from 'download';
4+
import decompress from 'decompress';
5+
import {DownloaderHelper} from 'node-downloader-helper';
56
import fs from 'fs';
67
import os from 'os';
78
import path from 'path';
89
import which from 'which';
10+
import fsP from 'fs/promises';
911

1012
import {symbols} from '../../../utils';
1113
import {ABI, AVAILABLE_OPTIONS, DEFAULT_CHROME_VERSIONS, DEFAULT_FIREFOX_VERSION, SDK_BINARY_LOCATIONS} from '../constants';
@@ -93,27 +95,50 @@ export const getBinaryLocation = (sdkRoot: string, platform: Platform, binaryNam
9395
};
9496

9597
export const downloadWithProgressBar = async (url: string, dest: string, extract = false) => {
98+
const absoluteFolderPath = path.resolve(dest);
99+
100+
if (!fs.existsSync(absoluteFolderPath)) {
101+
fs.mkdirSync(absoluteFolderPath, { recursive: true });
102+
}
103+
96104
const progressBar = new cliProgress.Bar({
97105
format: ' [{bar}] {percentage}% | ETA: {eta}s'
98106
}, cliProgress.Presets.shades_classic);
99107

100-
try {
101-
const stream = download(url, dest, {
102-
extract
108+
const downloader = new DownloaderHelper(url, dest, { override: { skip: true } });
109+
110+
downloader.on('start', () => progressBar.start(100, 0));
111+
downloader.on('progress', (stats) => {
112+
progressBar.update(stats.progress);
113+
});
114+
115+
return new Promise((resolve, reject) => {
116+
downloader.on('end', async (downloadInfo) => {
117+
progressBar.stop();
118+
if (extract) {
119+
try {
120+
await decompress(downloadInfo.filePath, dest);
121+
await fsP.unlink(downloadInfo.filePath);
122+
resolve(true);
123+
} catch (error) {
124+
console.error(`Error during decompression: ${error}`);
125+
reject(error);
126+
}
127+
} else {
128+
resolve(true);
129+
}
103130
});
104-
progressBar.start(100, 0);
105131

106-
await stream.on('downloadProgress', function(progress) {
107-
progressBar.update(progress.percent*100);
132+
downloader.on('error', (error) => {
133+
progressBar.stop();
134+
reject(error);
108135
});
109-
progressBar.stop();
110136

111-
return true;
112-
} catch {
113-
progressBar.stop();
114-
115-
return false;
116-
}
137+
downloader.start().catch((error) => {
138+
progressBar.stop();
139+
reject(error);
140+
});
141+
});
117142
};
118143

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

0 commit comments

Comments
 (0)