|
| 1 | +/** |
| 2 | + * Script to extract SVG badge icons from logos.tsx and encode them as base64 data URLs |
| 3 | + * Run with: bun run apps/api/scripts/encode-badge-icons.ts |
| 4 | + */ |
| 5 | + |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | + |
| 9 | +// Read the logos.tsx file |
| 10 | +const logosPath = path.join(__dirname, '../../app/src/app/(app)/[orgId]/trust/portal-settings/components/logos.tsx'); |
| 11 | +const logosContent = fs.readFileSync(logosPath, 'utf-8'); |
| 12 | + |
| 13 | +// Extract SVG content for each icon |
| 14 | +function extractSvg(componentName: string): string | null { |
| 15 | + // Find the component export |
| 16 | + const regex = new RegExp(`export const ${componentName} = \\(props.*?\\) => \\(\\s*(<svg[\\s\\S]*?<\\/svg>)\\s*\\);`, 's'); |
| 17 | + const match = logosContent.match(regex); |
| 18 | + |
| 19 | + if (!match) { |
| 20 | + console.warn(`Warning: Could not find ${componentName}`); |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + return match[1] |
| 25 | + .replace(/\{props\}/g, '') // Remove {props} spread |
| 26 | + .replace(/\{\.\.\.props\}/g, '') // Remove {...props} spread |
| 27 | + .trim(); |
| 28 | +} |
| 29 | + |
| 30 | +// Badge components to extract |
| 31 | +const badges = [ |
| 32 | + { name: 'SOC2Type2', type: 'soc2', label: 'SOC 2' }, |
| 33 | + { name: 'ISO27001', type: 'iso27001', label: 'ISO 27001' }, |
| 34 | + { name: 'ISO42001', type: 'iso42001', label: 'ISO 42001' }, |
| 35 | + { name: 'GDPR', type: 'gdpr', label: 'GDPR' }, |
| 36 | + { name: 'HIPAA', type: 'hipaa', label: 'HIPAA' }, |
| 37 | + { name: 'PCIDSS', type: 'pci_dss', label: 'PCI DSS' }, |
| 38 | + { name: 'NEN7510', type: 'nen7510', label: 'NEN 7510' }, |
| 39 | + { name: 'ISO9001', type: 'iso9001', label: 'ISO 9001' }, |
| 40 | +]; |
| 41 | + |
| 42 | +console.log('Extracting and encoding badge icons...\n'); |
| 43 | + |
| 44 | +const encodedBadges: Record<string, { icon: string; label: string }> = {}; |
| 45 | + |
| 46 | +for (const badge of badges) { |
| 47 | + const svgContent = extractSvg(badge.name); |
| 48 | + |
| 49 | + if (svgContent) { |
| 50 | + // Encode as base64 |
| 51 | + const base64 = Buffer.from(svgContent).toString('base64'); |
| 52 | + const dataUrl = `data:image/svg+xml;base64,${base64}`; |
| 53 | + |
| 54 | + encodedBadges[badge.type] = { |
| 55 | + icon: dataUrl, |
| 56 | + label: badge.label, |
| 57 | + }; |
| 58 | + |
| 59 | + console.log(`✓ ${badge.label} (${badge.name})`); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Read the service file |
| 64 | +const servicePath = path.join(__dirname, '../src/trust-portal/trust-access.service.ts'); |
| 65 | +let serviceContent = fs.readFileSync(servicePath, 'utf-8'); |
| 66 | + |
| 67 | +// Generate the new BADGE_ICON_MAP code |
| 68 | +const newMapCode = `const BADGE_ICON_MAP: Record<string, { icon: string; label: string }> = { |
| 69 | +${Object.entries(encodedBadges).map(([type, data]) => |
| 70 | + ` ${type}: { |
| 71 | + icon: '${data.icon}', |
| 72 | + label: '${data.label}', |
| 73 | + },` |
| 74 | +).join('\n')} |
| 75 | + };`; |
| 76 | + |
| 77 | +// Replace the old BADGE_ICON_MAP with the new one |
| 78 | +const mapRegex = /const BADGE_ICON_MAP: Record<string, \{ icon: string; label: string \}> = \{[\s\S]*?\};/; |
| 79 | + |
| 80 | +if (mapRegex.test(serviceContent)) { |
| 81 | + serviceContent = serviceContent.replace(mapRegex, newMapCode); |
| 82 | + fs.writeFileSync(servicePath, serviceContent, 'utf-8'); |
| 83 | + console.log('\n✅ Successfully updated trust-access.service.ts with encoded badge icons!'); |
| 84 | +} else { |
| 85 | + console.error('\n❌ Could not find BADGE_ICON_MAP in trust-access.service.ts'); |
| 86 | + console.log('\nGenerated code:\n'); |
| 87 | + console.log(newMapCode); |
| 88 | +} |
0 commit comments