const sharp = require("sharp");
const fs = require("fs");
sharp.concurrency(1);
async function run(job) {
let img = sharp(job.input);
if (job.mode === "resize") {
img = img.resize(job.maxW, job.maxH, { fit: "inside", withoutEnlargement: true });
}
const buf = await img
.jpeg({ quality: job.quality, mozjpeg: !!job.mozjpeg })
.toBuffer();
fs.writeFileSync(job.out, buf);
}
(async () => {
const jobs = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
const POOL = 8;
let i = 0;
await Promise.all(
Array.from({ length: POOL }, async () => {
while (i < jobs.length) {
const job = jobs[i++];
try {
await run(job);
} catch (e) {
console.error("FAIL", job.out, e.message);
process.exitCode = 1;
}
}
})
);
})();