Skip to content

Commit 500d54b

Browse files
authored
Merge pull request #1 from stplasim/master
Add material icon component generator for react
2 parents f105449 + 87bfcfb commit 500d54b

11 files changed

Lines changed: 6735 additions & 0 deletions

.eslintrc.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"env": {
4+
"browser": true,
5+
"node": true
6+
},
7+
"plugins": [
8+
"prettier",
9+
"@typescript-eslint",
10+
"jest",
11+
"import"
12+
],
13+
"extends": [
14+
"prettier",
15+
"plugin:prettier/recommended",
16+
"plugin:@typescript-eslint/recommended",
17+
"plugin:import/typescript"
18+
],
19+
"parserOptions": {
20+
"sourceType": "module",
21+
"ecmaVersion": 2018
22+
},
23+
"rules": {
24+
"import/namespace": "off",
25+
"import/order": [
26+
"error"
27+
],
28+
"no-unused-vars": "off",
29+
"@typescript-eslint/no-unused-vars": "error",
30+
"@typescript-eslint/member-delimiter-style": "off",
31+
"@typescript-eslint/explicit-function-return-type": "off",
32+
"@typescript-eslint/no-var-requires": "off",
33+
"@typescript-eslint/no-extra-semi": "off"
34+
},
35+
"overrides": [
36+
{
37+
// enable the rule specifically for TypeScript files
38+
"files": [
39+
"*.ts",
40+
"*.tsx"
41+
],
42+
"rules": {
43+
"@typescript-eslint/explicit-function-return-type": [
44+
"error"
45+
]
46+
}
47+
}
48+
]
49+
}

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Basic
2+
.DS_Store
3+
4+
# IDE
5+
/.idea
6+
/.vscode
7+
8+
# Project
9+
/package-lock.json
10+
/node_modules
11+
/dist

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"semi": false
4+
}

generator.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
})()

jestconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"transform": {
3+
"^.+\\.(t|j)sx?$": "ts-jest"
4+
},
5+
"testRegex": "(/__tests?__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
7+
}

license.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
Copyright About Bits GmbH
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.

0 commit comments

Comments
 (0)