| 243 | * @param {string} destPath Destination file path on local filesystem. |
| 244 | */ |
| 245 | export async function maybeDownload(sourceURL, destPath) { |
| 246 | const fs = require('fs'); |
| 247 | return new Promise(async (resolve, reject) => { |
| 248 | if (!fs.existsSync(destPath) || fs.lstatSync(destPath).size === 0) { |
| 249 | const localZipFile = fs.createWriteStream(destPath); |
| 250 | console.log(`Downloading file from ${sourceURL} to ${destPath}...`); |
| 251 | https.get(sourceURL, response => { |
| 252 | response.pipe(localZipFile); |
| 253 | localZipFile.on('finish', () => { |
| 254 | localZipFile.close(() => resolve()); |
| 255 | }); |
| 256 | localZipFile.on('error', err => reject(err)); |
| 257 | }); |
| 258 | } else { |
| 259 | return resolve(); |
| 260 | } |
| 261 | }); |
| 262 | } |