gulpfile.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. const gulp = require('gulp'),
  2. cp = require('child_process'),
  3. glob = require('glob'),
  4. fs = require('fs'),
  5. path = require('path'),
  6. p = require('./package.json'),
  7. zip = require('gulp-zip'),
  8. puppeteer = require('puppeteer'),
  9. outlineStroke = require('svg-outline-stroke'),
  10. iconfont = require('gulp-iconfont'),
  11. template = require('lodash.template'),
  12. sass = require('node-sass'),
  13. cleanCSS = require('clean-css'),
  14. argv = require('minimist')(process.argv.slice(2));
  15. async function asyncForEach(array, callback) {
  16. for (let index = 0; index < array.length; index++) {
  17. await callback(array[index], index, array);
  18. }
  19. }
  20. const svgToPng = async (filePath, destination) => {
  21. filePath = path.join(__dirname, filePath);
  22. const htmlFilePath = path.join("file:", filePath);
  23. const browser = await puppeteer.launch();
  24. const page = await browser.newPage();
  25. await page.setViewport({
  26. height: 24,
  27. width: 24,
  28. deviceScaleFactor: 10
  29. });
  30. await page.goto(htmlFilePath);
  31. await page.screenshot({
  32. path: path.join(__dirname, destination),
  33. omitBackground: true,
  34. fullPage: true
  35. });
  36. await browser.close();
  37. return page;
  38. };
  39. const createScreenshot = async (filePath) => {
  40. try {
  41. filePath = path.join(__dirname, filePath);
  42. const fileName = filePath.replace('.svg', '');
  43. const htmlFilePath = path.join("file:", filePath);
  44. const browser = await puppeteer.launch();
  45. const page = await browser.newPage();
  46. await page.setViewport({
  47. height: 10,
  48. width: 10,
  49. deviceScaleFactor: 2
  50. });
  51. await page.goto(htmlFilePath);
  52. await page.screenshot({
  53. path: `${fileName}.png`,
  54. omitBackground: false,
  55. fullPage: true
  56. });
  57. await browser.close();
  58. } catch (error) {
  59. console.error(error);
  60. throw Error(error);
  61. }
  62. };
  63. const printChangelog = function (newIcons, modifiedIcons, renamedIcons, pretty = false) {
  64. if (newIcons.length > 0) {
  65. if (pretty) {
  66. console.log(`### ${newIcons.length} new icons:`);
  67. newIcons.forEach(function (icon, i) {
  68. console.log(`- \`${icon}\``);
  69. });
  70. } else {
  71. let str = '';
  72. str += `${newIcons.length} new icons: `;
  73. newIcons.forEach(function (icon, i) {
  74. str += `\`${icon}\``;
  75. if ((i + 1) <= newIcons.length - 1) {
  76. str += ', '
  77. }
  78. });
  79. console.log(str);
  80. }
  81. console.log('');
  82. }
  83. if (modifiedIcons.length > 0) {
  84. let str = '';
  85. str += `Fixed icons: `;
  86. modifiedIcons.forEach(function (icon, i) {
  87. str += `\`${icon}\``;
  88. if ((i + 1) <= modifiedIcons.length - 1) {
  89. str += ', '
  90. }
  91. });
  92. console.log(str);
  93. console.log('');
  94. }
  95. if (renamedIcons.length > 0) {
  96. console.log(`Renamed icons: `);
  97. renamedIcons.forEach(function (icon, i) {
  98. console.log(`- \`${icon[0]}\` renamed to \`${icon[1]}\``);
  99. });
  100. }
  101. };
  102. const generateIconsPreview = function (files, destFile, cb, columnsCount = 18, paddingOuter = 3) {
  103. const padding = 28,
  104. iconSize = 24;
  105. const iconsCount = files.length,
  106. rowsCount = Math.ceil(iconsCount / columnsCount),
  107. width = columnsCount * (iconSize + padding) + 2 * paddingOuter - padding,
  108. height = rowsCount * (iconSize + padding) + 2 * paddingOuter - padding;
  109. let svgContentSymbols = '',
  110. svgContentIcons = '',
  111. x = paddingOuter,
  112. y = paddingOuter;
  113. files.forEach(function (file, i) {
  114. let name = path.basename(file, '.svg');
  115. let svgFile = fs.readFileSync(file),
  116. svgFileContent = svgFile.toString();
  117. svgFileContent = svgFileContent
  118. .replace('<svg xmlns="http://www.w3.org/2000/svg"', `<symbol id="${name}"`)
  119. .replace(' width="24" height="24"', '')
  120. .replace('</svg>', '</symbol>')
  121. .replace(/\n\s+/g, '');
  122. svgContentSymbols += `\t${svgFileContent}\n`;
  123. svgContentIcons += `\t<use xlink:href="#${name}" x="${x}" y="${y}" width="${iconSize}" height="${iconSize}" />\n`;
  124. x += padding + iconSize;
  125. if (i % columnsCount === columnsCount - 1) {
  126. x = paddingOuter;
  127. y += padding + iconSize;
  128. }
  129. });
  130. 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>`;
  131. fs.writeFileSync(destFile, svgContent);
  132. createScreenshot(destFile);
  133. cb();
  134. };
  135. //*********************************************************************************************
  136. gulp.task('iconfont-prepare', function (cb) {
  137. cp.exec('mkdir -p icons-outlined/ && rm -fd ./icons-outlined/* && mkdir -p && rm -fd ./iconfont/*', function () {
  138. cb();
  139. });
  140. });
  141. gulp.task('iconfont-clean', function (cb) {
  142. cp.exec('rm -rf ./icons-outlined', function () {
  143. cb();
  144. });
  145. });
  146. gulp.task('iconfont-svg-outline', function (cb) {
  147. cp.exec('mkdir -p icons-outlined/ && rm -fd ./icons-outlined/*', async () => {
  148. let files = glob.sync("./icons/*.svg");
  149. let iconfontUnicode = {};
  150. if(fs.existsSync('./iconfont-unicode.json')) {
  151. iconfontUnicode = require('./iconfont-unicode');
  152. }
  153. await asyncForEach(files, async function (file) {
  154. const name = path.basename(file, '.svg'),
  155. unicode = iconfontUnicode[name];
  156. await console.log('Stroke for:', file, unicode);
  157. let strokedSVG = fs.readFileSync(file).toString();
  158. strokedSVG = strokedSVG
  159. .replace('width="24"', 'width="1000"')
  160. .replace('height="24"', 'height="1000"');
  161. await outlineStroke(strokedSVG, {
  162. optCurve: false,
  163. steps: 4,
  164. round: 0,
  165. centerHorizontally: true,
  166. fixedWidth: true,
  167. color: 'black'
  168. }).then(outlined => {
  169. if(unicode) {
  170. fs.writeFileSync(`icons-outlined/u${unicode.toUpperCase()}-${name}.svg`, outlined);
  171. } else {
  172. fs.writeFileSync(`icons-outlined/${name}.svg`, outlined);
  173. }
  174. }).catch(error => console.log(error));
  175. });
  176. cb();
  177. });
  178. });
  179. gulp.task('iconfont', function () {
  180. let maxUnicode = 59905;
  181. if(fs.existsSync('./iconfont-unicode.json')) {
  182. const iconfontUnicode = require('./iconfont-unicode');
  183. for(const name in iconfontUnicode) {
  184. const unicode = parseInt(iconfontUnicode[name], 16);
  185. maxUnicode = Math.max(maxUnicode, unicode);
  186. }
  187. }
  188. maxUnicode = maxUnicode + 1;
  189. return gulp.src(['icons-outlined/*.svg'])
  190. .pipe(iconfont({
  191. fontName: 'tabler-icons',
  192. prependUnicode: true,
  193. formats: ['ttf', 'eot', 'woff', 'woff2'],
  194. normalize: true,
  195. startUnicode: maxUnicode
  196. }))
  197. .on('glyphs', function (glyphs, options) {
  198. //glyphs json
  199. let glyphsObject = {};
  200. //sort glypht
  201. glyphs = glyphs.sort(function(a, b){
  202. return ('' + a.name).localeCompare(b.name)
  203. });
  204. glyphs.forEach(function (glyph) {
  205. glyphsObject[glyph.name] = glyph.unicode[0].codePointAt(0).toString(16);
  206. });
  207. fs.writeFileSync(`iconfont-unicode.json`, JSON.stringify(glyphsObject));
  208. //css
  209. options['glyphs'] = glyphs;
  210. options['v'] = p.version;
  211. const compiled = template(fs.readFileSync('.build/iconfont.scss').toString());
  212. const result = compiled(options);
  213. fs.writeFileSync('iconfont/tabler-icons.scss', result);
  214. //html
  215. const compiledHtml = template(fs.readFileSync('.build/iconfont.html').toString());
  216. const resultHtml = compiledHtml(options);
  217. fs.writeFileSync('iconfont/tabler-icons.html', resultHtml);
  218. })
  219. .pipe(gulp.dest('iconfont/fonts'));
  220. });
  221. gulp.task('iconfont-css', function (cb) {
  222. sass.render({
  223. file: 'iconfont/tabler-icons.scss',
  224. outputStyle: 'expanded'
  225. }, function (err, result) {
  226. fs.writeFileSync('iconfont/tabler-icons.css', result.css);
  227. const cleanOutput = new cleanCSS({}).minify(result.css);
  228. fs.writeFileSync('iconfont/tabler-icons.min.css', cleanOutput.styles);
  229. cb();
  230. });
  231. });
  232. gulp.task('build-iconfont', gulp.series('iconfont-prepare', 'iconfont-svg-outline', 'iconfont', 'iconfont-css', 'iconfont-clean'));
  233. gulp.task('build-zip', function () {
  234. const version = p.version;
  235. return gulp.src('{icons/**/*,icons-png/**/*,iconfont/**/*,tabler-sprite.svg,tabler-sprite-nostroke.svg}')
  236. .pipe(zip(`tabler-icons-${version}.zip`))
  237. .pipe(gulp.dest('packages'))
  238. });
  239. gulp.task('build-jekyll', function (cb) {
  240. cp.exec('bundle exec jekyll build', function () {
  241. cb();
  242. });
  243. });
  244. gulp.task('build-copy', function (cb) {
  245. cp.exec('mkdir -p icons/ && rm -fd ./icons/* && cp ./_site/icons/* ./icons && cp ./_site/tags.json .', function () {
  246. cb();
  247. });
  248. });
  249. gulp.task('clean-png', function (cb) {
  250. cp.exec('rm -fd ./icons-png/*', function () {
  251. cb();
  252. });
  253. });
  254. gulp.task('icons-sprite', function (cb) {
  255. glob("_site/icons/*.svg", {}, function (er, files) {
  256. let svgContent = '';
  257. files.forEach(function (file, i) {
  258. let name = path.basename(file, '.svg'),
  259. svgFile = fs.readFileSync(file),
  260. svgFileContent = svgFile.toString();
  261. svgFileContent = svgFileContent
  262. .replace(/<svg[^>]+>/g, '')
  263. .replace(/<\/svg>/g, '')
  264. .replace(/\n+/g, '')
  265. .replace(/>\s+</g, '><')
  266. .trim();
  267. 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>`
  268. });
  269. let svg = `<svg xmlns="http://www.w3.org/2000/svg"><defs>${svgContent}</defs></svg>`;
  270. fs.writeFileSync('tabler-sprite.svg', svg);
  271. fs.writeFileSync('tabler-sprite-nostroke.svg', svg.replace(/stroke-width="2"\s/g, ''));
  272. cb();
  273. });
  274. });
  275. gulp.task('icons-preview', function (cb) {
  276. glob("icons/*.svg", {}, function (er, files) {
  277. generateIconsPreview(files, '.github/icons.svg', cb);
  278. });
  279. });
  280. gulp.task('icons-stroke', gulp.series('build-jekyll', function (cb) {
  281. const icon = "disabled",
  282. strokes = ['.5', '1', '1.5', '2', '2.75'],
  283. svgFileContent = fs.readFileSync(`icons/${icon}.svg`).toString(),
  284. padding = 16,
  285. paddingOuter = 3,
  286. iconSize = 32,
  287. width = 914,
  288. height = iconSize + paddingOuter * 2;
  289. let svgContentSymbols = '',
  290. svgContentIcons = '',
  291. x = paddingOuter;
  292. strokes.forEach(function (stroke) {
  293. let svgFileContentStroked = svgFileContent
  294. .replace('<svg xmlns="http://www.w3.org/2000/svg"', `<symbol id="icon-${stroke}"`)
  295. .replace(' width="24" height="24"', '')
  296. .replace(' stroke-width="2"', ` stroke-width="${stroke}"`)
  297. .replace('</svg>', '</symbol>')
  298. .replace(/\n\s+/g, '');
  299. svgContentSymbols += `\t${svgFileContentStroked}\n`;
  300. svgContentIcons += `\t<use xlink:href="#icon-${stroke}" x="${x}" y="${paddingOuter}" width="${iconSize}" height="${iconSize}" />\n`;
  301. x += padding + iconSize;
  302. });
  303. 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>`;
  304. fs.writeFileSync('.github/icons-stroke.svg', svgContent);
  305. createScreenshot('.github/icons-stroke.svg');
  306. cb();
  307. }));
  308. gulp.task('optimize', function (cb) {
  309. glob("src/_icons/*.svg", {}, function (er, files) {
  310. files.forEach(function (file, i) {
  311. let svgFile = fs.readFileSync(file),
  312. svgFileContent = svgFile.toString();
  313. svgFileContent = svgFileContent
  314. .replace(/><\/(polyline|line|rect|circle|path)>/g, '/>')
  315. .replace(/rx="([^"]+)"\s+ry="\1"/g, 'rx="$1"')
  316. .replace(/\s?\/>/g, ' />')
  317. .replace(/\n\s*<(line|circle|path|polyline|rect)/g, "\n <$1")
  318. .replace(/polyline points="([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s([0-9.]+)"/g, 'line x1="$1" y1="$2" x2="$3" y2="$4"')
  319. .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')
  320. .replace(/\n\n+/g, "\n");
  321. if(svgFile.toString() !== svgFileContent) {
  322. fs.writeFileSync(file, svgFileContent);
  323. }
  324. });
  325. cb();
  326. });
  327. });
  328. gulp.task('changelog-commit', function (cb) {
  329. cp.exec('git status', function (err, ret) {
  330. let newIcons = [], modifiedIcons = [], renamedIcons = [];
  331. ret.replace(/new file:\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  332. newIcons.push(fileName);
  333. });
  334. ret.replace(/modified:\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  335. modifiedIcons.push(fileName);
  336. });
  337. ret.replace(/renamed:\s+src\/_icons\/([a-z0-9-]+).svg -> src\/_icons\/([a-z0-9-]+).svg/g, function (m, fileNameBefore, fileNameAfter) {
  338. renamedIcons.push([fileNameBefore, fileNameAfter]);
  339. });
  340. modifiedIcons = modifiedIcons.filter(function (el) {
  341. return newIcons.indexOf(el) < 0;
  342. });
  343. printChangelog(newIcons, modifiedIcons, renamedIcons);
  344. cb();
  345. });
  346. });
  347. gulp.task('changelog', function (cb) {
  348. const version = argv['latest-tag'] || `v${p.version}`;
  349. if (version) {
  350. cp.exec(`git diff ${version} HEAD --name-status`, function (err, ret) {
  351. let newIcons = [], modifiedIcons = [], renamedIcons = [];
  352. ret.replace(/A\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  353. newIcons.push(fileName);
  354. });
  355. ret.replace(/M\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  356. modifiedIcons.push(fileName);
  357. });
  358. ret.replace(/R[0-9]+\s+src\/_icons\/([a-z0-9-]+)\.svg\s+src\/_icons\/([a-z0-9-]+).svg/g, function (m, fileNameBefore, fileNameAfter) {
  359. renamedIcons.push([fileNameBefore, fileNameAfter]);
  360. });
  361. modifiedIcons = modifiedIcons.filter(function (el) {
  362. return newIcons.indexOf(el) < 0;
  363. });
  364. printChangelog(newIcons, modifiedIcons, renamedIcons, true);
  365. cb();
  366. });
  367. }
  368. });
  369. gulp.task('changelog-image', function (cb) {
  370. const version = argv['latest-version'] || `${p.version}`,
  371. newVersion = argv['new-version'] || `${p.version}`;
  372. if (version) {
  373. cp.exec(`git diff v${version} HEAD --name-status`, function (err, ret) {
  374. let newIcons = [];
  375. ret.replace(/[AD]\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  376. newIcons.push(fileName);
  377. });
  378. newIcons = newIcons.map(function (icon) {
  379. return `./icons/${icon}.svg`;
  380. });
  381. if (newIcons.length > 0) {
  382. generateIconsPreview(newIcons, `.github/tabler-icons-${newVersion}.svg`, cb, 6, 24);
  383. } else {
  384. cb();
  385. }
  386. });
  387. } else {
  388. cb();
  389. }
  390. });
  391. gulp.task('svg-to-png', gulp.series('build-jekyll', 'clean-png', async (cb) => {
  392. let files = glob.sync("./icons/*.svg");
  393. await asyncForEach(files, async function (file, i) {
  394. let name = path.basename(file, '.svg');
  395. console.log('name', name);
  396. await svgToPng(file, `icons-png/${name}.png`);
  397. });
  398. cb();
  399. }));
  400. gulp.task('build', gulp.series('optimize', 'build-jekyll', 'build-copy', 'icons-sprite', 'icons-preview', 'svg-to-png', 'build-iconfont', 'changelog-image', 'build-zip'));