Skip to content

Commit 79e6ca2

Browse files
author
Simon Planinschek
committed
cleanup script
1 parent a838ca7 commit 79e6ca2

1 file changed

Lines changed: 55 additions & 61 deletions

File tree

generator.js

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@ const fs = require('fs')
22
const path = require('path')
33
const axios = require('axios')
44

5-
// This is the number of icons each thread will be downloading
6-
const DOWNLOAD_CHUNK_SIZE = 500
75
const GOOGLE_FONTS_URL = 'https://fonts.google.com/metadata/icons'
86
const ICON_FAMILIES = [
9-
{ id: 'materialicons', namePostfix: '' },
10-
{ id: 'materialiconsoutlined', namePostfix: 'Outlined' },
11-
{ id: 'materialiconsround', namePostfix: 'Round' },
12-
{ id: 'materialiconssharp', namePostfix: 'Sharp' },
13-
{ id: 'materialiconstwotone', namePostfix: 'TwoTone' },
7+
{ id: 'materialicons', postfix: '' },
8+
{ id: 'materialiconsoutlined', postfix: 'Outlined' },
9+
{ id: 'materialiconsround', postfix: 'Round' },
10+
{ id: 'materialiconssharp', postfix: 'Sharp' },
11+
{ id: 'materialiconstwotone', postfix: 'TwoTone' },
1412
]
1513

14+
;(async () => {
15+
generateIconPropsFile()
16+
17+
const res = await axios.get(GOOGLE_FONTS_URL)
18+
19+
// remove )]}' from the response
20+
const data = res.data.substring(4)
21+
const icons = await JSON.parse(data)
22+
23+
for (let i = 0; i < icons.icons.length; i++) {
24+
generateComponents(icons.icons[i])
25+
}
26+
})()
27+
1628
function generateIconPropsFile() {
1729
const typesFile = `
1830
import React from 'react'
@@ -24,31 +36,40 @@ function generateIconPropsFile() {
2436
fs.writeFileSync(path.join(__dirname, 'src', 'types.ts'), typesFile)
2537
}
2638

27-
function formatName(string, familyName) {
39+
async function generateComponents(icon) {
40+
for (let i = 0; i < ICON_FAMILIES.length; i++) {
41+
await generateComponent(icon, ICON_FAMILIES[i])
42+
}
43+
}
44+
45+
async function generateComponent(icon, family) {
46+
try {
47+
const name = formatName(icon.name, family.postfix)
48+
const svg = await downloadSVG(icon.name, family.id, icon.version)
49+
50+
console.log(`Downloading ${name}`)
51+
52+
await fs.writeFileSync(
53+
path.join(__dirname, 'src', `${name}.tsx`),
54+
mapSVGToTemplate(name, svg)
55+
)
56+
} catch {
57+
process.abort()
58+
}
59+
}
60+
61+
function formatName(string, familyPostfix) {
2862
const formattedString = string
2963
.replace(/_/g, ' ')
3064
.replace(/\w\S*/g, (txt) => {
3165
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
3266
})
3367
.replace(/ /g, '')
3468

35-
return 'Icon' + formattedString + familyName
36-
}
37-
38-
function componentTemplate(name, svg) {
39-
return `
40-
import React from 'react'
41-
import { IconProps } from './types'
42-
43-
const ${name}: React.FC<IconProps> = ({ ...props }) => (
44-
${svg}
45-
)
46-
47-
export { ${name} as default }
48-
`
69+
return 'Icon' + formattedString + familyPostfix
4970
}
5071

51-
async function getSVGFile(icon, familyId, version) {
72+
async function downloadSVG(icon, familyId, version) {
5273
const svg = await axios
5374
.get(
5475
`https://fonts.gstatic.com/s/i/${familyId}/${icon}/v${version}/24px.svg`
@@ -69,42 +90,15 @@ async function getSVGFile(icon, familyId, version) {
6990
.replace(/xlink:href/g, 'xlinkHref')
7091
}
7192

72-
async function generateComponent(icons) {
73-
for (let i = 0; i < icons.length; i++) {
74-
const icon = icons[i]
75-
76-
for (let j = 0; j < ICON_FAMILIES.length; j++) {
77-
const family = ICON_FAMILIES[j]
78-
79-
const name = formatName(icon.name, family.namePostfix)
80-
const svg = await getSVGFile(icon.name, family.id, icon.version)
81-
82-
console.log(`Downloading ${name} (${i + 1}/${icons.length})`)
83-
84-
await fs.writeFileSync(
85-
path.join(__dirname, 'src', `${name}.tsx`),
86-
componentTemplate(name, svg)
87-
)
88-
}
89-
}
93+
function mapSVGToTemplate(name, svg) {
94+
return `
95+
import React from 'react'
96+
import { IconProps } from './types'
97+
98+
const ${name}: React.FC<IconProps> = ({ ...props }) => (
99+
${svg}
100+
)
101+
102+
export { ${name} as default }
103+
`
90104
}
91-
92-
;(async () => {
93-
generateIconPropsFile()
94-
95-
const res = await axios.get(GOOGLE_FONTS_URL)
96-
97-
// remove )]}' from the response
98-
const data = res.data.substring(4)
99-
const icons = await JSON.parse(data)
100-
101-
const iconChunks = new Array(
102-
Math.ceil(icons.icons.length / DOWNLOAD_CHUNK_SIZE)
103-
)
104-
.fill()
105-
.map(() => icons.icons.splice(0, DOWNLOAD_CHUNK_SIZE))
106-
107-
for (let i = 0; i < iconChunks.length; i++) {
108-
generateComponent(iconChunks[i])
109-
}
110-
})()

0 commit comments

Comments
 (0)