#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); // 检查是否安装了 sharp try { require.resolve('sharp'); } catch (e) { console.log('正在安装 sharp...'); execSync('npm install sharp', { stdio: 'inherit', cwd: __dirname }); } const sharp = require('sharp'); const iconsDir = path.join(__dirname, 'miniprogram', 'assets', 'icons'); const svgFiles = [ 'font-size-decrease', 'font-size-increase', 'choose-color', 'export', 'export-svg', 'export-png', 'font-icon', 'expand', 'selectall', 'unselectall', 'checkbox' ]; async function convertSvgToPng() { console.log('开始转换 SVG 为 PNG...\n'); for (const name of svgFiles) { const svgPath = path.join(iconsDir, `${name}.svg`); const pngPath = path.join(iconsDir, `${name}.png`); if (!fs.existsSync(svgPath)) { console.log(`⚠️ ${name}.svg 不存在,跳过`); continue; } try { await sharp(svgPath, { density: 300 }) .resize(128, 128, { fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } }) .png() .toFile(pngPath); console.log(`✅ ${name}.svg -> ${name}.png`); } catch (error) { console.error(`❌ 转换 ${name}.svg 失败:`, error.message); } } console.log('\n转换完成!'); } convertSvgToPng().catch(console.error);