gulpfile.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. csv = require('csv-parser'),
  8. zip = require('gulp-zip'),
  9. svgo = require('gulp-svgo'),
  10. puppeteer = require('puppeteer'),
  11. outlineStroke = require('svg-outline-stroke'),
  12. iconfont = require('gulp-iconfont'),
  13. template = require('lodash.template'),
  14. sass = require('node-sass'),
  15. cleanCSS = require('clean-css'),
  16. argv = require('minimist')(process.argv.slice(2)),
  17. svgParse = require('parse-svg-path'),
  18. svgpath = require('svgpath'),
  19. svgr = require('@svgr/core').default;
  20. let compileOptions = {
  21. includeIcons: [],
  22. strokeWidth: null,
  23. fontForge: "fontforge"
  24. };
  25. if (fs.existsSync('./compile-options.json')) {
  26. try {
  27. let tempOptions = require('./compile-options');
  28. if (typeof tempOptions!="object") {
  29. throw "Compile options file does not contain an json object";
  30. }
  31. if (typeof tempOptions.includeIcons!="undefined") {
  32. if (!Array.isArray(tempOptions.includeIcons)) {
  33. throw "property inludeIcons is not an array";
  34. }
  35. compileOptions.includeIcons= tempOptions.includeIcons;
  36. }
  37. if (typeof tempOptions.strokeWidth!="undefined") {
  38. if (typeof tempOptions.strokeWidth!="string" && typeof tempOptions.strokeWidth!="number") {
  39. throw "property strokeWidth is not a string or number";
  40. }
  41. compileOptions.strokeWidth=tempOptions.strokeWidth.toString();
  42. }
  43. if (typeof tempOptions.fontForge!="undefined") {
  44. if (typeof tempOptions.fontForge!="string") {
  45. throw "property fontForge is not a string";
  46. }
  47. compileOptions.fontForge=tempOptions.fontForge;
  48. }
  49. } catch (error) {
  50. throw `Error reading compile-options.json: ${error}`
  51. }
  52. }
  53. async function asyncForEach(array, callback) {
  54. for (let index = 0; index < array.length; index++) {
  55. await callback(array[index], index, array);
  56. }
  57. }
  58. const svgToPng = async (filePath, destination) => {
  59. filePath = path.join(__dirname, filePath);
  60. const htmlFilePath = path.join("file:", filePath);
  61. const browser = await puppeteer.launch();
  62. const page = await browser.newPage();
  63. await page.setViewport({
  64. height: 24,
  65. width: 24,
  66. deviceScaleFactor: 10
  67. });
  68. await page.goto(htmlFilePath);
  69. await page.screenshot({
  70. path: path.join(__dirname, destination),
  71. omitBackground: true,
  72. fullPage: true
  73. });
  74. await browser.close();
  75. return page;
  76. };
  77. const createScreenshot = async (filePath) => {
  78. try {
  79. filePath = path.join(__dirname, filePath);
  80. const fileName = filePath.replace('.svg', '');
  81. const htmlFilePath = path.join("file:", filePath);
  82. const browser = await puppeteer.launch();
  83. const page = await browser.newPage();
  84. await page.setViewport({
  85. height: 10,
  86. width: 10,
  87. deviceScaleFactor: 2
  88. });
  89. await page.goto(htmlFilePath);
  90. await page.screenshot({
  91. path: `${fileName}.png`,
  92. omitBackground: false,
  93. fullPage: true
  94. });
  95. await browser.close();
  96. } catch (error) {
  97. console.error(error);
  98. throw Error(error);
  99. }
  100. };
  101. const printChangelog = function (newIcons, modifiedIcons, renamedIcons, pretty = false) {
  102. if (newIcons.length > 0) {
  103. if (pretty) {
  104. console.log(`### ${newIcons.length} new icons:`);
  105. newIcons.forEach(function (icon, i) {
  106. console.log(`- \`${icon}\``);
  107. });
  108. } else {
  109. let str = '';
  110. str += `${newIcons.length} new icons: `;
  111. newIcons.forEach(function (icon, i) {
  112. str += `\`${icon}\``;
  113. if ((i + 1) <= newIcons.length - 1) {
  114. str += ', '
  115. }
  116. });
  117. console.log(str);
  118. }
  119. console.log('');
  120. }
  121. if (modifiedIcons.length > 0) {
  122. let str = '';
  123. str += `Fixed icons: `;
  124. modifiedIcons.forEach(function (icon, i) {
  125. str += `\`${icon}\``;
  126. if ((i + 1) <= modifiedIcons.length - 1) {
  127. str += ', '
  128. }
  129. });
  130. console.log(str);
  131. console.log('');
  132. }
  133. if (renamedIcons.length > 0) {
  134. console.log(`Renamed icons: `);
  135. renamedIcons.forEach(function (icon, i) {
  136. console.log(`- \`${icon[0]}\` renamed to \`${icon[1]}\``);
  137. });
  138. }
  139. };
  140. const generateIconsPreview = function (files, destFile, cb, columnsCount = 17, paddingOuter = 7) {
  141. const padding = 26,
  142. iconSize = 24;
  143. const iconsCount = files.length,
  144. rowsCount = Math.ceil(iconsCount / columnsCount),
  145. width = columnsCount * (iconSize + padding) + 2 * paddingOuter - padding,
  146. height = rowsCount * (iconSize + padding) + 2 * paddingOuter - padding;
  147. let svgContentSymbols = '',
  148. svgContentIcons = '',
  149. x = paddingOuter,
  150. y = paddingOuter;
  151. files.forEach(function (file, i) {
  152. let name = path.basename(file, '.svg');
  153. let svgFile = fs.readFileSync(file),
  154. svgFileContent = svgFile.toString();
  155. svgFileContent = svgFileContent
  156. .replace('<svg xmlns="http://www.w3.org/2000/svg"', `<symbol id="${name}"`)
  157. .replace(' width="24" height="24"', '')
  158. .replace('</svg>', '</symbol>')
  159. .replace(/\n\s+/g, '');
  160. svgContentSymbols += `\t${svgFileContent}\n`;
  161. svgContentIcons += `\t<use xlink:href="#${name}" x="${x}" y="${y}" width="${iconSize}" height="${iconSize}" />\n`;
  162. x += padding + iconSize;
  163. if (i % columnsCount === columnsCount - 1) {
  164. x = paddingOuter;
  165. y += padding + iconSize;
  166. }
  167. });
  168. 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>`;
  169. fs.writeFileSync(destFile, svgContent);
  170. createScreenshot(destFile);
  171. cb();
  172. };
  173. //*********************************************************************************************
  174. gulp.task('iconfont-prepare', function (cb) {
  175. cp.exec('mkdir -p icons-outlined/ && rm -fd ./icons-outlined/* && mkdir -p && rm -fd ./iconfont/*', function () {
  176. cb();
  177. });
  178. });
  179. gulp.task('iconfont-clean', function (cb) {
  180. cp.exec('rm -rf ./icons-outlined', function () {
  181. cb();
  182. });
  183. });
  184. gulp.task('iconfont-svg-outline', function (cb) {
  185. cp.exec('mkdir -p icons-outlined/ && rm -fd ./icons-outlined/*', async () => {
  186. let files = glob.sync("./icons/*.svg");
  187. let iconfontUnicode = {};
  188. if (fs.existsSync('./.build/iconfont-unicode.json')) {
  189. iconfontUnicode = require('./.build/iconfont-unicode');
  190. }
  191. await asyncForEach(files, async function (file) {
  192. const name = path.basename(file, '.svg');
  193. if (compileOptions.includeIcons.length==0 || compileOptions.includeIcons.indexOf(name)>=0) {
  194. unicode = iconfontUnicode[name];
  195. await console.log('Stroke for:', file, unicode);
  196. let strokedSVG = fs.readFileSync(file).toString();
  197. strokedSVG = strokedSVG
  198. .replace('width="24"', 'width="1000"')
  199. .replace('height="24"', 'height="1000"');
  200. if (compileOptions.strokeWidth) {
  201. strokedSVG = strokedSVG.replace('stroke-width="2"', `stroke-width="${compileOptions.strokeWidth}"`);
  202. }
  203. await outlineStroke(strokedSVG, {
  204. optCurve: false,
  205. steps: 4,
  206. round: 0,
  207. centerHorizontally: true,
  208. fixedWidth: true,
  209. color: 'black'
  210. }).then(outlined => {
  211. if (unicode) {
  212. fs.writeFileSync(`icons-outlined/u${unicode.toUpperCase()}-${name}.svg`, outlined);
  213. } else {
  214. fs.writeFileSync(`icons-outlined/${name}.svg`, outlined);
  215. }
  216. }).catch(error => console.log(error));
  217. }
  218. });
  219. cb();
  220. });
  221. });
  222. gulp.task('iconfont-optimize', function() {
  223. return gulp.src('icons-outlined/*')
  224. .pipe(svgo())
  225. .pipe(gulp.dest('icons-outlined'));
  226. });
  227. gulp.task('iconfont-fix-outline', function(cb) {
  228. var fontForge= compileOptions.fontForge;
  229. // correct svg outline directions in a child process using fontforge
  230. const generate = cp.spawn(fontForge, ["-lang=py", "-script", "./fix-outline.py"], { stdio: 'inherit' });
  231. generate.on("close", function (code) {
  232. console.log(`Correcting svg outline directions exited with code ${code}`);
  233. if (!code) {
  234. cb();
  235. }
  236. });
  237. });
  238. gulp.task('iconfont', function () {
  239. let maxUnicode = 59905;
  240. if (fs.existsSync('./.build/iconfont-unicode.json')) {
  241. const iconfontUnicode = require('./.build/iconfont-unicode');
  242. for (const name in iconfontUnicode) {
  243. const unicode = parseInt(iconfontUnicode[name], 16);
  244. maxUnicode = Math.max(maxUnicode, unicode);
  245. }
  246. }
  247. maxUnicode = maxUnicode + 1;
  248. return gulp.src(['icons-outlined/*.svg'])
  249. .pipe(iconfont({
  250. fontName: 'tabler-icons',
  251. prependUnicode: true,
  252. formats: ['ttf', 'eot', 'woff', 'woff2', 'svg'],
  253. normalize: true,
  254. startUnicode: maxUnicode,
  255. fontHeight: 1000,
  256. descent: 100,
  257. ascent: 986.5
  258. }))
  259. .on('glyphs', function (glyphs, options) {
  260. //glyphs json
  261. let glyphsObject = {};
  262. //sort glypht
  263. glyphs = glyphs.sort(function (a, b) {
  264. return ('' + a.name).localeCompare(b.name)
  265. });
  266. glyphs.forEach(function (glyph) {
  267. glyphsObject[glyph.name] = glyph.unicode[0].codePointAt(0).toString(16);
  268. });
  269. fs.writeFileSync(`./.build/iconfont-unicode.json`, JSON.stringify(glyphsObject));
  270. //css
  271. options['glyphs'] = glyphs;
  272. options['v'] = p.version;
  273. const compiled = template(fs.readFileSync('.build/iconfont.scss').toString());
  274. const result = compiled(options);
  275. fs.writeFileSync('iconfont/tabler-icons.scss', result);
  276. //html
  277. const compiledHtml = template(fs.readFileSync('.build/iconfont.html').toString());
  278. const resultHtml = compiledHtml(options);
  279. fs.writeFileSync('iconfont/tabler-icons.html', resultHtml);
  280. })
  281. .pipe(gulp.dest('iconfont/fonts'));
  282. });
  283. gulp.task('iconfont-css', function (cb) {
  284. sass.render({
  285. file: 'iconfont/tabler-icons.scss',
  286. outputStyle: 'expanded'
  287. }, function (err, result) {
  288. fs.writeFileSync('iconfont/tabler-icons.css', result.css);
  289. const cleanOutput = new cleanCSS({}).minify(result.css);
  290. fs.writeFileSync('iconfont/tabler-icons.min.css', cleanOutput.styles);
  291. cb();
  292. });
  293. });
  294. gulp.task('update-tags-unicode', function(cb) {
  295. let tags = require('./tags.json'),
  296. unicodes = require('./.build/iconfont-unicode.json');
  297. for(let i in tags) {
  298. tags[i] = {
  299. ...tags[i],
  300. unicode: unicodes[i],
  301. }
  302. }
  303. console.log('tags', tags);
  304. fs.writeFileSync(`tags.json`, JSON.stringify(tags, null, 2));
  305. cb();
  306. });
  307. gulp.task('build-iconfont', gulp.series('iconfont-prepare', 'iconfont-svg-outline', 'iconfont-fix-outline', 'iconfont-optimize', 'iconfont', 'iconfont-css', 'iconfont-clean', 'update-tags-unicode'));
  308. gulp.task('build-zip', function () {
  309. const version = p.version;
  310. return gulp.src('{icons/**/*,icons-png/**/*,icons-react/**/*,iconfont/**/*,tabler-sprite.svg,tabler-sprite-nostroke.svg}')
  311. .pipe(zip(`tabler-icons-${version}.zip`))
  312. .pipe(gulp.dest('packages'))
  313. });
  314. gulp.task('build-jekyll', function (cb) {
  315. cp.exec('bundle exec jekyll build', function () {
  316. cb();
  317. });
  318. });
  319. gulp.task('build-copy', function (cb) {
  320. cp.exec('mkdir -p icons/ && rm -fd ./icons/* && cp ./_site/icons/* ./icons && cp ./_site/tags.json .', function () {
  321. cb();
  322. });
  323. });
  324. gulp.task('clean-png', function (cb) {
  325. cp.exec('rm -fd ./icons-png/*', function () {
  326. cb();
  327. });
  328. });
  329. gulp.task('icons-sprite', function (cb) {
  330. glob("_site/icons/*.svg", {}, function (er, files) {
  331. let svgContent = '';
  332. files.forEach(function (file, i) {
  333. let name = path.basename(file, '.svg'),
  334. svgFile = fs.readFileSync(file),
  335. svgFileContent = svgFile.toString();
  336. svgFileContent = svgFileContent
  337. .replace(/<svg[^>]+>/g, '')
  338. .replace(/<\/svg>/g, '')
  339. .replace(/\n+/g, '')
  340. .replace(/>\s+</g, '><')
  341. .trim();
  342. 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>`
  343. });
  344. let svg = `<svg xmlns="http://www.w3.org/2000/svg"><defs>${svgContent}</defs></svg>`;
  345. fs.writeFileSync('tabler-sprite.svg', svg);
  346. fs.writeFileSync('tabler-sprite-nostroke.svg', svg.replace(/stroke-width="2"\s/g, ''));
  347. cb();
  348. });
  349. });
  350. gulp.task('icons-preview', function (cb) {
  351. glob("icons/*.svg", {}, function (er, files) {
  352. generateIconsPreview(files, '.github/icons.svg', cb);
  353. });
  354. });
  355. gulp.task('icons-stroke', gulp.series('build-jekyll', function (cb) {
  356. const icon = "disabled",
  357. strokes = ['.5', '1', '1.5', '2', '2.75'],
  358. svgFileContent = fs.readFileSync(`icons/${icon}.svg`).toString(),
  359. padding = 16,
  360. paddingOuter = 3,
  361. iconSize = 32,
  362. width = 914,
  363. height = iconSize + paddingOuter * 2;
  364. let svgContentSymbols = '',
  365. svgContentIcons = '',
  366. x = paddingOuter;
  367. strokes.forEach(function (stroke) {
  368. let svgFileContentStroked = svgFileContent
  369. .replace('<svg xmlns="http://www.w3.org/2000/svg"', `<symbol id="icon-${stroke}"`)
  370. .replace(' width="24" height="24"', '')
  371. .replace(' stroke-width="2"', ` stroke-width="${stroke}"`)
  372. .replace('</svg>', '</symbol>')
  373. .replace(/\n\s+/g, '');
  374. svgContentSymbols += `\t${svgFileContentStroked}\n`;
  375. svgContentIcons += `\t<use xlink:href="#icon-${stroke}" x="${x}" y="${paddingOuter}" width="${iconSize}" height="${iconSize}" />\n`;
  376. x += padding + iconSize;
  377. });
  378. 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>`;
  379. fs.writeFileSync('.github/icons-stroke.svg', svgContent);
  380. createScreenshot('.github/icons-stroke.svg');
  381. cb();
  382. }));
  383. gulp.task('optimize', function (cb) {
  384. const addFloats = function (n1, n2) {
  385. return Math.round((parseFloat(n1) + parseFloat(n2)) * 1000) / 1000
  386. };
  387. const optimizePath = function(path) {
  388. let transformed = svgpath(path)
  389. .rel()
  390. .round(3)
  391. .toString();
  392. return svgParse(transformed).map(function(a){
  393. return a.join(' ');
  394. }).join(' ');
  395. };
  396. glob("src/_icons/*.svg", {}, function (er, files) {
  397. files.forEach(function (file, i) {
  398. let svgFile = fs.readFileSync(file),
  399. svgFileContent = svgFile.toString();
  400. svgFileContent = svgFileContent
  401. .replace(/><\/(polyline|line|rect|circle|path)>/g, '/>')
  402. .replace(/rx="([^"]+)"\s+ry="\1"/g, 'rx="$1"')
  403. .replace(/\s?\/>/g, ' />')
  404. .replace(/\n\s*<(line|circle|path|polyline|rect)/g, "\n <$1")
  405. .replace(/polyline points="([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s([0-9.]+)"/g, 'line x1="$1" y1="$2" x2="$3" y2="$4"')
  406. .replace(/<path d="([^"]+)"/g, function(f, r1) {
  407. r1 = optimizePath(r1);
  408. return `<path d="${r1}"`;
  409. })
  410. .replace(/d="m/g, 'd="M')
  411. .replace(/([Aa])\s?([0-9.]+)\s([0-9.]+)\s([0-9.]+)\s?([0-1])\s?([0-1])\s?(-?[0-9.]+)\s?(-?[0-9.]+)/gi, '$1$2 $3 $4 $5 $6 $7 $8')
  412. .replace(/\n\n+/g, "\n")
  413. .replace(/<path d="M([0-9.]*) ([0-9.]*)l\s?([-0-9.]*) ([-0-9.]*)"/g, function (f, r1, r2, r3, r4) {
  414. return `<line x1="${r1}" y1="${r2}" x2="${addFloats(r1, r3)}" y2="${addFloats(r2, r4)}"`;
  415. })
  416. .replace(/<path d="M([0-9.]*) ([0-9.]*)v\s?([-0-9.]*)"/g, function (f, r1, r2, r3) {
  417. return `<line x1="${r1}" y1="${r2}" x2="${r1}" y2="${addFloats(r2, r3)}"`;
  418. })
  419. .replace(/<path d="M([0-9.]*) ([0-9.]*)h\s?([-0-9.]*)"/g, function (f, r1, r2, r3) {
  420. return `<line x1="${r1}" y1="${r2}" x2="${addFloats(r1, r3)}" y2="${r2}"`;
  421. })
  422. .replace(/<path d="([^"]+)"/g, function (f, r1) {
  423. r1 = r1
  424. .replace(/ -0\./g, " -.")
  425. .replace(/ 0\./g, " .")
  426. .replace(/\s([a-z])/gi, "$1")
  427. .replace(/([a-z])\s/gi, "$1");
  428. return `<path d="${r1}"`;
  429. })
  430. ;
  431. if (svgFile.toString() !== svgFileContent) {
  432. fs.writeFileSync(file, svgFileContent);
  433. }
  434. });
  435. cb();
  436. });
  437. });
  438. gulp.task('changelog-commit', function (cb) {
  439. cp.exec('git status', function (err, ret) {
  440. let newIcons = [], modifiedIcons = [], renamedIcons = [];
  441. ret.replace(/new file:\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  442. newIcons.push(fileName);
  443. });
  444. ret.replace(/modified:\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  445. modifiedIcons.push(fileName);
  446. });
  447. ret.replace(/renamed:\s+src\/_icons\/([a-z0-9-]+).svg -> src\/_icons\/([a-z0-9-]+).svg/g, function (m, fileNameBefore, fileNameAfter) {
  448. renamedIcons.push([fileNameBefore, fileNameAfter]);
  449. });
  450. modifiedIcons = modifiedIcons.filter(function (el) {
  451. return newIcons.indexOf(el) < 0;
  452. });
  453. printChangelog(newIcons, modifiedIcons, renamedIcons);
  454. cb();
  455. });
  456. });
  457. gulp.task('changelog', function (cb) {
  458. const version = argv['latest-tag'] || `v${p.version}`;
  459. if (version) {
  460. cp.exec(`git diff ${version} HEAD --name-status`, function (err, ret) {
  461. let newIcons = [], modifiedIcons = [], renamedIcons = [];
  462. ret.replace(/A\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  463. newIcons.push(fileName);
  464. });
  465. ret.replace(/M\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  466. modifiedIcons.push(fileName);
  467. });
  468. ret.replace(/R[0-9]+\s+src\/_icons\/([a-z0-9-]+)\.svg\s+src\/_icons\/([a-z0-9-]+).svg/g, function (m, fileNameBefore, fileNameAfter) {
  469. renamedIcons.push([fileNameBefore, fileNameAfter]);
  470. });
  471. modifiedIcons = modifiedIcons.filter(function (el) {
  472. return newIcons.indexOf(el) < 0;
  473. });
  474. printChangelog(newIcons, modifiedIcons, renamedIcons, true);
  475. cb();
  476. });
  477. }
  478. });
  479. gulp.task('changelog-image', function (cb) {
  480. const version = argv['latest-version'] || `${p.version}`,
  481. newVersion = argv['new-version'] || `${p.version}`;
  482. if (version) {
  483. cp.exec(`git diff v${version} HEAD --name-status`, function (err, ret) {
  484. let newIcons = [];
  485. ret.replace(/[A]\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  486. newIcons.push(fileName);
  487. });
  488. newIcons = newIcons.map(function (icon) {
  489. return `./icons/${icon}.svg`;
  490. });
  491. if (newIcons.length > 0) {
  492. generateIconsPreview(newIcons, `.github/tabler-icons-${newVersion}.svg`, cb, 6, 24);
  493. } else {
  494. cb();
  495. }
  496. });
  497. } else {
  498. cb();
  499. }
  500. });
  501. gulp.task('svg-to-png', gulp.series('build-jekyll', 'clean-png', async (cb) => {
  502. let files = glob.sync("./icons/*.svg");
  503. await asyncForEach(files, async function (file, i) {
  504. let name = path.basename(file, '.svg');
  505. console.log('name', name);
  506. await svgToPng(file, `icons-png/${name}.png`);
  507. });
  508. cb();
  509. }));
  510. gulp.task('clean-react', function (cb) {
  511. cp.exec('rm -fd ./icons-react/* && mkdir icons-react/icons-js', function () {
  512. cb();
  513. });
  514. });
  515. gulp.task('svg-to-react', gulp.series('clean-react', async function (cb) {
  516. let files = glob.sync("./icons/*.svg");
  517. const camelize = function (str) {
  518. str = str.replace(/-/g, ' ');
  519. return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
  520. return word.toUpperCase();
  521. }).replace(/\s+/g, '');
  522. };
  523. const componentName = function (file) {
  524. file = path.basename(file, '.svg');
  525. file = camelize(`Icon ${file}`);
  526. return file;
  527. };
  528. const optimizeSvgCode = function(svgCode) {
  529. return svgCode.replace('<path stroke="none" d="M0 0h24v24H0z"/>', '');
  530. };
  531. let indexCode = '',
  532. indexDCode = `import { FC, SVGAttributes } from 'react';\n\ninterface TablerIconProps extends SVGAttributes<SVGElement> { color?: string; size?: string | number; stroke?: string | number; }\n\ntype TablerIcon = FC<TablerIconProps>;\n\n`;
  533. await asyncForEach(files, async function (file) {
  534. const svgCode = optimizeSvgCode(fs.readFileSync(file).toString()),
  535. fileName = path.basename(file, '.svg') + '.js',
  536. iconComponentName = componentName(file);
  537. svgr(svgCode, {
  538. icon: false,
  539. svgProps: { width: '{size}', height: '{size}', strokeWidth: '{stroke}', stroke: '{color}' },
  540. template: require('./.build/svgr-template')
  541. }, { componentName: iconComponentName }).then(jsCode => {
  542. fs.writeFileSync('icons-react/icons-js/' + fileName, jsCode);
  543. indexCode += `export { default as ${iconComponentName} } from './icons-js/${fileName}';\n`;
  544. indexDCode += `export const ${iconComponentName}: TablerIcon;\n`;
  545. });
  546. fs.writeFileSync('icons-react/index.js', indexCode);
  547. fs.writeFileSync('icons-react/index.d.ts', indexDCode);
  548. });
  549. cb();
  550. }));
  551. const setVersions = function(version, files) {
  552. for(const i in files) {
  553. const file = files[i];
  554. if (fs.existsSync(`src/_icons/${file}.svg`)) {
  555. let svgFile = fs.readFileSync(`src/_icons/${file}.svg`).toString();
  556. if(!svgFile.match(/version: ([0-9.]+)/i)) {
  557. svgFile = svgFile.replace(/---\n<svg>/i, function(m){
  558. return `version: ${version}\n${m}`;
  559. });
  560. fs.writeFileSync(`src/_icons/${file}.svg`, svgFile);
  561. } else {
  562. console.log(`File ${file} already has version`);
  563. }
  564. } else {
  565. console.log(`File ${file} doesn't exists`);
  566. }
  567. }
  568. };
  569. gulp.task('update-icons-version', function (cb) {
  570. const version = argv['latest-version'] || `${p.version}`,
  571. newVersion = argv['new-version'] || `${p.version}`;
  572. if (version) {
  573. cp.exec(`git diff v${version} HEAD --name-status`, function (err, ret) {
  574. let newIcons = [];
  575. ret.replace(/[A]\s+src\/_icons\/([a-z0-9-]+)\.svg/g, function (m, fileName) {
  576. newIcons.push(fileName);
  577. });
  578. if(newIcons.length) {
  579. setVersions(newVersion.replace(/\.0$/, ''), newIcons);
  580. }
  581. });
  582. }
  583. cb();
  584. });
  585. gulp.task('import-tags', function(cb) {
  586. fs.createReadStream('./_import.csv')
  587. .pipe(csv({
  588. headers: false,
  589. separator: "\t"
  590. }))
  591. .on('data', (row) => {
  592. console.log(row[0], row[1]);
  593. const filename = `src/_icons/${row[0]}.svg`;
  594. let data = fs.readFileSync(filename).toString();
  595. data = data.replace(/(---[\s\S]+?---)/, function(m, headerContent){
  596. headerContent = headerContent.replace(/tags: .*\n/, '');
  597. headerContent = headerContent.replace(/---/, `---\ntags: [${row[1]}]`);
  598. return headerContent;
  599. });
  600. fs.writeFileSync(filename, data);
  601. })
  602. .on('end', () => {
  603. console.log('CSV file successfully processed');
  604. });
  605. cb();
  606. });
  607. gulp.task("build-react", function (cb) {
  608. cp.exec("npm run build-react", function () {
  609. cb();
  610. });
  611. });
  612. gulp.task('build', gulp.series('optimize', 'update-icons-version', 'build-jekyll', 'build-copy', 'icons-sprite', 'svg-to-react', 'build-react', 'icons-preview', 'svg-to-png', 'build-iconfont', 'changelog-image', 'build-zip'));