-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (37 loc) · 1.28 KB
/
index.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
const _ = require('lodash');
const cheerio = require('cheerio');
const fs = require('fs');
const humps = require('humps');
const sanitize = require('sanitize-filename');
const sourcePath = `${__dirname}/node_modules/simple-icons/icons`;
const destinationPath = `${__dirname}/icons`;
const componentTemplate = _.template(_.trim(`
import React from 'react';
import SvgIcon from '@material-ui/core/SvgIcon';
export default function (props) {
return (
<SvgIcon <%= props %>{...props}>
<%= markup %>
</SvgIcon>
);
}
`));
if (!fs.existsSync(destinationPath)) {
fs.mkdirSync(destinationPath);
}
fs.readdirSync(sourcePath).forEach(file => {
const $ = cheerio.load(fs.readFileSync(`${sourcePath}/${file}`, 'utf8'));
const svg = $('svg');
const name = sanitize(file.substr(0, file.lastIndexOf('.')) || file);
const markup = svg.html().replace('fill-rule', 'fillRule').replace(' class=', ' className=');
let props = '';
_.forEach(_.get(svg, '0.attribs', {}), (value, key) => {
if (key === 'class') {
props += `className=${value}`;
} else {
props += `${key.includes('aria') ? key : humps.camelize(key)}="${value}" `;
}
});
const component = componentTemplate({ name, markup, props });
fs.writeFileSync(`${destinationPath}/${name}.js`, component);
});