Files
dating-app-frontend/scripts/generate-sprite.js
Oscar ffbfd772cb upd
2026-06-24 17:43:45 +03:00

50 lines
1.6 KiB
JavaScript

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 <svg ...> and </svg>
const inner = (raw.match(/<svg[^>]*>([\s\S]*?)<\/svg>/i) ?? [])[1]
?.trim()
.replace(/\n\s*/g, '\n ') ?? ''
return ` <symbol id="icon-${name}" viewBox="${viewBox}">\n ${inner}\n </symbol>`
})
// --- sprite.svg ---
const sprite = `<!-- Generated by scripts/generate-sprite.js — do not edit manually -->
<svg xmlns="http://www.w3.org/2000/svg" style="display:none">
${symbols.join('\n')}
</svg>
`
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')