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, fal.faFileImage, fal.faQuestionCircle, ], 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 = ''; for (let item of toMergeData) { data += item.replace('', '').replace(/(]*)/g, ''); } data += ''; 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;