|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const prettier = require('prettier'); |
| 7 | + |
| 8 | +(async function main() { |
| 9 | + // Run svgo on all svg files in this folder |
| 10 | + try { |
| 11 | + console.log('Optimizing SVGs with svgo...'); |
| 12 | + execSync('svgo *.svg', { stdio: 'inherit' }); |
| 13 | + } catch (err) { |
| 14 | + console.error('Error running svgo:', err); |
| 15 | + process.exit(1); |
| 16 | + } |
| 17 | + |
| 18 | + // Get all .svg files in the current directory |
| 19 | + const allFiles = fs.readdirSync(process.cwd()); |
| 20 | + const svgFiles = allFiles.filter((file) => file.endsWith('.svg')); |
| 21 | + |
| 22 | + for (const svgFile of svgFiles) { |
| 23 | + const name = path.basename(svgFile, '.svg'); |
| 24 | + const tsxFileName = `${name}Icon.tsx`; |
| 25 | + |
| 26 | + // Read the original SVG file |
| 27 | + let svgContent = fs.readFileSync(svgFile, 'utf8'); |
| 28 | + |
| 29 | + // Replace '#43436B' with 'currentColor' |
| 30 | + svgContent = svgContent.replace(/#43436B/g, 'currentColor'); |
| 31 | + |
| 32 | + // Ensure the <svg> tag has a viewBox attribute; if not, add one. |
| 33 | + const svgTagMatch = svgContent.match(/<svg\b([^>]*)>/); |
| 34 | + if (svgTagMatch) { |
| 35 | + const svgTag = svgTagMatch[0]; |
| 36 | + if (!/viewBox=/.test(svgTag)) { |
| 37 | + // Insert viewBox attribute right after <svg |
| 38 | + const newSvgTag = svgTag.replace('<svg', '<svg viewBox="0 0 16 16"'); |
| 39 | + svgContent = svgContent.replace(svgTag, newSvgTag); |
| 40 | + } |
| 41 | + } else { |
| 42 | + console.warn(`No <svg> tag found in ${svgFile}. Skipping file.`); |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + // Convert dash-case attributes to camelCase (e.g. fill-rule -> fillRule) |
| 47 | + svgContent = svgContent.replace( |
| 48 | + /(\s+)([a-z]+-[a-z0-9-]+)(\s*=)/gi, |
| 49 | + (match, pre, attrName, post) => { |
| 50 | + const camelAttr = attrName.replace(/-([a-z])/g, (_, letter) => |
| 51 | + letter.toUpperCase(), |
| 52 | + ); |
| 53 | + return pre + camelAttr + post; |
| 54 | + }, |
| 55 | + ); |
| 56 | + |
| 57 | + // Build the TSX content using the template. |
| 58 | + const tsxTemplate = ` |
| 59 | +import { wrapIcon } from './wrap-icon'; |
| 60 | +
|
| 61 | +export const ${name}Icon = wrapIcon( |
| 62 | + '${name}Icon', |
| 63 | + ( |
| 64 | + ${svgContent.trim()} |
| 65 | + ), |
| 66 | +); |
| 67 | +`; |
| 68 | + |
| 69 | + // Format the content with local Prettier configuration (using babel-ts parser) |
| 70 | + let formattedTSX; |
| 71 | + try { |
| 72 | + formattedTSX = await prettier.format(tsxTemplate, { parser: 'babel-ts' }); |
| 73 | + } catch (err) { |
| 74 | + console.error(`Error formatting ${tsxFileName} with Prettier:`, err); |
| 75 | + formattedTSX = tsxTemplate; // Fallback to unformatted version |
| 76 | + } |
| 77 | + |
| 78 | + // Write the new TSX file |
| 79 | + fs.writeFileSync(tsxFileName, formattedTSX, 'utf8'); |
| 80 | + console.log(`Created ${tsxFileName}`); |
| 81 | + } |
| 82 | + |
| 83 | + // Update index.ts to add new exports and sort them |
| 84 | + const indexPath = path.join(process.cwd(), 'index.ts'); |
| 85 | + if (!fs.existsSync(indexPath)) { |
| 86 | + console.error('index.ts not found in the current directory.'); |
| 87 | + process.exit(1); |
| 88 | + } |
| 89 | + |
| 90 | + let indexContent = fs.readFileSync(indexPath, 'utf8'); |
| 91 | + const lines = indexContent.split('\n'); |
| 92 | + |
| 93 | + // Locate the line with "export { wrapIcon } from './wrap-icon';" |
| 94 | + const wrapIconLineIndex = lines.findIndex((line) => |
| 95 | + line.includes("export { wrapIcon } from './wrap-icon';"), |
| 96 | + ); |
| 97 | + if (wrapIconLineIndex === -1) { |
| 98 | + console.error("Couldn't find the export { wrapIcon } line in index.ts."); |
| 99 | + process.exit(1); |
| 100 | + } |
| 101 | + |
| 102 | + // Get the export block above the wrapIcon export |
| 103 | + let headerLines = lines.slice(0, wrapIconLineIndex); |
| 104 | + const footerLines = lines.slice(wrapIconLineIndex); |
| 105 | + |
| 106 | + // Ensure that for each SVG file there is an export line |
| 107 | + svgFiles.forEach((svgFile) => { |
| 108 | + const name = path.basename(svgFile, '.svg'); |
| 109 | + const exportLine = `export { ${name}Icon } from './${name}Icon';`; |
| 110 | + // Add if not already present (ignoring extra whitespace) |
| 111 | + if (!headerLines.some((line) => line.trim() === exportLine)) { |
| 112 | + headerLines.push(exportLine); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + // Remove empty lines and sort the export lines alphabetically |
| 117 | + headerLines = headerLines |
| 118 | + .filter((line) => line.trim() !== '') |
| 119 | + .sort((a, b) => a.localeCompare(b)); |
| 120 | + |
| 121 | + // Reassemble the file content |
| 122 | + const newIndexContent = [...headerLines, ...footerLines].join('\n'); |
| 123 | + |
| 124 | + // Format the updated index.ts with Prettier |
| 125 | + let formattedIndex; |
| 126 | + try { |
| 127 | + formattedIndex = await prettier.format(newIndexContent, { |
| 128 | + parser: 'babel-ts', |
| 129 | + }); |
| 130 | + } catch (err) { |
| 131 | + console.error('Error formatting index.ts with Prettier:', err); |
| 132 | + formattedIndex = newIndexContent; |
| 133 | + } |
| 134 | + |
| 135 | + // Write the updated index.ts |
| 136 | + fs.writeFileSync(indexPath, formattedIndex, 'utf8'); |
| 137 | + console.log('Updated index.ts'); |
| 138 | + |
| 139 | + // Remove all initial SVG files |
| 140 | + for (const svgFile of svgFiles) { |
| 141 | + fs.unlinkSync(svgFile); |
| 142 | + console.log(`Removed ${svgFile}`); |
| 143 | + } |
| 144 | +})(); |
0 commit comments