-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-react-component.mjs
52 lines (48 loc) · 1.69 KB
/
generate-react-component.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
import path from 'path';
import {readdir, readFile, writeFile} from 'fs/promises';
import pascalcase from 'pascalcase';
import { optimize } from 'svgo'
const __dirname = import.meta.dirname
const svgs = (await readdir(path.join(__dirname, './svg'))).filter(file => file.endsWith('.svg'));
await Promise.all((svgs).map(async (file) => {
const svg = await readFile(path.join(__dirname, './svg', file), 'utf8');
const optimizedSvg = optimize(svg, {
plugins: [
'removeTitle',
{name: 'removeAttrs', params: {attrs: 'fill'}},
{
type: 'visitor',
name: 'reactnize-attr-names',
fn: () => {
return {
element: {
enter: node => {
Object.keys(node.attributes).forEach(attr => {
if (!attr.includes('-')) return;
node.attributes[attr.replace(/-(.)/g, (_, char) => char.toUpperCase())] = node.attributes[attr];
delete node.attributes[attr];
})
}
}
}
}
}
]
}).data;
const name = path.basename(file, '.svg');
await writeFile(
path.join(__dirname, './src/react', `${pascalcase(name)}.tsx`),
`import React from 'react';
function ${pascalcase(name)} (props: React.HTMLProps<SVGSVGElement>): React.ReactElement {
return (
${optimizedSvg.replace(/<svg(.*?)>/, `<svg $1 {...props}>`)}
);
}
export {${pascalcase(name)}};
`
);
}));
await writeFile(path.join(__dirname, './src/react/index.ts'), svgs.map(file => {
const name = path.basename(file, '.svg');
return `export {${pascalcase(name)} as Kamon${pascalcase(name)}} from './${pascalcase(name)}';`
}).join('\n'));