audio-vis/build/task/spriteBuilder.js

106 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-04-07 21:44:46 +02:00
const faSvgSprite = require('fontawesome-svg-sprite-generator'),
SVGSpriter = require('svg-sprite'),
path = require('path'),
fs = require('fs'),
fal = require('@fortawesome/pro-light-svg-icons'),
far = require('@fortawesome/pro-regular-svg-icons'),
fas = require('@fortawesome/pro-solid-svg-icons'),
fsextra = require('node-fs-extra'),
gulp = require('gulp');
let toMergeData = [];
function buildIconSprites() {
let config = {
src: __dirname + '/../../raw/icons',
dest: __dirname + '/../../out/icon-sprite.svg',
spriter: {
shape: {
id: {
generator: function (name) {
return "vt-" + name.replace('.svg', '');
}
}
},
"svg": {
"xmlDeclaration": false,
"doctypeDeclaration": false
},
"mode": {
"symbol": true
}
}
};
let icons = {
fa: [
fal.faPlay,
fal.faPause,
fal.faCaretRight,
fal.faCaretLeft,
fal.faRandom,
fal.faCogs,
fal.faFolderUpload,
fal.faListMusic,
2020-08-01 21:51:54 +02:00
fal.faFileAudio,
2020-04-07 21:44:46 +02:00
],
vt: []
};
builder.init(config, icons);
builder.generateFontAwesome();
builder.generateFromIcons();
return gulp.src(config.src);
}
let builder = {
init: function (config, icons) {
this.config = config;
this.icons = icons;
},
generateFontAwesome: function () {
let sprite = faSvgSprite.generate(this.icons.fa, {license: '', xmlDeclaration: false});
toMergeData.push(sprite.svg);
},
generateFromIcons: function () {
builder.merge();
},
merge: function () {
if (toMergeData.length === 0) {
return;
}
let data = '<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg">';
for (let item of toMergeData) {
data += item.replace('</svg>', '').replace(/(<svg)([^<]*|[^>]*)/g, '');
}
data += '</svg>';
let dirname = path.dirname(builder.config.dest);
if (!fs.existsSync(dirname)) {
fsextra.mkdirpSync(dirname);
}
fs.writeFileSync(builder.config.dest, data);
},
readFiles: function (dir, spriter, icons) {
let files = fs.readdirSync(dir);
for (let file of files) {
let split = file.split('.');
let suffix = split[split.length - 1];
if (suffix === 'svg' && icons.indexOf('vt-' + split[0]) !== -1) {
let filePath = path.resolve(dir, file);
spriter.add(
filePath,
file,
fs.readFileSync(filePath, {encoding: 'utf-8'})
);
}
}
}
};
module.exports.buildIconSprites = buildIconSprites;