-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerateIcons.cjs
57 lines (45 loc) · 1.4 KB
/
generateIcons.cjs
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
/**
* run
* node generateIcons.cjs
* to generate the index.ts file in the icons directory
*/
const fs = require('fs')
const path = require('path')
const iconsDir = path.join(__dirname, 'src', 'icons')
const outputFile = path.join(iconsDir, 'index.ts')
function getAllSvgFiles(dir, fileList = [], relativePath = '') {
const files = fs.readdirSync(dir)
files.forEach((file) => {
const filePath = path.join(dir, file)
const fileStat = fs.statSync(filePath)
const fileRelativePath = path.join(relativePath, file)
if (fileStat.isDirectory()) {
if (relativePath.split(path.sep).length < 2) {
getAllSvgFiles(filePath, fileList, fileRelativePath)
}
} else if (path.extname(file) === '.svg') {
fileList.push(fileRelativePath)
}
})
return fileList
}
const svgFiles = getAllSvgFiles(iconsDir)
let importStatements = ''
let exportStatements = 'export default {\n'
svgFiles.forEach((file) => {
const iconNameParts = file
.split(path.sep)
.map(
(part) =>
part.charAt(0).toUpperCase() + part.slice(1).replace('.svg', ''),
)
const iconName = iconNameParts.join('')
importStatements += `import ${iconName} from './${file.replace(
/\\/g,
'/',
)}';\n`
exportStatements += ` ${iconName},\n`
})
exportStatements += '};\n'
fs.writeFileSync(outputFile, importStatements + '\n' + exportStatements)
console.log(`Generated ${outputFile}`)