import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const projectRoot = path.resolve(__dirname, ".."); const sourcePngPath = path.join(projectRoot, "src-tauri", "icons", "128x128@2x.png"); const targetIcoPath = path.join(projectRoot, "src-tauri", "icons", "icon.ico"); const pngBytes = fs.readFileSync(sourcePngPath); if (pngBytes.length < 24 || pngBytes.toString("ascii", 1, 4) !== "PNG") { throw new Error(`Expected a PNG file at ${sourcePngPath}`); } const width = pngBytes.readUInt32BE(16); const height = pngBytes.readUInt32BE(20); if (width !== 256 || height !== 256) { throw new Error(`Expected a 256x256 PNG, received ${width}x${height}`); } const headerSize = 6; const directoryEntrySize = 16; const imageOffset = headerSize + directoryEntrySize; const icoBytes = Buffer.alloc(imageOffset + pngBytes.length); icoBytes.writeUInt16LE(0, 0); icoBytes.writeUInt16LE(1, 2); icoBytes.writeUInt16LE(1, 4); icoBytes.writeUInt8(0, 6); icoBytes.writeUInt8(0, 7); icoBytes.writeUInt8(0, 8); icoBytes.writeUInt8(0, 9); icoBytes.writeUInt16LE(1, 10); icoBytes.writeUInt16LE(32, 12); icoBytes.writeUInt32LE(pngBytes.length, 14); icoBytes.writeUInt32LE(imageOffset, 18); pngBytes.copy(icoBytes, imageOffset); fs.writeFileSync(targetIcoPath, icoBytes); console.log(`Generated ${path.relative(projectRoot, targetIcoPath)} from ${path.relative(projectRoot, sourcePngPath)}`);