-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiconsGenerator.js
executable file
·117 lines (101 loc) · 3.18 KB
/
iconsGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const p = require('path')
const fs = require('fs')
const uniqueNames = new Set()
function findInDir(dir, filter, fileList = []) {
const files = fs.readdirSync(dir)
files.forEach((file) => {
const filePath = p.join(dir, file)
const fileStat = fs.lstatSync(filePath)
if (fileStat.isDirectory()) {
findInDir(filePath, filter, fileList)
} else if (p.extname(filePath) === filter) {
const name = p.basename(filePath, filter)
if (uniqueNames.has(name)) throw new Error(`file exists: ${name}`)
uniqueNames.add(name)
fileList.push(filePath)
}
})
return fileList
}
const path = 'assets'
const icons = findInDir(path, '.svg')
const fileName = `${path}/AppIcon.tsx`
if (fs.existsSync(fileName)) fs.unlinkSync(fileName)
const logger = fs.createWriteStream(fileName)
logger.write(`/* eslint-disable */
import React, {memo} from 'react'
import {StyleProp, View, ViewStyle} from 'react-native'
`)
const iconNames = Array.from(uniqueNames.values())
const types = iconNames.map((c) => `'${c}'`).join(' | ')
for (const icon of icons) {
const name = p.basename(icon, '.svg')
let capitalized = name.charAt(0).toUpperCase() + name.slice(1)
capitalized = capitalized
.split('-')
.map((c) => c.charAt(0).toUpperCase() + c.slice(1))
.join('')
logger.write(`import ${capitalized} from '.${icon.replace(path, '')}'\n`)
}
logger.write(`
export type AppIconType = ${types}
export interface AppIconProps {
readonly type: AppIconType
readonly testID?: string
readonly nativeID?: string
readonly style?: StyleProp<ViewStyle>
readonly isVisible?: boolean
readonly tint?: string
readonly stroke?: string
readonly width?: number
readonly height?: number
}
const getIcon = (type: AppIconType, tint: string | undefined, stroke: string | undefined, width: number | undefined, height: number | undefined): JSX.Element => {
switch (type) {
`)
for (let iconName of iconNames) {
let capitalized = iconName.charAt(0).toUpperCase() + iconName.slice(1)
capitalized = capitalized
.split('-')
.map((c) => c.charAt(0).toUpperCase() + c.slice(1))
.join('')
logger.write(` case '${iconName}':\n`)
logger.write(
` if (!!tint && !width && !height) return <${capitalized} fill={tint} />\n`,
)
logger.write(
` if (!!tint && width && height) return <${capitalized} fill={tint} width={width} height={height} />\n`,
)
logger.write(
` if (!!stroke && !width && !height) return <${capitalized} stroke={stroke} />\n`,
)
logger.write(
` if (!!stroke && width && height) return <${capitalized} stroke={stroke} width={width} height={height} />\n`,
)
logger.write(
` if (!stroke && !tint && width && height) return <${capitalized} width={width} height={height} />\n`,
)
logger.write(` return <${capitalized} />\n`)
}
logger.write(` }
}
const AppIcon: React.FC<AppIconProps> = ({
type,
testID,
nativeID,
style,
isVisible,
tint,
stroke,
width,
height
}) => {
if (isVisible === false) return null
return (
<View accessibilityLabel={testID} nativeID={nativeID} pointerEvents={'none'} style={style}>
{getIcon(type, tint, stroke, width, height)}
</View>
)
}
export default memo(AppIcon)
`)