Skip to content

Commit 2a62bb0

Browse files
committed
replace download with alternate package
1 parent 387f5af commit 2a62bb0

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-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: 42 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,53 @@ 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+
// Check if the destination directory exists, if not, create it
101+
if (!fs.existsSync(absoluteFolderPath)) {
102+
fs.mkdirSync(absoluteFolderPath, { recursive: true });
103+
}
104+
96105
const progressBar = new cliProgress.Bar({
97106
format: ' [{bar}] {percentage}% | ETA: {eta}s'
98107
}, cliProgress.Presets.shades_classic);
99108

100-
try {
101-
const stream = download(url, dest, {
102-
extract
109+
const downloader = new DownloaderHelper(url, dest, { override: { skip: true } });
110+
111+
downloader.on('start', () => progressBar.start(100, 0));
112+
downloader.on('progress', (stats) => {
113+
progressBar.update(stats.progress);
114+
});
115+
116+
// Return a new promise to handle the asynchronous operation of decompressing the installed zip file.
117+
return new Promise((resolve, reject) => {
118+
downloader.on('end', async (downloadInfo) => {
119+
progressBar.stop();
120+
if (extract) {
121+
try {
122+
await decompress(downloadInfo.filePath, dest);
123+
// remove the zip file after extraction
124+
await fsP.unlink(downloadInfo.filePath);
125+
resolve(true);
126+
} catch (error) {
127+
console.error(`Error during decompression: ${error}`);
128+
reject(error);
129+
}
130+
} else {
131+
resolve(true);
132+
}
103133
});
104-
progressBar.start(100, 0);
105134

106-
await stream.on('downloadProgress', function(progress) {
107-
progressBar.update(progress.percent*100);
135+
downloader.on('error', (error) => {
136+
progressBar.stop();
137+
reject(error);
108138
});
109-
progressBar.stop();
110139

111-
return true;
112-
} catch {
113-
progressBar.stop();
114-
115-
return false;
116-
}
140+
downloader.start().catch((error) => {
141+
progressBar.stop();
142+
reject(error);
143+
});
144+
});
117145
};
118146

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

0 commit comments

Comments
 (0)