forked from ResearchHub/researchhub-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileiconCodeMod.js
78 lines (71 loc) · 2.26 KB
/
fileiconCodeMod.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
import icons from "./config/themes/icons.json";
module.exports = function (fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);
const fa = [
"fa-light",
"fa-duotone",
"fa-regular",
"fa-solid",
"fa-solid",
"fa-brands",
];
const allImports = [
"@fortawesome/pro-light-svg-icons",
"@fortawesome/pro-duotone-svg-icons",
"@fortawesome/pro-regular-svg-icons",
"@fortawesome/pro-solid-svg-icons",
"@fortawesome/free-solid-svg-icons",
"@fortawesome/fontawesome-free-brands",
];
for (let i = 0; i < allImports.length; i++) {
const curImport = allImports[i];
// Find the import statement for "@fortawesome/pro-duotone-svg-icons"
const importStatement = root.find(j.ImportDeclaration, {
source: {
value: curImport,
},
});
// Extract the imported icon names
const iconNames = importStatement
.find(j.ImportSpecifier)
.nodes()
.map((node) => node.imported.name);
// Find object with key "icons" and value of an object expression
const iconsObject = root.find(j.VariableDeclarator, {
id: { name: "icons" },
init: { type: "ObjectExpression" },
});
// Replace values of properties in icons object with equivalent <i> tags
iconNames.forEach((iconName) => {
const iconNameWithoutFa = iconName.slice(2);
const iconNameCamelCase = `${iconNameWithoutFa
.charAt(0)
.toLowerCase()}${iconNameWithoutFa.slice(1)}`;
iconsObject
.find(j.Property, { key: { name: iconNameCamelCase } })
.forEach((path) => {
j(path).replaceWith(
j.property(
"init",
j.identifier(iconNameCamelCase),
j.jsxElement(
j.jsxOpeningElement(j.jsxIdentifier("i"), [
j.jsxAttribute(
j.jsxIdentifier("className"),
j.literal(
`${fa[i]} fa-${iconNameCamelCase
.replace(/([a-z])([A-Z])/g, "$1-$2")
.toLowerCase()}`
)
),
]),
j.jsxClosingElement(j.jsxIdentifier("i"))
)
)
);
});
});
}
return root.toSource();
};