|
1 | | -const fetch = require('node-fetch'); |
| 1 | +const axios = require('axios').default; |
2 | 2 | const fs = require('fs'); |
3 | 3 | const path = require('path'); |
| 4 | +const stream = require('stream'); |
| 5 | +const util = require('util'); |
4 | 6 | const packageConfig = require('../package.json'); |
5 | 7 |
|
6 | 8 | /** |
@@ -44,20 +46,27 @@ if (localBinary) { |
44 | 46 | ? `https://repo.lightbend.com/raw/kalix/versions/${kalixCodegenVersion}/${releases[release]}.exe` |
45 | 47 | : `https://repo.lightbend.com/raw/kalix/versions/${kalixCodegenVersion}/${releases[release]}`; |
46 | 48 | console.info(`Fetching kalix-codegen-js from ${url}`); |
47 | | - fetch(url).then((response) => { |
48 | | - if (!response.ok) { |
49 | | - throw new Error( |
50 | | - `Error fetching Kalix codegen tool from [${url}]: ${response.statusText}.`, |
51 | | - ); |
52 | | - } |
53 | | - console.debug(`Saving to ${targetFile}`); |
54 | | - if (!fs.existsSync(binDir)) { |
55 | | - fs.mkdirSync(binDir); |
56 | | - } |
57 | 49 |
|
58 | | - const fileWriter = fs.createWriteStream(targetFile, { mode: 0o755 }); |
59 | | - response.body.pipe(fileWriter); |
60 | | - }); |
| 50 | + axios |
| 51 | + .get(url, { responseType: 'stream' }) |
| 52 | + .then((response) => { |
| 53 | + console.debug(`Saving to ${targetFile}`); |
| 54 | + if (!fs.existsSync(binDir)) { |
| 55 | + fs.mkdirSync(binDir); |
| 56 | + } |
| 57 | + |
| 58 | + const streamPipeline = util.promisify(stream.pipeline); |
| 59 | + const fileWriter = fs.createWriteStream(targetFile, { mode: 0o755 }); |
| 60 | + return streamPipeline(response.data, fileWriter); |
| 61 | + }) |
| 62 | + .catch((error) => { |
| 63 | + const failure = error.isAxiosError ? error.response.statusText : error; |
| 64 | + console.error('Failed to download Kalix codegen binary:', failure); |
| 65 | + if (fs.existsSync(targetFile)) { |
| 66 | + fs.rmSync(targetFile); |
| 67 | + } |
| 68 | + process.exit(1); |
| 69 | + }); |
61 | 70 | } else { |
62 | 71 | throw new Error( |
63 | 72 | 'Unsupported platform. No prebuilt version of the Kalix codegen tool exists for this platform.', |
|
0 commit comments