forked from EnterpriseDB/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateIconTypes.js
More file actions
52 lines (47 loc) · 1.37 KB
/
createIconTypes.js
File metadata and controls
52 lines (47 loc) · 1.37 KB
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
const { readdirSync, writeFileSync } = require("fs");
const isSVG = (file) => /.svg$/.test(file);
const removeExtension = (file) => file.split(".")[0];
// ThisIsPascalCase
const toPascalCase = (string) =>
string
.match(/[a-z0-9]+/gi)
.map((word) => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase())
.join("");
// THIS_IS_SCREAMING_SNAKE_CASE
const toScreamingSnakeCase = (string) =>
string.replace(/-/g, "_").toUpperCase();
const icons = readdirSync("static/icons").filter(isSVG).map(removeExtension);
const IconTypeContent = [
"import React from 'react';",
"import iconNames from './iconNames';",
"",
icons
.map(
(icon) =>
`import ${toPascalCase(
icon,
)}Svg from '../../../static/icons/${icon}.svg';`,
)
.join("\n"),
"",
"function formatIconName(name) {",
" return name && name.replace(/ /g, '').toLowerCase();",
"}",
"",
"export default function IconType({ iconName, ...rest }) {",
" switch (formatIconName(iconName)) {",
icons
.map(
(icon) =>
` case iconNames.${toScreamingSnakeCase(
icon,
)}:\n return <${toPascalCase(icon)}Svg {...rest} />;`,
)
.join("\n"),
" default:",
" return null;",
" }",
"};\n",
].join("\n");
writeFileSync(`src/components/icon/iconType.js`, IconTypeContent);
console.log("Icon Types file created! ✅");