-
Notifications
You must be signed in to change notification settings - Fork 3
/
icons.create.json.js
44 lines (37 loc) · 1.2 KB
/
icons.create.json.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
const { optimize } = require('svgo');
const fs = require('fs');
const path = require('path');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
// dir iconos svg
const dirIcons = path.join(process.cwd(), 'icons');
const optimizeSVG = (path) => {
const svgString = fs.readFileSync(path, 'utf8');
const result = optimize(svgString, {
multipass: true,
});
return result.data;
}
const getBody = (svgContent) => {
const dom = new JSDOM(svgContent);
const svg = dom.window.document.documentElement.querySelector('svg');
return svg.innerHTML;
}
const svgOptimized = () => {
let svgOptimized = {};
let files = fs.readdirSync(dirIcons);
files.forEach((fileName) => {
let pathSVG = `${dirIcons}/${fileName}`;
let cleanFileName = fileName.replace('.svg', '').replace('-', '_');
svgOptimized[cleanFileName] = getBody(optimizeSVG(pathSVG));
})
return svgOptimized;
}
// create file icons.json all icons
const createFileJSON = () => {
const data = svgOptimized();
const fileData = fs.readFileSync('icons.json', 'utf8');
fs.writeFileSync('icons.json', JSON.stringify(data, null, 4));
return data;
}
console.log(createFileJSON())