-
Notifications
You must be signed in to change notification settings - Fork 3
/
icons.rebuild.js
51 lines (43 loc) · 1.71 KB
/
icons.rebuild.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
// parse the svg files in the "icons" folder and if necessary add classNames and opacity attribute
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const fs = require('fs');
const path = require('path');
// add classNames
const addClassNames = (path) => {
const dom = new JSDOM(fs.readFileSync(path, 'utf8'));
const svg = dom.window.document.documentElement.querySelector('svg');
if (!svg.classList.contains('duoicon')) {
svg.classList.add('duoicon')
}
svg.querySelectorAll('svg *').forEach(item => {
item.removeAttribute('class');
// remove style
if (item.hasAttribute('style') && [".3", "0.3"].includes(item.style['opacity'])) {
item.style.removeProperty("opacity");
item.setAttribute('opacity', '.3');
}
if ((item.hasAttribute("opacity") && [".3", "0.3"].includes(item.getAttribute("opacity")))) {
item.classList.add('duoicon-secondary-layer');
} else {
item.classList.add('duoicon-primary-layer');
}
if (item.hasAttribute('style') && !item.getAttribute('style')) {
item.removeAttribute('style')
}
if (item.classList.contains('secondary-layer')) {
item.classList.replace('secondary-layer', 'duoicon-secondary-layer')
}
if (item.classList.contains('primary-layer')) {
item.classList.replace('primary-layer', 'duoicon-primary-layer')
}
})
return svg.outerHTML;
}
const dir = path.join(process.cwd(), 'icons');
let files = fs.readdirSync(dir);
files.forEach((fileName) => {
let pathSVG = `${dir}/${fileName}`;
let content = addClassNames(pathSVG);
fs.writeFileSync(pathSVG, content, 'utf8');
})