import { readFileSync, writeFileSync, readdirSync } from 'fs' import { join, basename } from 'path' import { fileURLToPath } from 'url' const __dirname = fileURLToPath(new URL('.', import.meta.url)) const iconsDir = join(__dirname, '../src/assets/icons') const svgFiles = readdirSync(iconsDir) .filter(f => f.endsWith('.svg') && f !== 'sprite.svg') .sort() if (!svgFiles.length) { console.error('No SVG files found in', iconsDir) process.exit(1) } // Extract viewBox and inner content from each SVG file const symbols = svgFiles.map((file) => { const name = basename(file, '.svg') const raw = readFileSync(join(iconsDir, file), 'utf-8') const viewBox = (raw.match(/viewBox="([^"]+)"/) ?? [])[1] ?? '0 0 24 24' // Everything between and const inner = (raw.match(/]*>([\s\S]*?)<\/svg>/i) ?? [])[1] ?.trim() .replace(/\n\s*/g, '\n ') ?? '' return ` \n ${inner}\n ` }) // --- sprite.svg --- const sprite = ` ${symbols.join('\n')} ` writeFileSync(join(iconsDir, 'sprite.svg'), sprite) // --- icon-names.ts (IconName union type) --- const names = svgFiles.map(f => `'${basename(f, '.svg')}'`).join('\n | ') const meta = `// Generated by scripts/generate-sprite.js — do not edit manually export type IconName = | ${names} ` writeFileSync(join(iconsDir, 'icon-names.ts'), meta) console.log(`✓ sprite.svg — ${svgFiles.length} icons`) console.log('✓ icon-names.ts')