@@ -4,83 +4,124 @@ import uppercamelcase from "uppercamelcase";
44import path from "path" ;
55import mkdirp from "mkdirp" ;
66import { transform } from "@svgr/core" ;
7- import * as cheerio from "cheerio"
7+ import * as cheerio from "cheerio" ;
8+ import {
9+ COMPONENT_GENERATION_CONFIG ,
10+ ENCODING_STANDARD ,
11+ } from "./constants.mjs" ;
812
9- export const generateComponents = ( config = { } ) => {
10- return {
11- name : "generate-components" ,
12- buildStart : async ( ) => {
13- await config . forEach ( ( {
14- options = { } ,
15- source = "./source/icons/**.svg" ,
16- additionalTransformations = ( string ) => string ,
17- destination = "./generate/icons" ,
18- } ) => {
19- try {
20- const encodingStandard = "utf-8"
21- const indexFilePath = path . join ( destination , "index.js" ) ;
22- const directory = path . parse ( indexFilePath ) . dir ;
13+ const generateComponents = ( ) => {
14+ // delete the dist and generate folders
15+ try {
16+ fs . rmSync ( "dist" , { recursive : true , force : true } ) ;
17+ fs . rmSync ( "generate" , { recursive : true , force : true } ) ;
18+ } catch ( err ) {
19+ if ( err . code !== "ENOENT" ) console . log ( err ) ;
20+ }
2321
24- // make the directory
25- mkdirp . sync ( directory ) ;
22+ COMPONENT_GENERATION_CONFIG . forEach (
23+ ( {
24+ options = { } ,
25+ source = "./source/icons/**.svg" ,
26+ additionalTransformations = ( string ) => string ,
27+ destination = "./generate/icons" ,
28+ iconType,
29+ } ) => {
30+ try {
31+ const indexFilePath = path . join ( destination , "index.js" ) ;
32+ const typeFilePath = `${ destination . split ( "/" ) . at ( - 1 ) } .d.ts` ;
33+ const directory = path . parse ( indexFilePath ) . dir ;
2634
27- // make the index file
28- fs . writeFileSync ( indexFilePath , "" , encodingStandard ) ;
35+ // make the directory
36+ mkdirp . sync ( directory ) ;
2937
30- // make the icon folder
31- mkdirp . sync ( destination ) ;
38+ // make the index file
39+ fs . writeFileSync ( indexFilePath , "" , ENCODING_STANDARD ) ;
3240
33- // read the icon source
34- glob ( source , ( err , icons ) => {
35- if ( err ) {
36- throw err ;
37- }
41+ // make the type file
42+ fs . writeFileSync (
43+ typeFilePath ,
44+ `import React from "react";\n\n${ iconType } \r\n` ,
45+ ENCODING_STANDARD
46+ ) ;
3847
39- if ( icons . length === 0 ) {
40- throw Error (
41- "Unable to find any icons. Make sure you use a glob format. [path/**.svg]" ,
42- ) ;
43- }
48+ // make the icon folder
49+ mkdirp . sync ( destination ) ;
4450
45- icons . forEach ( ( iconPath ) => {
46- const svg = cheerio . load ( fs . readFileSync ( iconPath , encodingStandard ) , { xmlMode : true } ) ;
47- const iconName = path . basename ( iconPath , path . extname ( iconPath ) ) ;
48- const componentName = uppercamelcase ( iconName ) ;
49- const importStatement = `export * from "./${ componentName } ";\r\n`
50- const iconDestination = path . join (
51- destination ,
52- componentName + ".js" , ) ;
51+ // read the icon source
52+ glob ( source , ( err , icons ) => {
53+ if ( err ) {
54+ throw err ;
55+ }
5356
54- // perform some santization and specific transformations besides
55- // the ones provided to us by svgr
56- svg ( "*" ) . each ( ( _ , el ) => {
57- const element = svg ( el )
58- Object . keys ( el . attribs ) . forEach ( ( attribute ) => {
59- additionalTransformations ( attribute , svg ( el ) ) ;
60- } )
61- if ( el . name === "svg" ) {
62- element . removeAttr ( "class" )
63- . removeAttr ( "xmlns" )
64- }
65- } ) ;
57+ if ( icons . length === 0 ) {
58+ throw Error (
59+ "Unable to find any icons. Make sure you use a glob format. [path/**.svg]"
60+ ) ;
61+ }
6662
67- // generate component code
68- const reactComponent = transform . sync (
69- svg ( "svg" ) . toString ( ) ,
70- options ,
71- { componentName, filePath : iconDestination } ,
72- ) ;
63+ const components = [ ] ;
64+ icons . forEach ( ( iconPath ) => {
65+ const svg = cheerio . load (
66+ fs . readFileSync ( iconPath , ENCODING_STANDARD ) ,
67+ { xmlMode : true }
68+ ) ;
69+ const iconName = path . basename ( iconPath , path . extname ( iconPath ) ) ;
70+ const componentName = uppercamelcase ( iconName ) ;
71+ const importStatement = `import ${ componentName } from "./${ componentName } ";\r\n` ;
72+ const typeDefinition = `export const ${ componentName } : React.FC<IconProps>;\r\n` ;
73+ const iconDestination = path . join (
74+ destination ,
75+ componentName + ".js"
76+ ) ;
77+ components . push ( componentName ) ;
7378
74- // write component to file
75- fs . writeFileSync ( iconDestination , reactComponent , encodingStandard ) ;
76- // append component import statement to the index file
77- fs . appendFileSync ( indexFilePath , importStatement , encodingStandard ) ;
79+ // perform some santization and specific transformations besides
80+ // the ones provided to us by svgr
81+ svg ( "*" ) . each ( ( _ , el ) => {
82+ const element = svg ( el ) ;
83+ Object . keys ( el . attribs ) . forEach ( ( attribute ) => {
84+ additionalTransformations ( attribute , svg ( el ) ) ;
85+ } ) ;
86+ if ( el . name === "svg" ) {
87+ element . removeAttr ( "class" ) . removeAttr ( "xmlns" ) ;
88+ }
7889 } ) ;
90+
91+ // generate component code
92+ const reactComponent = transform . sync (
93+ svg ( "svg" ) . toString ( ) ,
94+ options ,
95+ { componentName, filePath : iconDestination }
96+ ) ;
97+
98+ // write component to file
99+ fs . writeFileSync (
100+ iconDestination ,
101+ reactComponent ,
102+ ENCODING_STANDARD
103+ ) ;
104+ // append component import statement to the index file
105+ fs . appendFileSync (
106+ indexFilePath ,
107+ importStatement ,
108+ ENCODING_STANDARD
109+ ) ;
110+ // append type definition
111+ fs . appendFileSync ( typeFilePath , typeDefinition , ENCODING_STANDARD ) ;
79112 } ) ;
80- } catch ( err ) {
81- console . log ( err ) ;
82- }
83- } ) ;
84- } ,
85- } ;
113+
114+ const exportStatement = `export { \r\n\t${ components . join (
115+ ",\r\n\t"
116+ ) } \r\n};`;
117+ // append export statement to the index file
118+ fs . appendFileSync ( indexFilePath , exportStatement , ENCODING_STANDARD ) ;
119+ } ) ;
120+ } catch ( err ) {
121+ console . log ( err ) ;
122+ }
123+ }
124+ ) ;
86125} ;
126+
127+ generateComponents ( ) ;
0 commit comments