// 睿新致遠 / NOVALON · Gitea 品牌资产栅格化 // 用仓库 node_modules 的 sharp(package.json 已声明 ^0.35.3),不引入新依赖。 // 判据:输出尺寸必须与上游同名资产实测尺寸一致(logo.png 512×512、avatar_default.png 200×200), // 且写完必须回读 —— 只数退出码不算通过。 const fs = require('fs'); const path = require('path'); const sharp = require('sharp'); const [SRC, OUT] = process.argv.slice(2); if (!SRC || !OUT) { console.error('用法: node build-raster.cjs <输出 img 目录>'); process.exit(2); } const JOBS = [ // 头像默认图:avatar-standard(full 字面 0.80,圆内不裁切)→ 上游 200×200 { in: 'avatar-standard.svg', size: 200, out: 'avatar_default.png' }, // Open Graph 图(head_opengraph.tmpl 固定引用 /img/logo.png)→ 上游 512×512 { in: 'seal-master.svg', size: 512, out: 'logo.png' }, ]; async function main() { fs.mkdirSync(OUT, { recursive: true }); const report = []; for (const job of JOBS) { const from = path.join(SRC, job.in); const to = path.join(OUT, job.out); await sharp(from, { density: 300 }) .resize(job.size, job.size, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } }) .png({ compressionLevel: 9 }) .toFile(to); const meta = await sharp(to).metadata(); if (meta.width !== job.size || meta.height !== job.size) { throw new Error(`${job.out} 回读尺寸 ${meta.width}×${meta.height} ≠ ${job.size}`); } report.push(`${job.out} ${meta.width}x${meta.height} ${meta.channels}ch ${fs.statSync(to).size}B`); } console.log(report.join('\n')); } main().catch((e) => { console.error('栅格化失败:', e.message); process.exit(1); });