* Helper function to download a file from a URL
({ url, dest }: DownloadOptions)
| 32 | * Helper function to download a file from a URL |
| 33 | */ |
| 34 | async function downloadFile({ url, dest }: DownloadOptions): Promise<void> { |
| 35 | await mkdir(path.dirname(dest), { recursive: true }); |
| 36 | |
| 37 | return new Promise((resolve, reject) => { |
| 38 | const request = https.get(url, (response) => { |
| 39 | if (response.statusCode === 302 || response.statusCode === 301) { |
| 40 | // Handle redirects |
| 41 | downloadFile({ url: response.headers.location!, dest }) |
| 42 | .then(resolve) |
| 43 | .catch(reject); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | if (!response.statusCode || response.statusCode !== 200) { |
| 48 | reject( |
| 49 | new DownloadError("Failed to download binary", response.statusCode), |
| 50 | ); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | const fileStream = fs.createWriteStream(dest); |
| 55 | |
| 56 | pipelineAsync(response, fileStream).then(resolve).catch(reject); |
| 57 | }); |
| 58 | |
| 59 | request.on("error", (error) => { |
| 60 | reject(new DownloadError(`Network error: ${error.message}`)); |
| 61 | }); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Trigger the erpc cli installation (downloading the right binary and checking it's checksum) |