|
1 | 1 | import colors from 'ansi-colors'; |
2 | 2 | import axios, {AxiosResponse} from 'axios'; |
3 | 3 | import cliProgress from 'cli-progress'; |
4 | | -import download from 'download'; |
| 4 | +import decompress from 'decompress'; |
| 5 | +import {DownloaderHelper} from 'node-downloader-helper'; |
5 | 6 | import fs from 'fs'; |
6 | 7 | import os from 'os'; |
7 | 8 | import path from 'path'; |
8 | 9 | import which from 'which'; |
| 10 | +import fsP from 'fs/promises'; |
9 | 11 |
|
10 | 12 | import {symbols} from '../../../utils'; |
11 | 13 | 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 |
93 | 95 | }; |
94 | 96 |
|
95 | 97 | 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 | + |
96 | 105 | const progressBar = new cliProgress.Bar({ |
97 | 106 | format: ' [{bar}] {percentage}% | ETA: {eta}s' |
98 | 107 | }, cliProgress.Presets.shades_classic); |
99 | 108 |
|
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 | + } |
103 | 133 | }); |
104 | | - progressBar.start(100, 0); |
105 | 134 |
|
106 | | - await stream.on('downloadProgress', function(progress) { |
107 | | - progressBar.update(progress.percent*100); |
| 135 | + downloader.on('error', (error) => { |
| 136 | + progressBar.stop(); |
| 137 | + reject(error); |
108 | 138 | }); |
109 | | - progressBar.stop(); |
110 | 139 |
|
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 | + }); |
117 | 145 | }; |
118 | 146 |
|
119 | 147 | export const getLatestVersion = async (browser: 'firefox' | 'chrome'): Promise<string> => { |
|
0 commit comments