|
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'; |
@@ -92,28 +93,39 @@ export const getBinaryLocation = (sdkRoot: string, platform: Platform, binaryNam |
92 | 93 | return ''; |
93 | 94 | }; |
94 | 95 |
|
95 | | -export const downloadWithProgressBar = async (url: string, dest: string, extract = false) => { |
| 96 | +export const downloadWithProgressBar = async (url: string, dest: string, extract=false) => { |
| 97 | + const absoluteFolderPath = path.resolve(dest); |
| 98 | + |
| 99 | + if (!fs.existsSync(absoluteFolderPath)) { |
| 100 | + fs.mkdirSync(absoluteFolderPath, { recursive: true }); |
| 101 | + } |
| 102 | + |
96 | 103 | const progressBar = new cliProgress.Bar({ |
97 | 104 | format: ' [{bar}] {percentage}% | ETA: {eta}s' |
98 | 105 | }, cliProgress.Presets.shades_classic); |
99 | 106 |
|
100 | | - try { |
101 | | - const stream = download(url, dest, { |
102 | | - extract |
103 | | - }); |
104 | | - progressBar.start(100, 0); |
105 | | - |
106 | | - await stream.on('downloadProgress', function(progress) { |
107 | | - progressBar.update(progress.percent*100); |
108 | | - }); |
109 | | - progressBar.stop(); |
110 | | - |
111 | | - return true; |
112 | | - } catch { |
| 107 | + const downloader = new DownloaderHelper(url, dest, {override: {skip: true}}); |
| 108 | + |
| 109 | + downloader.on('start', () => progressBar.start(100, 0)); |
| 110 | + downloader.on('progress', (stats) => { |
| 111 | + progressBar.update(stats.progress); |
| 112 | + }); |
| 113 | + downloader.on('end', (downloadInfo) => { |
| 114 | + console.log(downloadInfo); |
| 115 | + if (extract) { |
| 116 | + decompress(downloadInfo.filePath, dest).then(() => { |
| 117 | + fs.unlink(downloadInfo.filePath, (err) => { |
| 118 | + if (err) { |
| 119 | + console.log(`Please delete the zip file at: ${downloadInfo.filePath}.`); |
| 120 | + } |
| 121 | + }) |
| 122 | + }); |
| 123 | + } |
113 | 124 | progressBar.stop(); |
| 125 | + }); |
| 126 | + downloader.on('error', () => progressBar.stop()); |
114 | 127 |
|
115 | | - return false; |
116 | | - } |
| 128 | + return await downloader.start(); |
117 | 129 | }; |
118 | 130 |
|
119 | 131 | export const getLatestVersion = async (browser: 'firefox' | 'chrome'): Promise<string> => { |
|
0 commit comments