nguhmap/devscripts/optimise_assets.ts
2025-09-15 15:44:59 +02:00

25 lines
992 B
TypeScript

import { sleep, spawn } from "bun";
import { readdir } from "node:fs/promises"
let cur_subprocs = 0;
const max_subprocs = (process.argv.length >= 3 && process.argv[2] != undefined) ? +process.argv[2] : 8
const assets_dir = "./static/"
let assets_files = (await readdir(assets_dir, {recursive: true})).map(it => assets_dir + it)
console.log(assets_files)
for (const index in assets_files)
{
const file = assets_files[index]
if (file == undefined) continue;
if (file.match(/\.png$/)) {
while (cur_subprocs >= max_subprocs) {await sleep(200)}
cur_subprocs++
console.log(`optimizing ${file} (${index} / ${assets_files.length}): ${cur_subprocs} processes running`)
spawn(["optipng", "-o7", file], {
onExit: () => {cur_subprocs--},
stdout: "ignore",
stderr: "ignore"
})
} else {
console.log(`Ignoring file ${file}: not an optimisable filetype`)
}
}
while (cur_subprocs > 0) {await sleep(1000)}