import sharp from "sharp"; import { mkdir, writeFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; const assets = new URL("./assets/", import.meta.url); await mkdir(assets, { recursive: true }); const source = fileURLToPath(new URL("../app/icon.png", import.meta.url)); await sharp(source) .resize(512, 512, { kernel: "nearest" }) .png() .toFile(fileURLToPath(new URL("icon.png", assets))); const sizes = [16, 24, 32, 48, 64, 128, 256]; const images = await Promise.all( sizes.map((size) => sharp(source).resize(size, size, { kernel: "nearest" }).png().toBuffer(), ), ); const header = Buffer.alloc(6 + 16 * sizes.length); header.writeUInt16LE(1, 2); header.writeUInt16LE(sizes.length, 4); let offset = header.length; sizes.forEach((size, index) => { const entry = 6 + index * 16; header[entry] = size === 256 ? 0 : size; header[entry + 1] = header[entry]; header.writeUInt16LE(1, entry + 4); header.writeUInt16LE(32, entry + 6); header.writeUInt32LE(images[index].length, entry + 8); header.writeUInt32LE(offset, entry + 12); offset += images[index].length; }); await writeFile( new URL("icon.ico", assets), Buffer.concat([header, ...images]), );