forked from stakwork/sphinx-nav-fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgLibBuilder.js
63 lines (50 loc) · 1.88 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
/* 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)
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)
console.log(`Converted ${file} to ${componentName}.tsx`)
}
}
} catch (error) {
console.error('Error processing SVG files:', error)
}
}
processFiles()