-
Notifications
You must be signed in to change notification settings - Fork 1
Add all icon variants #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8d366d8
update generator script to support all variants of icons
c4b8d0b
update readme
4d1530c
split generate and clear script
cbdc09e
add handler for xlink
a838ca7
fix typo
79e6ca2
cleanup script
a5bacb4
renaming
935cd7a
add icons
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,45 +2,39 @@ const fs = require('fs') | |
| const path = require('path') | ||
| const axios = require('axios') | ||
|
|
||
| // Base url for icons | ||
| const iconURL = 'https://fonts.google.com/metadata/icons' | ||
| // This is the number of icons each thread will be downloading | ||
| const DOWNLOAD_CHUNK_SIZE = 500 | ||
| const GOOGLE_FONTS_URL = 'https://fonts.google.com/metadata/icons' | ||
| const ICON_FAMILIES = [ | ||
| { id: 'materialicons', namePostfix: '' }, | ||
| { id: 'materialiconsoutlined', namePostfix: 'Outlined' }, | ||
| { id: 'materialiconsround', namePostfix: 'Round' }, | ||
| { id: 'materialiconssharp', namePostfix: 'Sharp' }, | ||
| { id: 'materialiconstwotone', namePostfix: 'TwoTone' }, | ||
| ] | ||
|
|
||
| function setup() { | ||
| function generateIconPropsFile() { | ||
|
stplasim marked this conversation as resolved.
Outdated
|
||
| const typesFile = ` | ||
| import React from 'react' | ||
| export interface IconProps extends React.SVGProps<SVGSVGElement> { | ||
| title?: string | ||
| } | ||
| ` | ||
|
|
||
| // Create types file | ||
| fs.writeFileSync(path.join(__dirname, 'src', 'types.ts'), typesFile) | ||
| } | ||
|
|
||
| /** | ||
| * Format name. | ||
| * Covert from snake case to camel case and prevent numbers at the beginning | ||
| * | ||
| * @param string - String to format | ||
| * @returns {string} - Formatted string | ||
| */ | ||
| function formatName(string) { | ||
| function formatName(string, familyName) { | ||
|
stplasim marked this conversation as resolved.
Outdated
|
||
| const formattedString = string | ||
| .replace(/_/g, ' ') | ||
| .replace(/\w\S*/g, (txt) => { | ||
| return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() | ||
| }) | ||
| .replace(/ /g, '') | ||
| return 'Icon' + formattedString | ||
|
|
||
| return 'Icon' + formattedString + familyName | ||
| } | ||
|
|
||
| /** | ||
| * Component template for functional react icon component | ||
| * | ||
| * @param name - Component name | ||
| * @param svg - Icon SVG | ||
| * @returns {string} - Component | ||
| */ | ||
| function componentTemplate(name, svg) { | ||
| return ` | ||
| import React from 'react' | ||
|
|
@@ -54,34 +48,12 @@ function componentTemplate(name, svg) { | |
| ` | ||
| } | ||
|
|
||
| /** | ||
| * Generate React Icon components | ||
| * | ||
| * @param icon - Icon object | ||
| */ | ||
| function generateComponent(icon) { | ||
| icon.forEach(async (c) => { | ||
| const name = formatName(c.name) | ||
| const svg = await getSVGFile(c.name, c.version) | ||
|
|
||
| fs.writeFileSync( | ||
| path.join(__dirname, 'src', `${name}.tsx`), | ||
| componentTemplate(name, svg) | ||
| async function getSVGFile(icon, familyId, version) { | ||
| const svg = await axios | ||
| .get( | ||
| `https://fonts.gstatic.com/s/i/${familyId}/${icon}/v${version}/24px.svg` | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Get SVG data form google static fonts api | ||
| * | ||
| * @param icon - Icon name | ||
| * @param version - Icon Version | ||
| * @returns {Promise<any>} - SVG Data | ||
| */ | ||
| async function getSVGFile(icon, version) { | ||
| const svg = await axios.get( | ||
| `https://fonts.gstatic.com/s/i/materialicons/${icon}/v${version}/24px.svg` | ||
| ) | ||
| .catch((err) => console.log(err)) | ||
|
|
||
| return svg.data | ||
| .replace('height="24"', '') | ||
|
|
@@ -93,23 +65,46 @@ async function getSVGFile(icon, version) { | |
| .replace('>', '>{props.title && <title>{props.title}</title>}') | ||
| .replace(/style="enableBackground:(.*?);?"/g, 'enableBackground="$1"') | ||
| .replace(/style="fill:(.*?);?"/g, 'fill="$1"') | ||
| .replace(/xmlns:xlink/g, 'xmlnsXlink') | ||
| .replace(/xlink:href/g, 'xlinkHref') | ||
| } | ||
|
|
||
| async function generateComponent(icons) { | ||
| for (let i = 0; i < icons.length; i++) { | ||
| const icon = icons[i] | ||
|
|
||
| for (let j = 0; j < ICON_FAMILIES.length; j++) { | ||
| const family = ICON_FAMILIES[j] | ||
|
|
||
| const name = formatName(icon.name, family.namePostfix) | ||
|
stplasim marked this conversation as resolved.
Outdated
|
||
| const svg = await getSVGFile(icon.name, family.id, icon.version) | ||
|
|
||
| console.log(`Downloading ${name} (${i + 1}/${icons.length})`) | ||
|
|
||
| await fs.writeFileSync( | ||
| path.join(__dirname, 'src', `${name}.tsx`), | ||
| componentTemplate(name, svg) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Main function | ||
| */ | ||
| ;(async () => { | ||
| // Setup source folder | ||
| setup() | ||
| generateIconPropsFile() | ||
|
|
||
| const res = await axios.get(GOOGLE_FONTS_URL) | ||
|
|
||
| // Get icon information from google fonts | ||
| const res = await axios.get(iconURL) | ||
| // remove )]}' from the response | ||
|
stplasim marked this conversation as resolved.
Outdated
|
||
| const data = res.data.substring(4) | ||
|
|
||
| // Parse response from json to js object | ||
| const icons = await JSON.parse(data) | ||
|
|
||
| // Generate icon components and Index | ||
| await generateComponent(icons.icons) | ||
| const iconChunks = new Array( | ||
| Math.ceil(icons.icons.length / DOWNLOAD_CHUNK_SIZE) | ||
| ) | ||
| .fill() | ||
| .map(() => icons.icons.splice(0, DOWNLOAD_CHUNK_SIZE)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you do the split up into multiple chunks? I don't really see any benefit here how you handle it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have cleaned it up |
||
|
|
||
| for (let i = 0; i < iconChunks.length; i++) { | ||
| generateComponent(iconChunks[i]) | ||
| } | ||
| })() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.