1
0
mirror of https://github.com/tabler/tabler-icons.git synced 2025-01-17 20:58:34 +01:00
tabler-icons/gulpfile.js

147 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-03-01 16:06:35 +01:00
const gulp = require('gulp'),
2020-03-09 22:18:31 +01:00
cp = require('child_process'),
2020-03-01 16:06:35 +01:00
glob = require("glob"),
fs = require("fs"),
2020-03-09 22:18:31 +01:00
path = require("path");
2020-03-01 16:06:35 +01:00
gulp.task('icons-sprite', function (cb) {
2020-03-10 00:29:22 +01:00
glob("_site/icons/*.svg", {}, function (er, files) {
let svgContent = '';
files.forEach(function (file, i) {
let name = path.basename(file, '.svg'),
svgFile = fs.readFileSync(file),
svgFileContent = svgFile.toString();
svgFileContent = svgFileContent
.replace(/<svg[^>]+>/g, '')
.replace(/<\/svg>/g, '')
.replace(/\n+/g, '')
.replace(/>\s+</g, '><')
.trim();
2020-03-10 00:31:35 +01:00
svgContent += `<symbol id="${name}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${svgFileContent}</symbol>`
2020-03-10 00:29:22 +01:00
});
let svg = `<svg xmlns="http://www.w3.org/2000/svg"><defs>${svgContent}</defs></svg>`;
2020-03-10 22:48:47 +01:00
fs.writeFileSync('dist/icons-sprite.svg', svg);
2020-03-10 00:29:22 +01:00
cb();
});
});
gulp.task('icons-preview', function (cb) {
2020-03-05 23:53:38 +01:00
const columnsCount = 17,
padding = 29,
paddingOuter = 5,
2020-03-01 18:16:09 +01:00
iconSize = 24;
2020-03-01 16:06:35 +01:00
glob("_site/icons/*.svg", {}, function (er, files) {
const iconsCount = files.length,
rowsCount = Math.ceil(iconsCount / columnsCount),
width = columnsCount * (iconSize + padding) + 2 * paddingOuter - padding,
height = rowsCount * (iconSize + padding) + 2 * paddingOuter - padding;
let svgContentSymbols = '',
svgContentIcons = '',
x = paddingOuter,
y = paddingOuter;
files.forEach(function (file, i) {
let name = path.basename(file, '.svg');
let svgFile = fs.readFileSync(file),
svgFileContent = svgFile.toString();
svgFileContent = svgFileContent
.replace('<svg xmlns="http://www.w3.org/2000/svg"', `<symbol id="${name}"`)
.replace(' width="24" height="24"', '')
.replace('</svg>', '</symbol>')
.replace(/\n\s+/g, '');
svgContentSymbols += `\t${svgFileContent}\n`;
svgContentIcons += `\t<use xlink:href="#${name}" x="${x}" y="${y}" width="${iconSize}" height="${iconSize}" />\n`;
x += padding + iconSize;
if (i % columnsCount === columnsCount - 1) {
x = paddingOuter;
y += padding + iconSize;
}
});
2020-03-05 23:50:49 +01:00
const svgContent = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}" style="color: #354052"><rect x="0" y="0" width="${width}" height="${height}" fill="#fff"></rect>\n${svgContentSymbols}\n${svgContentIcons}\n</svg>`;
2020-03-01 16:06:35 +01:00
2020-03-10 22:12:47 +01:00
fs.writeFileSync('icons.svg', svgContent);
2020-03-01 16:06:35 +01:00
cb();
});
});
2020-03-02 00:59:10 +01:00
gulp.task('icons-stroke', function (cb) {
const icon = "disabled",
strokes = ['.5', '1', '1.5', '2', '2.75'],
2020-03-09 22:18:31 +01:00
svgFileContent = fs.readFileSync(`_site/icons/${icon}.svg`).toString(),
2020-03-05 23:53:38 +01:00
padding = 16,
paddingOuter = 5,
iconSize = 64,
width = (strokes.length * (iconSize + padding) - padding) + paddingOuter * 2,
height = iconSize + paddingOuter * 2;
let svgContentSymbols = '',
svgContentIcons = '',
x = paddingOuter;
strokes.forEach(function (stroke) {
let svgFileContentStroked = svgFileContent
.replace('<svg xmlns="http://www.w3.org/2000/svg"', `<symbol id="icon-${stroke}"`)
.replace(' width="24" height="24"', '')
.replace(' stroke-width="2"', ` stroke-width="${stroke}"`)
.replace('</svg>', '</symbol>')
.replace(/\n\s+/g, '');
svgContentSymbols += `\t${svgFileContentStroked}\n`;
svgContentIcons += `\t<use xlink:href="#icon-${stroke}" x="${x}" y="${paddingOuter}" width="${iconSize}" height="${iconSize}" />\n`;
x += padding + iconSize;
});
2020-03-05 23:50:49 +01:00
const svgContent = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}" style="color: #354052"><rect x="0" y="0" width="${width}" height="${height}" fill="#fff"></rect>\n${svgContentSymbols}\n${svgContentIcons}\n</svg>`;
2020-03-10 22:48:47 +01:00
fs.writeFileSync('icons-stroke.svg', svgContent);
cb();
});
gulp.task('optimize', function (cb) {
2020-03-09 22:29:48 +01:00
glob("src/_icons/*.svg", {}, function (er, files) {
2020-03-02 00:59:10 +01:00
files.forEach(function (file, i) {
let svgFile = fs.readFileSync(file),
svgFileContent = svgFile.toString();
svgFileContent = svgFileContent
2020-03-02 16:42:02 +01:00
.replace(/><\/(polyline|line|rect|circle|path)>/g, '/>')
.replace(/rx="([^"]+)"\s+ry="\1"/g, 'rx="$1"')
.replace(/\s?\/>/g, ' />')
.replace(/\n\s*<(line|circle|path|polyline)/g, "\n <$1")
.replace(/polyline points="([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s([0-9.]+)"/g, 'line x1="$1" y1="$2" x2="$3" y2="$4"')
2020-03-02 23:03:46 +01:00
.replace(/\s+"/g, '"')
2020-03-02 00:59:10 +01:00
.replace(/\n\n+/g, "\n");
2020-03-02 00:59:10 +01:00
fs.writeFileSync(file, svgFileContent);
});
cb();
});
});
2020-03-09 22:18:31 +01:00
gulp.task('build', function (cb) {
cp.exec('bundle exec jekyll build', function() {
2020-03-10 22:12:47 +01:00
cp.exec('rm -f ./dist/icons/* && cp ./_site/icons/* ./dist/icons', function() {
2020-03-09 22:18:31 +01:00
cb();
});
})
});