import { cleanupSVG, importDirectory, parseColors, runSVGO, } from '@iconify/tools' import { buildUtilsReadFile, buildUtilsWriteFile } from '../../utils' import { IconLog, SvgPrefix, iconSVGPath } from '../src' export async function generateSvgJSON(whiteList?: string[]) { // build the empty json file await buildUtilsWriteFile( iconSVGPath, JSON.stringify({ icons: {}, prefix: SvgPrefix }), ) // Import icons const iconSet = await importDirectory('.svg', { prefix: SvgPrefix, }) // Validate, clean up, fix palette and optimise await iconSet.forEach(async (name, type) => { if (type !== 'icon') return // white list limit if (whiteList && !whiteList.includes(name)) iconSet.remove(name) const svg = iconSet.toSVG(name) if (!svg) { // Invalid icon iconSet.remove(name) return } // Clean up and optimise icons try { // Cleanup icon code await cleanupSVG(svg) // Assume icon is monotone: replace color with currentColor, add if missing // If icon is not monotone, remove this code await parseColors(svg, { defaultColor: 'currentColor', // eslint-disable-next-line unused-imports/no-unused-vars callback: (attr, colorStr, color) => { // return !color || isEmptyColor(color) ? colorStr : 'currentColor' return colorStr || 'currentColor' }, }) // Optimise runSVGO(svg) } catch (err) { // Invalid icon console.error(`Error parsing ${name}:`, err) iconSet.remove(name) return } // Update icon iconSet.fromSVG(name, svg) }) const data = JSON.parse(await buildUtilsReadFile(iconSVGPath)) // Generate to icon list await iconSet.forEach((name) => { // auto remove width and height const body = String(iconSet.toString(name)) .replaceAll(/width="(.[0-9]*)"/gi, '') .replaceAll(/height="(.[0-9]*)"/gi, '') data.icons[name] = { body, } }) // write into json await buildUtilsWriteFile(iconSVGPath, JSON.stringify(data, null, 2)) IconLog( 'Icon SVG', `Detecting ${iconSet.count()} custom svg icon, writing into file: ${iconSVGPath}`, ) }