forked from Torchlite/simple-icons-mui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (33 loc) · 1.11 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
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/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');
let props = '';
_.forEach(_.get(svg, '0.attribs', {}), (value, key) => {
props = props + `${humps.camelize(key)}="${value}" `;
});
const component = componentTemplate({ name, markup, props });
fs.writeFileSync(`${destinationPath}/${name}.js`, component);
});