Skip to content

Instantly share code, notes, and snippets.

@OMGZui
Created November 18, 2022 09:41
Show Gist options
  • Save OMGZui/63af5292050f959a567475f8be08a278 to your computer and use it in GitHub Desktop.
Save OMGZui/63af5292050f959a567475f8be08a278 to your computer and use it in GitHub Desktop.
process.on('message', (tasks: imageType[]) => {
;(async () => {
const data = tasks
.filter(({ path }: { path: string }) => /\.(jpe?g|png)$/.test(path))
.map(ele => {
return compressImg({ ...ele, file: Buffer.from(ele.file) })
})
const svgaData = tasks
.filter(({ path }: { path: string }) => /\.(svga)$/.test(path))
.map(ele => {
return compressSvga(ele.path, Buffer.from(ele.file))
})
const details = await Promise.all([
...data.map(fn => fn()),
...svgaData.map(fn => fn())
])
await Promise.all(
details.map(
({ path, file }) =>
new Promise((resolve, reject) => {
fs.writeFile(path, file, err => {
if (err) reject(err)
resolve(true)
})
})
)
)
if (process.send) {
process.send(details)
}
})()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment