-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-svgs.js
31 lines (25 loc) · 980 Bytes
/
generate-svgs.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
const svgstore = require('svgstore');
const fs = require('fs');
const path = require('path');
let sprite = svgstore();
const type = process.argv[process.argv.length - 1];
const folder = `./src/assets/sprites`; // путь к папке с svg иконками
const getFilesFromFolder = folder => {
return fs.existsSync(folder)
? fs
.readdirSync(folder, { withFileTypes: true })
.filter(item => !item.isDirectory())
.map(item => {
return { name: item.name, folder };
})
: [];
};
const generateSprite = (outputPath = './src/assets/sprites/sprite.svg') => {
const svgSprites = [...getFilesFromFolder(folder)];
svgSprites.sort((a, b) => a.name.localeCompare(b.name));
svgSprites.forEach(file => {
sprite = sprite.add(file.name.split('.')[0], fs.readFileSync(path.join(file.folder, file.name), 'utf8'));
});
fs.writeFileSync(outputPath, sprite);
};
generateSprite(`./src/assets/sprites/sprite.svg`); // путь, куда выгрузится sprite