Skip to content

Commit

Permalink
Handle stuck png compression promises
Browse files Browse the repository at this point in the history
  • Loading branch information
onyb committed Jan 9, 2024
1 parent 1c93b1e commit 8de1a13
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ async function stageEVMTokenImages(stagingDir, inputTokenFilePath) {
util.contractReplaceSvgToPng(outputTokenFilePath)
}

const withTimeout = (millis, promise) => {
let timeoutPid
const timeout = new Promise(
(resolve, reject) =>
(timeoutPid = setTimeout(
() => reject(`Timed out after ${millis} ms.`),
millis
))
)
return Promise.race([promise, timeout]).finally(() => {
if (timeoutPid) {
clearTimeout(timeoutPid);
}
})
}

async function compressPng(imagesDstPath, stagingDir) {
if (!fs.existsSync(stagingDir + '/images')) {
fs.mkdirSync(stagingDir + '/images')
Expand All @@ -120,17 +136,24 @@ async function compressPng(imagesDstPath, stagingDir) {
const files = await glob.glob(imagesDstPath + "/*.png")
console.log(`Compressing ${files.length} images...`)

for (let i = 0; i < files.length; i += 4) {
const chunk = files.slice(i, i + 4);
console.log(`Compress batch: ${chunk}`)
await imagemin(chunk, {
for (const file of files) {
console.log(`Compress: ${file}`)

const promise = imagemin([file], {
destination: stagingDir + "/images",
plugins: [
imageminPngquant({
quality: [0.1, 0.3],
}),
],
})

try {
await withTimeout(10000, promise)
} catch (err) {
console.log(`Failed to compress: ${file}`)
fs.copyFileSync(file, stagingDir + "/images/" + path.basename(file))
}
}
}

Expand Down

0 comments on commit 8de1a13

Please sign in to comment.