|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { mkdir, rm } from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import sharp from "sharp"; |
| 6 | + |
| 7 | +const SIZES = [16, 32, 64, 128, 256, 512]; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | +const ROOT = path.resolve(__dirname, ".."); |
| 12 | +const SOURCE = path.join(ROOT, "docs", "img", "logo.webp"); |
| 13 | +const BUILD_DIR = path.join(ROOT, "build"); |
| 14 | +const ICONSET_DIR = path.join(BUILD_DIR, "icon.iconset"); |
| 15 | +const PNG_OUTPUT = path.join(BUILD_DIR, "icon.png"); |
| 16 | +const ICNS_OUTPUT = path.join(BUILD_DIR, "icon.icns"); |
| 17 | + |
| 18 | +const args = new Set(process.argv.slice(2)); |
| 19 | +if (args.size === 0) { |
| 20 | + args.add("png"); |
| 21 | + args.add("icns"); |
| 22 | +} |
| 23 | +const needsPng = args.has("png") || args.has("icns"); |
| 24 | +const needsIcns = args.has("icns"); |
| 25 | + |
| 26 | +async function generateIconPng() { |
| 27 | + await sharp(SOURCE).resize(512, 512).toFile(PNG_OUTPUT); |
| 28 | +} |
| 29 | + |
| 30 | +async function generateIconsetPngs() { |
| 31 | + await mkdir(ICONSET_DIR, { recursive: true }); |
| 32 | + |
| 33 | + const tasks = SIZES.flatMap((size) => { |
| 34 | + const outputs = [ |
| 35 | + { |
| 36 | + file: path.join(ICONSET_DIR, `icon_${size}x${size}.png`), |
| 37 | + dimension: size, |
| 38 | + }, |
| 39 | + ]; |
| 40 | + |
| 41 | + if (size <= 256) { |
| 42 | + const retina = size * 2; |
| 43 | + outputs.push({ |
| 44 | + file: path.join(ICONSET_DIR, `icon_${size}x${size}@2x.png`), |
| 45 | + dimension: retina, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + return outputs.map(({ file, dimension }) => |
| 50 | + sharp(SOURCE) |
| 51 | + .resize(dimension, dimension, { fit: "cover" }) |
| 52 | + .toFile(file), |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + await Promise.all(tasks); |
| 57 | +} |
| 58 | + |
| 59 | +async function generateIcns() { |
| 60 | + if (process.platform !== "darwin") { |
| 61 | + throw new Error("ICNS generation requires macOS (iconutil)"); |
| 62 | + } |
| 63 | + |
| 64 | + const proc = Bun.spawn([ |
| 65 | + "iconutil", |
| 66 | + "-c", |
| 67 | + "icns", |
| 68 | + ICONSET_DIR, |
| 69 | + "-o", |
| 70 | + ICNS_OUTPUT, |
| 71 | + ]); |
| 72 | + const status = await proc.exited; |
| 73 | + if (status !== 0) { |
| 74 | + throw new Error("iconutil failed to generate .icns file"); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +await mkdir(BUILD_DIR, { recursive: true }); |
| 79 | + |
| 80 | +if (needsPng) { |
| 81 | + await generateIconPng(); |
| 82 | +} |
| 83 | + |
| 84 | +if (needsIcns) { |
| 85 | + await generateIconsetPngs(); |
| 86 | + await generateIcns(); |
| 87 | +} |
| 88 | + |
| 89 | +await rm(ICONSET_DIR, { recursive: true, force: true }); |
0 commit comments