Skip to content

Commit

Permalink
[ci] added retry when downloading form jspm
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtieReus committed Nov 2, 2023
1 parent 6612b6b commit 317076c
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions ci/scripts/generate_importmap.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -239,28 +239,40 @@ const installPackage = async (name) => {
* @param {string} path
* @returns void
*/
const downloadFile = (url, path) => {
const downloadFile = (url, path, maxRetries = 5, currentRetry = 0) => {
return new Promise((resolve, reject) => {
fs.mkdirSync(pathLib.dirname(path), { recursive: true })
const file = fs.createWriteStream(path)
https
.get(url, (response) => {
response.pipe(file)
console.log(
"[generate_importmap]::downloadFile: success with url:",
url
const req = https.get(url, (response) => {
console.log("[generate_importmap]::downloadFile: success with url:", url)
response.pipe(file)
// The whole response has been received. Print out the result.
response.on("end", () => {
file.close()
resolve()
})
})

req.on("error", (error) => {
if (currentRetry < maxRetries) {
console.error(
`[generate_importmap]::downloadFile: attempt ${
currentRetry + 1
} failed. Retrying...`
)
// Retry after a delay (you can adjust the delay as needed)
setTimeout(() => {
resolve(downloadFile(url, path, maxRetries, currentRetry + 1))
}, 1000) // 1000 milliseconds (1 second) delay in this example
} else {
// Maximum retries reached, reject the promise
reject(
`[generate_importmap]::downloadFile: max retries (${maxRetries}) reached. Error: ${error.message}`
)
}
})

// The whole response has been received. Print out the result.
response.on("end", () => {
file.close()
resolve()
})
})
.on("error", (err) => {
console.log("[generate_importmap]::downloadFile: error with url:", url)
reject(err)
})
req.end()
})
}

Expand Down

0 comments on commit 317076c

Please sign in to comment.