forked from kickstand-ui/kickstand-ui
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathicon_loader.js
59 lines (49 loc) · 1.83 KB
/
icon_loader.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
#!/usr/bin/env node
const fs = require("fs-extra");
const glob = require("glob-promise");
const path = require("path");
const svgson = require("svgson");
glob("src/scripts/components/icon/icons/*.svg")
.then((filePaths) =>
Promise.all(
filePaths.map((fileName) => {
const file = fs.readFileSync(fileName, "utf-8");
const content = svgson.parseSync(file);
return new Promise((resolve) =>
resolve({ file: fileName, content })
);
})
)
)
.then((files) => {
const iconList = {};
files.forEach((svg) => {
let file = path.basename(svg.file).replace(".svg", "");
let iconGroup = svg.content.children.find(
(child) => child.name === "g" || child.name === "path"
);
updateSvgAttributes(iconGroup);
iconList[file] = svgson.stringify(iconGroup);
});
let filename = `src/scripts/components/icon/icon-list.ts`;
fs.writeFileSync(filename, buildIconFileContents(iconList), "utf8");
console.info("Icon Loading Complete");
process.exit(0);
});
function updateSvgAttributes(element) {
if(!element)
return;
delete element.attributes.id;
delete element.attributes["clip-path"];
delete element.attributes["data-name"];
if (element.attributes.fill)
element.attributes.fill =
element.attributes.fill === "none" ? "none" : "currentColor";
if (element.attributes.stroke)
element.attributes.stroke =
element.attributes.stroke === "none" ? "none" : "currentColor";
element.children.forEach((x) => updateSvgAttributes(x));
}
function buildIconFileContents(iconList) {
return `export const iconList = ${JSON.stringify(iconList, null, 4)};`;
}