|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | +const axios = require('axios') |
| 4 | + |
| 5 | +// Base url for icons |
| 6 | +const iconURL = 'https://fonts.google.com/metadata/icons' |
| 7 | + |
| 8 | +function setup() { |
| 9 | + // Create components folder |
| 10 | + fs.mkdirSync(path.join(__dirname, 'src', 'components')) |
| 11 | + |
| 12 | + // Create types file |
| 13 | + fs.writeFileSync( |
| 14 | + path.join(__dirname, 'src', 'components', 'types.ts'), |
| 15 | + 'import React from "react" \nexport type IconProps = React.SVGProps<SVGSVGElement>' |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Format name. |
| 21 | + * Covert from snake case to camel case and prevent numbers at the beginning |
| 22 | + * |
| 23 | + * @param string - String to format |
| 24 | + * @returns {string} - Formatted string |
| 25 | + */ |
| 26 | +function formatName(string) { |
| 27 | + const formattedString = string |
| 28 | + .replace(/_/g, ' ') |
| 29 | + .replace(/\w\S*/g, (txt) => { |
| 30 | + return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() |
| 31 | + }) |
| 32 | + .replace(/ /g, '') |
| 33 | + return 'Icon' + formattedString |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Component template for functional react icon component |
| 38 | + * |
| 39 | + * @param name - Component name |
| 40 | + * @param svg - Icon SVG |
| 41 | + * @returns {string} - Component |
| 42 | + */ |
| 43 | +function componentTemplate(name, svg) { |
| 44 | + let component = "import React from 'react'\n" |
| 45 | + component += "import { IconProps } from './types'\n" |
| 46 | + component += `const ${name}: React.FC<IconProps> = (props) => (` |
| 47 | + component += svg + ')\n\n' |
| 48 | + component += `export { ${name} }` |
| 49 | + |
| 50 | + return component |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Generate React Icon components |
| 55 | + * |
| 56 | + * @param icon - Icon object |
| 57 | + */ |
| 58 | +function generateComponent(icon) { |
| 59 | + icon.forEach(async (c) => { |
| 60 | + const name = formatName(c.name) |
| 61 | + const svg = await getSVGFile(c.name, c.version) |
| 62 | + |
| 63 | + fs.writeFileSync( |
| 64 | + path.join(__dirname, 'src', 'components', `${name}.tsx`), |
| 65 | + componentTemplate(name, svg) |
| 66 | + ) |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Generate index tsx file for easy import |
| 72 | + * |
| 73 | + * @param icons - Array of icons |
| 74 | + */ |
| 75 | +function generateIndex(icons) { |
| 76 | + let imports = '' |
| 77 | + let exports = 'export {\n' |
| 78 | + |
| 79 | + icons.forEach((icon) => { |
| 80 | + const name = formatName(icon.name) |
| 81 | + imports += `import { ${name} } from './components/${name}'\n` |
| 82 | + exports += ` ${name},\n` |
| 83 | + }) |
| 84 | + exports += '}' |
| 85 | + |
| 86 | + fs.writeFileSync( |
| 87 | + path.join(__dirname, 'src', 'index.ts'), |
| 88 | + imports + '\n' + exports |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Get SVG data form google static fonts api |
| 94 | + * |
| 95 | + * @param icon - Icon name |
| 96 | + * @param version - Icon Version |
| 97 | + * @returns {Promise<any>} - SVG Data |
| 98 | + */ |
| 99 | +async function getSVGFile(icon, version) { |
| 100 | + const svg = await axios.get( |
| 101 | + `https://fonts.gstatic.com/s/i/materialicons/${icon}/v${version}/24px.svg` |
| 102 | + ) |
| 103 | + |
| 104 | + return svg.data.replace('height="24"', '').replace('width="24"', '{...props}') |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Main function |
| 109 | + */ |
| 110 | +;(async () => { |
| 111 | + // Setup source folder |
| 112 | + setup() |
| 113 | + |
| 114 | + // Get icon information from google fonts |
| 115 | + const res = await axios.get(iconURL) |
| 116 | + // remove )]}' from the response |
| 117 | + const data = res.data.substring(4) |
| 118 | + |
| 119 | + // Parse response from json to js object |
| 120 | + const icons = await JSON.parse(data) |
| 121 | + |
| 122 | + // Generate icon components and Index |
| 123 | + await generateComponent(icons.icons) |
| 124 | + await generateIndex(icons.icons) |
| 125 | +})() |
0 commit comments