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

394 lines
10 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-12 11:59:18 +01:00
glob = require('glob'),
fs = require('fs'),
path = require('path'),
p = require('./package.json'),
zip = require('gulp-zip'),
2020-03-22 13:01:56 +01:00
puppeteer = require('puppeteer'),
argv = require('minimist')(process.argv.slice(2));
2020-03-12 11:59:18 +01:00
2020-03-16 21:32:48 +01:00
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const svgToPng = async (filePath, destination) => {
filePath = path.join(__dirname, filePath);
const htmlFilePath = path.join("file:", filePath);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({
height: 24,
width: 24,
deviceScaleFactor: 10
});
await page.goto(htmlFilePath);
await page.screenshot({
path: path.join(__dirname, destination),
omitBackground: true,
fullPage: true
});
await browser.close();
return page;
};
2020-03-12 11:59:18 +01:00
const createScreenshot = async (filePath) => {
try {
filePath = path.join(__dirname, filePath);
2020-03-12 14:47:47 +01:00
const fileName = filePath.replace('.svg', '');
2020-03-12 11:59:18 +01:00
const htmlFilePath = path.join("file:", filePath);
const browser = await puppeteer.launch();
const page = await browser.newPage();
2020-03-22 13:01:56 +01:00
2020-03-12 11:59:18 +01:00
await page.setViewport({
2020-03-21 22:33:22 +01:00
height: 10,
width: 10,
deviceScaleFactor: 2
2020-03-12 11:59:18 +01:00
});
await page.goto(htmlFilePath);
await page.screenshot({
path: `${fileName}.png`,
omitBackground: false,
fullPage: true
});
2020-03-01 16:06:35 +01:00
2020-03-12 11:59:18 +01:00
await browser.close();
} catch (error) {
console.error(error);
throw Error(error);
}
};
2020-03-01 16:06:35 +01:00
2020-03-21 22:33:22 +01:00
2020-03-22 17:08:19 +01:00
const printChangelog = function (newIcons, modifiedIcons, renamedIcons, pretty = false) {
2020-03-22 13:01:56 +01:00
if (newIcons.length > 0) {
2020-03-22 17:08:19 +01:00
if(pretty) {
console.log(`### ${newIcons.length} new icons:`);
2020-03-21 22:33:22 +01:00
2020-03-22 17:08:19 +01:00
newIcons.forEach(function (icon, i) {
console.log(`- \`${icon}\``);
});
} else {
let str = '';
str += `${newIcons.length} new icons: `;
2020-03-21 22:33:22 +01:00
2020-03-22 17:08:19 +01:00
newIcons.forEach(function (icon, i) {
str += `\`${icon}\``;
if ((i + 1) <= newIcons.length - 1) {
str += ', '
}
});
console.log(str);
}
2020-03-21 22:33:22 +01:00
console.log('');
}
2020-03-22 13:01:56 +01:00
if (modifiedIcons.length > 0) {
2020-03-21 22:33:22 +01:00
let str = '';
str += `Fixed icons: `;
2020-03-22 13:01:56 +01:00
modifiedIcons.forEach(function (icon, i) {
2020-03-21 22:33:22 +01:00
str += `\`${icon}\``;
2020-03-22 13:01:56 +01:00
if ((i + 1) <= modifiedIcons.length - 1) {
2020-03-21 22:33:22 +01:00
str += ', '
}
});
console.log(str);
console.log('');
}
2020-03-22 13:01:56 +01:00
if (renamedIcons.length > 0) {
2020-03-21 22:33:22 +01:00
console.log(`Renamed icons: `);
2020-03-22 13:01:56 +01:00
renamedIcons.forEach(function (icon, i) {
2020-03-21 22:33:22 +01:00
console.log(`- \`${icon[0]}\` renamed to \`${icon[1]}\``);
});
}
};
2020-03-22 17:38:00 +01:00
const generateIconsPreview = function(files, destFile, cb, columnsCount = 17, paddingOuter = 5) {
2020-03-22 13:01:56 +01:00
const padding = 29,
iconSize = 24;
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');
2020-03-21 22:33:22 +01:00
2020-03-22 13:01:56 +01:00
let svgFile = fs.readFileSync(file),
svgFileContent = svgFile.toString();
2020-03-21 22:33:22 +01:00
2020-03-22 13:01:56 +01:00
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;
}
});
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>`;
fs.writeFileSync(destFile, svgContent);
createScreenshot(destFile);
cb();
};
//*********************************************************************************************
gulp.task('build-zip', function () {
2020-03-21 22:33:22 +01:00
const version = p.version;
return gulp.src('{icons/**/*,icons-png/**/*,tabler-sprite.svg,tabler-sprite-nostroke.svg}')
.pipe(zip(`tabler-icons-${version}.zip`))
.pipe(gulp.dest('packages'))
});
2020-03-22 13:01:56 +01:00
gulp.task('build-jekyll', function (cb) {
cp.exec('bundle exec jekyll build', function () {
2020-03-21 22:33:22 +01:00
cb();
});
});
2020-03-22 13:01:56 +01:00
gulp.task('build-copy', function (cb) {
cp.exec('mkdir -p icons/ && rm -fd ./icons/* && cp ./_site/icons/* ./icons', function () {
2020-03-21 22:33:22 +01:00
cb();
});
});
2020-03-22 13:01:56 +01:00
gulp.task('clean-png', function (cb) {
cp.exec('rm -fd ./icons-png/*', function () {
2020-03-21 22:33:22 +01:00
cb();
});
});
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 23:00:25 +01:00
svgContent += `<symbol id="tabler-${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-12 15:04:56 +01:00
fs.writeFileSync('tabler-sprite.svg', svg);
fs.writeFileSync('tabler-sprite-nostroke.svg', svg.replace(/stroke-width="2"\s/g, ''));
2020-03-10 00:29:22 +01:00
cb();
});
});
gulp.task('icons-preview', function (cb) {
2020-03-22 16:47:31 +01:00
glob("icons/*.svg", {}, function (er, files) {
2020-03-22 13:01:56 +01:00
generateIconsPreview(files, '.github/icons.svg', cb);
2020-03-01 16:06:35 +01:00
});
});
2020-03-02 00:59:10 +01:00
2020-03-21 22:33:22 +01:00
gulp.task('icons-stroke', gulp.series('build-jekyll', function (cb) {
const icon = "disabled",
strokes = ['.5', '1', '1.5', '2', '2.75'],
2020-03-21 22:33:22 +01:00
svgFileContent = fs.readFileSync(`icons/${icon}.svg`).toString(),
2020-03-05 23:53:38 +01:00
padding = 16,
paddingOuter = 5,
2020-03-21 22:33:22 +01:00
iconSize = 32,
2020-03-21 22:35:21 +01:00
width = 882,
height = iconSize + paddingOuter * 2;
let svgContentSymbols = '',
svgContentIcons = '',
x = paddingOuter;
2020-03-22 13:01:56 +01:00
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-21 22:33:22 +01:00
fs.writeFileSync('.github/icons-stroke.svg', svgContent);
createScreenshot('.github/icons-stroke.svg');
cb();
2020-03-21 22:33:22 +01:00
}));
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|rect)/g, "\n <$1")
2020-03-02 16:42:02 +01:00
.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-21 14:05:39 +01:00
.replace(/a\s?([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s?([0-1])\s?([0-1])\s?(-?[0-9.]+)\s?(-?[0-9.]+)/g, 'a$1 $2 $3 $4 $5 $6 $7')
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
2020-03-12 11:59:18 +01:00
2020-03-22 13:01:56 +01:00
gulp.task('changelog-commit', function (cb) {
cp.exec('git status', function (err, ret) {
2020-03-21 22:33:22 +01:00
let newIcons = [], modifiedIcons = [], renamedIcons = [];
2020-03-12 11:59:18 +01:00
2020-03-21 22:33:22 +01:00
ret.replace(/new file:\s+src\/_icons\/([a-z1-9-]+)\.svg/g, function (m, fileName) {
newIcons.push(fileName);
});
ret.replace(/modified:\s+src\/_icons\/([a-z1-9-]+)\.svg/g, function (m, fileName) {
modifiedIcons.push(fileName);
});
ret.replace(/renamed:\s+src\/_icons\/([a-z1-9-]+).svg -> src\/_icons\/([a-z1-9-]+).svg/g, function (m, fileNameBefore, fileNameAfter) {
renamedIcons.push([fileNameBefore, fileNameAfter]);
});
2020-03-22 13:01:56 +01:00
modifiedIcons = modifiedIcons.filter(function (el) {
return newIcons.indexOf(el) < 0;
2020-03-21 22:33:22 +01:00
});
printChangelog(newIcons, modifiedIcons, renamedIcons);
2020-03-09 22:18:31 +01:00
2020-03-12 11:59:18 +01:00
cb();
});
2020-03-09 22:18:31 +01:00
});
2020-03-12 11:59:18 +01:00
2020-03-22 16:47:31 +01:00
gulp.task('changelog', function (cb) {
2020-03-22 13:01:56 +01:00
const version = argv['latest-tag'] || `v${p.version}`;
2020-03-21 22:33:22 +01:00
2020-03-22 16:47:31 +01:00
if(version) {
cp.exec(`git diff ${version} HEAD --name-status`, function (err, ret) {
2020-03-21 22:33:22 +01:00
2020-03-22 16:47:31 +01:00
let newIcons = [], modifiedIcons = [], renamedIcons = [];
2020-03-21 22:33:22 +01:00
2020-03-22 16:47:31 +01:00
ret.replace(/A\s+src\/_icons\/([a-z1-9-]+)\.svg/g, function (m, fileName) {
newIcons.push(fileName);
});
2020-03-21 22:33:22 +01:00
2020-03-22 16:47:31 +01:00
ret.replace(/M\s+src\/_icons\/([a-z1-9-]+)\.svg/g, function (m, fileName) {
modifiedIcons.push(fileName);
});
2020-03-21 22:33:22 +01:00
2020-03-22 16:47:31 +01:00
ret.replace(/R[0-9]+\s+src\/_icons\/([a-z1-9-]+)\.svg\s+src\/_icons\/([a-z1-9-]+).svg/g, function (m, fileNameBefore, fileNameAfter) {
renamedIcons.push([fileNameBefore, fileNameAfter]);
});
modifiedIcons = modifiedIcons.filter(function (el) {
return newIcons.indexOf(el) < 0;
});
2020-03-22 17:08:19 +01:00
printChangelog(newIcons, modifiedIcons, renamedIcons, true);
2020-03-22 16:47:31 +01:00
cb();
2020-03-21 22:33:22 +01:00
});
2020-03-22 16:47:31 +01:00
}
});
2020-03-21 22:33:22 +01:00
2020-03-22 16:47:31 +01:00
gulp.task('changelog-image', function (cb) {
const version = argv['latest-version'] || `${p.version}`,
newVersion = argv['new-version'] || `${p.version}`;
if(version) {
cp.exec(`git diff v${version} HEAD --name-status`, function (err, ret) {
let newIcons = [];
2020-03-21 22:33:22 +01:00
2020-03-22 16:47:31 +01:00
ret.replace(/[AD]\s+src\/_icons\/([a-z1-9-]+)\.svg/g, function (m, fileName) {
newIcons.push(fileName);
});
newIcons = newIcons.map(function(icon){
return `./icons/${icon}.svg`;
});
if(newIcons.length > 0) {
2020-03-22 17:38:53 +01:00
generateIconsPreview(newIcons, `.github/tabler-icons-${newVersion}.svg`, cb, 6, 24);
2020-03-22 16:47:31 +01:00
} else {
cb();
}
});
} else {
2020-03-17 22:05:54 +01:00
cb();
2020-03-22 16:47:31 +01:00
}
2020-03-17 22:05:54 +01:00
});
2020-03-21 22:33:22 +01:00
2020-03-17 22:05:54 +01:00
gulp.task('svg-to-png', gulp.series('build-jekyll', 'clean-png', async (cb) => {
2020-03-22 16:47:31 +01:00
let files = glob.sync("./icons/*.svg");
2020-03-16 21:32:48 +01:00
await asyncForEach(files, async function (file, i) {
let name = path.basename(file, '.svg');
console.log('name', name);
await svgToPng(file, `icons-png/${name}.png`);
});
cb();
}));
2020-03-22 16:47:31 +01:00
gulp.task('build', gulp.series('optimize', 'build-jekyll', 'build-copy', 'icons-sprite', 'icons-preview', 'svg-to-png', 'changelog-image', 'build-zip'));