Skip to content

Instantly share code, notes, and snippets.

@zeromancer
Created February 16, 2018 14:26
Show Gist options
  • Save zeromancer/9369c8859e321e72a80f0c81700f4d41 to your computer and use it in GitHub Desktop.
Save zeromancer/9369c8859e321e72a80f0c81700f4d41 to your computer and use it in GitHub Desktop.
const fs = require("fs");
const pathRegex = /\sd="(.*)"/;
const basePath = `./icons/svg`;
const svgFiles = fs.readdirSync(basePath);
for (let svgFile of svgFiles) {
const name = svgFile
.split(/-/g)
.map(part => {
return part.charAt(0).toUpperCase() + part.slice(1);
})
.join("")
.slice(0, -4);
const content = fs.readFileSync(`${basePath}/${svgFile}`);
const pathMatches = pathRegex.exec(content);
const path = pathMatches && pathMatches[1];
// Skip on empty path
if (!path) continue;
const fileContent = `import React from 'react';
const ${name}Icon = ({ width = 24, height = 24, viewBox = '0 0 24 24', className, children, ...props }) => {
let classes = 'mdi-icon';
if (className) classes += \` \${className}\`;
return (
<svg {...props} width={width} height={height} viewBox={viewBox} className={classes}>
<path d="${path}" />
</svg>
);
};
export default ${name}Icon;
`;
const nameOrig = svgFile.replace(".svg", "");
const nameValid = name;
const line = `public static readonly ${nameValid} = "${nameOrig}";
`;
//console.log(nameValid, "=", nameOrig);
//fs.writeFileSync(`${__dirname}/../build/${name}Icon.js`, fileContent);
fs.appendFileSync(`./MdiIcons.ts`, line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment