-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicons.mjs
62 lines (57 loc) · 1.64 KB
/
icons.mjs
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
import { transform } from '@svgr/core';
import chokidar from 'chokidar';
import _ from 'lodash';
import fs from 'node:fs';
import path from 'node:path';
const iconsDir = './src/assets/icons';
const reactIconsDir = './src/assets/react-icons';
// Ensure the react-icons directory exists
if (!fs.existsSync(reactIconsDir)) {
fs.mkdirSync(reactIconsDir, { recursive: true });
}
// Function to convert SVG to React component
const convertSvgToReact = async (filePath) => {
const fileName = path.basename(filePath, '.svg');
const componentName = _.upperFirst(_.camelCase(fileName));
const outputFilePath = path.join(reactIconsDir, `${componentName}.tsx`);
const svgCode = fs.readFileSync(filePath, 'utf8');
try {
const jsCode = await transform(
svgCode,
{
icon: true,
typescript: true,
plugins: [
'@svgr/plugin-svgo',
'@svgr/plugin-jsx',
// '@svgr/plugin-prettier',
],
svgoConfig: {
plugins: [
{
name: 'removeAttrs',
params: {
attrs: '(fill)',
},
},
],
},
svgProps: {
fill: 'currentColor',
},
},
{ componentName },
);
fs.writeFileSync(outputFilePath, jsCode);
// eslint-disable-next-line no-console
console.log(`Converted ${filePath} to ${outputFilePath}`);
} catch (error) {
console.error(`Error converting ${filePath}:`, error);
}
};
// Watch for changes in the icons directory
chokidar
.watch(iconsDir)
.on('add', convertSvgToReact)
// .on('unlink', convertSvgToReact)
.on('change', convertSvgToReact);