@@ -5,6 +5,17 @@ const axios = require('axios')
55// Base url for icons
66const iconURL = 'https://fonts.google.com/metadata/icons'
77
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+
819/**
920 * Format name.
1021 * Covert from snake case to camel case and prevent numbers at the beginning
@@ -23,59 +34,22 @@ function formatName(string) {
2334}
2435
2536/**
26- * Setup the dist folder and some static files
27- */
28- function setup ( ) {
29- // Create the dist folder
30- fs . mkdirSync ( path . join ( __dirname , '..' , 'dist' ) )
31-
32- // Create the components folder
33- fs . mkdirSync ( path . join ( __dirname , '..' , 'dist' , 'components' ) )
34-
35- // Add the type declaration file to the components folder
36- fs . writeFileSync (
37- path . join ( __dirname , '..' , 'dist' , 'components' , 'types.d.ts' ) ,
38- '/// <reference types="react" />\nexport type IconProps = React.SVGProps<SVGSVGElement>\n'
39- )
40-
41- // Add type file to the components folder
42- fs . writeFileSync (
43- path . join ( __dirname , '..' , 'dist' , 'components' , 'types.js' ) ,
44- '"use strict"\nexports.__esModule = true;'
45- )
46- }
47-
48- /**
49- * Component template for functional Javascript react icon component
37+ * Component template for functional react icon component
5038 *
5139 * @param name - Component name
5240 * @param svg - Icon SVG
5341 * @returns {string } - Component
5442 */
55- function componentJSTemplate ( name , svg ) {
43+ function componentTemplate ( name , svg ) {
5644 let component = "import React from 'react'\n"
57- component += `const ${ name } = (props) => (`
45+ component += "import { IconProps } from './types'\n"
46+ component += `const ${ name } : React.FC<IconProps> = (props) => (`
5847 component += svg + ')\n\n'
5948 component += `export { ${ name } }`
6049
6150 return component
6251}
6352
64- /**
65- * Generate Declaration file for icon components
66- *
67- * @param name - Icon name
68- * @returns {string } - Declaration file template
69- */
70- function componentDeclarationTemplate ( name ) {
71- let declaration = "import React from 'react';\n"
72- declaration += "import { IconProps } from './types';\n\n"
73- declaration += `declare const ${ name } : React.FC<IconProps>;\n\n`
74- declaration += `export { ${ name } };`
75-
76- return declaration
77- }
78-
7953/**
8054 * Generate React Icon components
8155 *
@@ -86,16 +60,9 @@ function generateComponent(icon) {
8660 const name = formatName ( c . name )
8761 const svg = await getSVGFile ( c . name , c . version )
8862
89- // Generate js component
9063 fs . writeFileSync (
91- path . join ( __dirname , '..' , 'dist' , 'components' , `${ name } .jsx` ) ,
92- componentJSTemplate ( name , svg )
93- )
94-
95- // Generate ts declaration file for js component
96- fs . writeFileSync (
97- path . join ( __dirname , '..' , 'dist' , 'components' , `${ name } .d.ts` ) ,
98- componentDeclarationTemplate ( name )
64+ path . join ( __dirname , 'src' , 'components' , `${ name } .tsx` ) ,
65+ componentTemplate ( name , svg )
9966 )
10067 } )
10168}
@@ -116,15 +83,8 @@ function generateIndex(icons) {
11683 } )
11784 exports += '}'
11885
119- // Write js index
120- fs . writeFileSync (
121- path . join ( __dirname , '..' , 'dist' , 'index.js' ) ,
122- imports + '\n' + exports
123- )
124-
125- // Write ts index
12686 fs . writeFileSync (
127- path . join ( __dirname , '.. ' , 'dist' , ' index.d .ts') ,
87+ path . join ( __dirname , 'src ' , 'index.ts' ) ,
12888 imports + '\n' + exports
12989 )
13090}
@@ -147,20 +107,19 @@ async function getSVGFile(icon, version) {
147107/**
148108 * Main function
149109 */
150- ; ( async function ( ) {
151- // Setup environment
110+ ; ( async ( ) => {
111+ // Setup source folder
152112 setup ( )
153113
154114 // Get icon information from google fonts
155115 const res = await axios . get ( iconURL )
156-
157116 // remove )] }' from the response
158117 const data = res . data . substring ( 4 )
159118
160119 // Parse response from json to js object
161120 const icons = await JSON . parse ( data )
162- // Generate icon components and Index
163121
122+ // Generate icon components and Index
164123 await generateComponent ( icons . icons )
165124 await generateIndex ( icons . icons )
166125} ) ( )
0 commit comments