- Introduced `generate-icons.mjs` script to automate the creation of PWA icons and favicons from the provided SVG logo. - Generated icons include: - PWA icons: 192x192 and 512x512 - Maskable icon with 12% padding: 512x512 - Favicons: 32x32, 16x16 - Apple-touch icon: 180x180 - Added a new PNG file for the maskable icon.
59 lines
1.8 KiB
JavaScript
59 lines
1.8 KiB
JavaScript
// Generates PWA icons and favicons from public/jei-logo.svg
|
|
// Requires: sharp (devDependency)
|
|
|
|
import fs from 'node:fs/promises'
|
|
import path from 'node:path'
|
|
import sharp from 'sharp'
|
|
|
|
const root = path.resolve(process.cwd())
|
|
const candidatePng = path.join(root, 'public', 'brand.png')
|
|
const srcSvg = path.join(root, 'public', 'jei-logo.svg')
|
|
let srcImage = srcSvg
|
|
try {
|
|
const stat = await fs.stat(candidatePng)
|
|
if (stat.isFile()) srcImage = candidatePng
|
|
} catch {}
|
|
const outDir = path.join(root, 'public')
|
|
const iconsDir = path.join(outDir, 'icons')
|
|
|
|
async function ensureDir(dir) {
|
|
await fs.mkdir(dir, { recursive: true })
|
|
}
|
|
|
|
async function makePng(size, outPath, padding = 0) {
|
|
// Create a square canvas and fit the SVG inside with optional padding
|
|
const inner = Math.round(size * (1 - padding))
|
|
const background = { r: 0, g: 0, b: 0, alpha: 0 } // transparent
|
|
const buf = await sharp(srcImage)
|
|
.resize(inner, inner, { fit: 'contain', background })
|
|
.png()
|
|
.toBuffer()
|
|
|
|
// Composite on a square canvas to ensure exact size
|
|
await sharp({ create: { width: size, height: size, channels: 4, background } })
|
|
.composite([{ input: buf, gravity: 'center' }])
|
|
.png()
|
|
.toFile(outPath)
|
|
}
|
|
|
|
async function run() {
|
|
await ensureDir(iconsDir)
|
|
|
|
// PWA icons
|
|
await makePng(192, path.join(iconsDir, 'icon-192x192.png'))
|
|
await makePng(512, path.join(iconsDir, 'icon-512x512.png'))
|
|
// Maskable: add ~12% padding
|
|
await makePng(512, path.join(iconsDir, 'maskable-512x512.png'), 0.12)
|
|
|
|
// Favicons and apple-touch icon
|
|
await makePng(32, path.join(outDir, 'favicon-32x32.png'))
|
|
await makePng(16, path.join(outDir, 'favicon-16x16.png'))
|
|
await makePng(180, path.join(outDir, 'apple-touch-icon-180x180.png'))
|
|
|
|
console.log('Icons generated in public/ and public/icons/')
|
|
}
|
|
|
|
run().catch((err) => {
|
|
console.error(err)
|
|
process.exit(1)
|
|
})
|