|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Generates assets/atomic-wind/icons.json from the individual SVG files in |
| 4 | + * assets/atomic-wind/icons/. |
| 5 | + * |
| 6 | + * Each key is the icon name (filename without .svg). |
| 7 | + * Each value is the minified inner SVG markup — the content between <svg> and </svg>. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * node bin/generate-icons-json.mjs |
| 11 | + * node bin/generate-icons-json.mjs --icons-dir path/to/icons --out path/to/output.json |
| 12 | + */ |
| 13 | + |
| 14 | +import { readFileSync, writeFileSync, readdirSync } from 'fs'; |
| 15 | +import { join, basename, dirname } from 'path'; |
| 16 | +import { fileURLToPath } from 'url'; |
| 17 | + |
| 18 | +const ROOT = join( dirname( fileURLToPath( import.meta.url ) ), '..' ); |
| 19 | + |
| 20 | +function parseArgs() { |
| 21 | + const args = process.argv.slice( 2 ); |
| 22 | + const opts = { |
| 23 | + iconsDir: join( ROOT, 'assets/atomic-wind/icons' ), |
| 24 | + out: join( ROOT, 'assets/atomic-wind/icons.json' ), |
| 25 | + }; |
| 26 | + for ( let i = 0; i < args.length; i++ ) { |
| 27 | + if ( args[ i ] === '--icons-dir' ) { opts.iconsDir = args[ ++i ]; } |
| 28 | + if ( args[ i ] === '--out' ) { opts.out = args[ ++i ]; } |
| 29 | + } |
| 30 | + return opts; |
| 31 | +} |
| 32 | + |
| 33 | +function extractInner( svgText ) { |
| 34 | + const match = svgText.match( /<svg[^>]*>([\s\S]*?)<\/svg>/i ); |
| 35 | + if ( ! match ) { return null; } |
| 36 | + // Collapse all runs of whitespace (newlines, tabs, multiple spaces) to a single space, |
| 37 | + // then trim and remove spaces around > and <. |
| 38 | + return match[ 1 ] |
| 39 | + .replace( /\s+/g, ' ' ) |
| 40 | + .replace( />\s+</g, '><' ) |
| 41 | + .trim(); |
| 42 | +} |
| 43 | + |
| 44 | +function main() { |
| 45 | + const { iconsDir, out } = parseArgs(); |
| 46 | + |
| 47 | + const files = readdirSync( iconsDir ) |
| 48 | + .filter( ( f ) => f.endsWith( '.svg' ) ) |
| 49 | + .sort(); |
| 50 | + |
| 51 | + if ( files.length === 0 ) { |
| 52 | + console.error( `No SVG files found in ${ iconsDir }` ); |
| 53 | + process.exit( 1 ); |
| 54 | + } |
| 55 | + |
| 56 | + const icons = {}; |
| 57 | + let skipped = 0; |
| 58 | + |
| 59 | + for ( const file of files ) { |
| 60 | + const name = basename( file, '.svg' ); |
| 61 | + const svg = readFileSync( join( iconsDir, file ), 'utf8' ); |
| 62 | + const inner = extractInner( svg ); |
| 63 | + if ( inner === null ) { |
| 64 | + console.warn( ` skipped (no <svg> found): ${ file }` ); |
| 65 | + skipped++; |
| 66 | + continue; |
| 67 | + } |
| 68 | + icons[ name ] = inner; |
| 69 | + } |
| 70 | + |
| 71 | + writeFileSync( out, JSON.stringify( icons ), 'utf8' ); |
| 72 | + |
| 73 | + const sizeKB = ( Buffer.byteLength( JSON.stringify( icons ), 'utf8' ) / 1024 ).toFixed( 1 ); |
| 74 | + console.log( `icons.json written: ${ Object.keys( icons ).length } icons, ${ sizeKB } KB${ skipped ? ` (${ skipped } skipped)` : '' }` ); |
| 75 | + console.log( ` -> ${ out }` ); |
| 76 | +} |
| 77 | + |
| 78 | +main(); |
0 commit comments