-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgenerate-icon-exports.js
96 lines (85 loc) · 2.32 KB
/
generate-icon-exports.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// This is a simple node script to make named exports available for consumers.
// This allows for: import { NamedIcon } from 'feather-icons-react';
// this file is run at build time and generates React components.
// it does not get included in the published npm package
const fs = require('fs');
const { appendFile } = require('fs/promises');
const containsLine = (path, line) => {
const fileData = fs.readFileSync(path);
return fileData.includes(line);
};
const appendLineIfLacking = (path, line) => {
if (!containsLine(path, line)) {
appendFile(path, `${line}\n`);
}
};
const iconsText = fs.readFileSync('src/icons.json');
const iconsJson = JSON.parse(iconsText);
const dashCasetoTitleCase = (inputString) =>
inputString
.toLowerCase()
.replace(/(?:^|[\s-/])\w/g, (match) => match.toUpperCase())
.replace(/-/g, '');
// component file function
const createComponent = ({
iconName,
iconMarkup,
iconKey,
}) => `import React from 'react';
const ${iconName} = ({
size = 24,
className = '',
fill = 'none',
...otherProps
}) => {
return (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill={fill}
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className={\`feather feather-${iconKey} \${className}\`}
{...otherProps}
><g>${iconMarkup}</g>
</svg>
);
};
export default ${iconName};
`;
// creates obj of { iconMarkup, iconName }
const iconNames = Object.keys(iconsJson).map((iconKey) => {
const iconMarkup = iconsJson[iconKey];
const iconName = dashCasetoTitleCase(iconKey);
return {
iconMarkup,
iconName,
iconKey,
};
});
// make the subdirectory for icon components
const iconComponentsDir = `src/IconComponents`;
fs.mkdir(iconComponentsDir, { recursive: true }, (err) => {
if (err) throw err;
});
const exportList = [];
iconNames.forEach((icon) => {
const { iconName } = icon;
fs.writeFile(
`${iconComponentsDir}/${iconName}.js`,
createComponent(icon),
(err) => {
if (err) throw err;
}
);
exportList.push(
`export { default as ${iconName} } from './IconComponents/${iconName}';`
);
});
exportList.forEach((exportLine) => {
appendLineIfLacking('src/index.js', exportLine);
});
console.log('Finished building and creating feather icon components!');