-
Notifications
You must be signed in to change notification settings - Fork 1
Add material icon component generator for react #1
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
Changes from 11 commits
ee5d779
fb5ac91
7ffbc05
20c982a
df815d6
a67a56b
295a8b4
2e3757d
20f9e58
a8fc593
bd2bf5a
e87258f
4d3513b
5232fb9
497f760
daac250
e0f7e78
815ef0a
0dcdee2
69c90f7
202fe22
4574a61
26205f5
7b2369f
b185076
0cff09d
87e2b61
f10c073
5fd82c9
291c9c6
89f8e52
9607952
a1b412e
87bfcfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # IDE Stuff | ||
| .idea | ||
|
|
||
|
|
||
| # Node and Javascirpt | ||
| node_modules | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export type IconProps = React.SVGProps<SVGSVGElement> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| const axios = require('axios'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| // Base url for icons | ||
| const iconURL = "https://fonts.google.com/metadata/icons"; | ||
|
|
||
| /** | ||
| * 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) { | ||
| const formattedString = string | ||
| .replace(/_/g, ' ') | ||
| .replace(/\w\S*/g, (txt) => { | ||
| return txt | ||
| .charAt(0) | ||
| .toUpperCase() + txt.substr(1) | ||
| .toLowerCase(); | ||
| }) | ||
| .replace(/ /g, ''); | ||
| return 'Icon' + formattedString; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Component template for functional react icon component | ||
| * | ||
| * @param name - Component name | ||
| * @param svg - Icon SVG | ||
| * @returns {string} - Component | ||
| */ | ||
| function componentTemplate(name, svg) { | ||
| let component = "import React from 'react'\n"; | ||
| component += "import { IconProps } from './types'\n"; | ||
| component += `const ${name}: React.FC<IconProps> = (props) => (`; | ||
| component += svg + ")\n\n"; | ||
| component += `export { ${name} }`; | ||
|
|
||
| return component; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * 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, './components', `${name}.tsx`), | ||
| componentTemplate(name, svg) | ||
| ) | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Generate index tsx file for easy import | ||
| * | ||
| * @param icons - Array of icons | ||
| */ | ||
| function generateIndex(icons) { | ||
| let imports = ""; | ||
| let exports = "export {\n"; | ||
|
|
||
| icons.forEach(icon => { | ||
| const name = formatName(icon.name) | ||
| imports += `import { ${name} } from './components/${name}'\n` | ||
| exports += `\t${name},\n` | ||
| }); | ||
| exports += "}"; | ||
|
|
||
| fs.writeFileSync( | ||
| "./index.tsx", | ||
| (imports + "\n" + exports) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 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`); | ||
|
|
||
| return svg | ||
| .data | ||
| .replace('height="24"', '') | ||
| .replace('width="24"', '{...props}'); | ||
| } | ||
|
|
||
|
|
||
| (async () => { | ||
| // Get icon information from google fonts | ||
| const res = await axios.get(iconURL); | ||
| // remove )]}' from the response | ||
| 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); | ||
| await generateIndex(icons.icons); | ||
|
|
||
| })(); | ||
|
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. I would add an empty line at the end of the file. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
|
|
||
|
|
||
| Copyright About Bits GmbH | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "material-design-icons-crawler", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "generator.js", | ||
| "scripts": { | ||
| "build": "node generator.js", | ||
|
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. Attention, the build command is a reserved command that will be used when you execute "npm version patch/minor/major". That's why I would rename this command to something more explicitly named like "import". In addition, in this file lots of required dependencies and scripts are missing. They are required for publishing the package. See: https://github.com/aboutbits/pagination/blob/master/package.json
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. All necessary fields have been added. Since tsc is not required for this package, the build command executes the bootstrapping command to create the icon components |
||
| "clean": "rm components/*.tsx && rm index.tsx" | ||
| }, | ||
| "devDependencies": { | ||
| "axios": "^0.20.0" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,67 @@ | ||
| React Material Icons | ||
| ==================== | ||
|
|
||
| This package includes all [Material Icons](https://material.io/resources/icons/?style=baseline) as reusable react components | ||
|
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. I would write "react" as "React", because it's the right way to write 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. All names have been updated |
||
|
|
||
| ## Table of content | ||
| - [Usage](#usage) | ||
|
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. Please add consistent empty lines before and after titles and paragraphs like in the other packages: https://raw.githubusercontent.com/aboutbits/pagination/master/readme.md |
||
| - [Build & Publish](#build--publish) | ||
| - [Information](#information) | ||
|
|
||
| ## Usage | ||
|
|
||
| In order to use this package you have to install it through npm | ||
|
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. I would use here the same wording like in the other packages: First, ... |
||
| ```bash | ||
| npm install @aboutbits/react-material-icons | ||
| ``` | ||
|
|
||
| After you successfully installed the package you can simply use the icons in you react application like this | ||
| ```jsx | ||
| import React from 'react' | ||
|
|
||
| const MyCommponent = () => { | ||
| return ( | ||
| <IconCached /> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| SVG related parameters like height and width can be passed as props | ||
|
|
||
| ## Build & Publish | ||
|
|
||
| To build the package, simply run the following command | ||
|
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. I would extract the description of the build/clean command into it's own section. And keep the "Build & Publish" section equals to the other packages.
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. Build and clean have got an own section. |
||
| ```bash | ||
| npm run build | ||
| ``` | ||
|
|
||
| To publish the package commit all changes and push them to master. Then run one of the following commands locally: | ||
| ```bash | ||
| npm version patch | ||
| npm version minor | ||
| npm version major | ||
| ``` | ||
|
|
||
| To remove all generated files run the clean script | ||
|
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. Should the user be able to clean the generated files? Or should they be automatically be deleted as first step of the generation? For example, first delete the src folder and then regenerate the files in there. The clean task that you mention here, is to delete the dist folder. And the dist folder is just required for publishing. |
||
| ```bash | ||
| npm run clean | ||
| ``` | ||
|
|
||
| ## Information | ||
|
|
||
| About Bits is a company based in South Tyrol, Italy. You can find more information about us on [our website](https://aboutbits.it). | ||
|
|
||
| ### Support | ||
|
|
||
| For support, please contact [info@aboutbits.it](mailto:info@aboutbits.it). | ||
|
|
||
| ### Credits | ||
|
|
||
| - [Simon Planinschek](https://github.com/stplasim) | ||
| - [Alex Lanz](https://github.com/alexlanz) | ||
| - [Martin Malfertheiner](https://github.com/mmalfertheiner) | ||
| - [All Contributors](../../contributors) | ||
|
|
||
| ### License | ||
|
|
||
| The MIT License (MIT). Please see the [license file](license.md) for more information. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add more files. Maybe you can adopt the structure from the other packages:
https://github.com/aboutbits/pagination/blob/master/.gitignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added the required fields