forked from stakwork/sphinx-nav-fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgLibBuilder.js
96 lines (76 loc) · 2.91 KB
/
svgLibBuilder.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
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
const fs = require('fs').promises
const path = require('path')
const prettier = require('prettier')
const parser = require('prettier/parser-babel')
const inputDirectory = './public/svg-icons'
const outputDirectory = './src/components/Icons'
function removeMaskTypeAlpha(svgContent) {
return svgContent.replace(/style="[^"]*mask-type:alpha[^"]*"/g, '')
}
async function processFiles() {
try {
try {
await fs.access(outputDirectory)
} catch (error) {
await fs.mkdir(outputDirectory)
}
const files = await fs.readdir(inputDirectory)
const iconComponents = []
for (const file of files) {
if (path.extname(file) === '.svg') {
const componentName = path.basename(file, '.svg')
const svgContent = await fs.readFile(path.join(inputDirectory, file), 'utf-8')
const cleanSvg = removeMaskTypeAlpha(svgContent)
const modifiedSVG = cleanSvg.replace(/width="\d+(\.\d+)?" height="\d+(\.\d+)?" /g, 'width="1em" height="1em" ')
const finalSVG = modifiedSVG.replace(/fill="[^"]*"/g, 'fill="currentColor"')
const componentCode = `
/* eslint-disable */
import React from 'react';
const ${componentName}: React.FC<React.SVGProps<SVGSVGElement>> = (props) => (
${finalSVG}
);
export default ${componentName};
`
const formattedComponentCode = await prettier.format(componentCode, {
parser: 'babel-ts',
semi: true,
singleQuote: true,
trailingComma: 'all',
plugins: [parser],
})
await fs.writeFile(path.join(outputDirectory, `${componentName}.tsx`), formattedComponentCode)
iconComponents.push(componentName)
console.log(`Converted ${file} to ${componentName}.tsx`)
}
}
// Generate index.tsx file with type definition and string literal keys
const imports = iconComponents
.map((componentName) => `import ${componentName} from './${componentName}';`)
.join('\n')
const iconsObject = iconComponents
.map((componentName) => ` '${componentName}': ${componentName},`) // Ensure keys are in quotes
.join('\n')
const indexFileContent = `
/* eslint-disable */
import React from 'react';
${imports}
export const Icons: Record<string, React.FC<React.SVGProps<SVGSVGElement>>> = {
${iconsObject}
};
`
const formattedIndexFileContent = await prettier.format(indexFileContent, {
parser: 'babel-ts',
semi: true,
singleQuote: true,
trailingComma: 'all',
plugins: [parser],
})
await fs.writeFile(path.join(outputDirectory, 'index.tsx'), formattedIndexFileContent)
console.log('Generated index.tsx with Icons object and types')
} catch (error) {
console.error('Error processing SVG files:', error)
}
}
processFiles()