@@ -4,6 +4,7 @@ const gulp = require('gulp'),
4
4
fs = require ( 'fs' ) ,
5
5
path = require ( 'path' ) ,
6
6
p = require ( './package.json' ) ,
7
+ csv = require ( 'csv-parser' ) ,
7
8
zip = require ( 'gulp-zip' ) ,
8
9
puppeteer = require ( 'puppeteer' ) ,
9
10
outlineStroke = require ( 'svg-outline-stroke' ) ,
@@ -12,6 +13,8 @@ const gulp = require('gulp'),
12
13
sass = require ( 'node-sass' ) ,
13
14
cleanCSS = require ( 'clean-css' ) ,
14
15
argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ,
16
+ svgParse = require ( 'parse-svg-path' ) ,
17
+ svgpath = require ( 'svgpath' ) ,
15
18
svgr = require ( '@svgr/core' ) . default ;
16
19
17
20
async function asyncForEach ( array , callback ) {
@@ -404,6 +407,17 @@ gulp.task('optimize', function (cb) {
404
407
return Math . round ( ( parseFloat ( n1 ) + parseFloat ( n2 ) ) * 1000 ) / 1000
405
408
} ;
406
409
410
+ const optimizePath = function ( path ) {
411
+ let transformed = svgpath ( path )
412
+ . rel ( )
413
+ . round ( 3 )
414
+ . toString ( ) ;
415
+
416
+ return svgParse ( transformed ) . map ( function ( a ) {
417
+ return a . join ( ' ' ) ;
418
+ } ) . join ( ' ' ) ;
419
+ } ;
420
+
407
421
glob ( "src/_icons/*.svg" , { } , function ( er , files ) {
408
422
409
423
files . forEach ( function ( file , i ) {
@@ -416,6 +430,11 @@ gulp.task('optimize', function (cb) {
416
430
. replace ( / \s ? \/ > / g, ' />' )
417
431
. replace ( / \n \s * < ( l i n e | c i r c l e | p a t h | p o l y l i n e | r e c t ) / g, "\n <$1" )
418
432
. replace ( / p o l y l i n e p o i n t s = " ( [ 0 - 9 . ] + ) \s ( [ 0 - 9 . ] + ) \s ( [ 0 - 9 . ] + ) \s ( [ 0 - 9 . ] + ) " / g, 'line x1="$1" y1="$2" x2="$3" y2="$4"' )
433
+ . replace ( / < p a t h d = " ( [ ^ " ] + ) " / g, function ( f , r1 ) {
434
+ r1 = optimizePath ( r1 ) ;
435
+
436
+ return `<path d="${ r1 } "` ;
437
+ } )
419
438
. replace ( / d = " m / g, 'd="M' )
420
439
. replace ( / ( [ A a ] ) \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' )
421
440
. replace ( / \n \n + / g, "\n" )
@@ -439,9 +458,6 @@ gulp.task('optimize', function (cb) {
439
458
} )
440
459
;
441
460
442
- //
443
- //
444
-
445
461
if ( svgFile . toString ( ) !== svgFileContent ) {
446
462
fs . writeFileSync ( file , svgFileContent ) ;
447
463
}
@@ -610,17 +626,17 @@ const setVersions = function(version, files) {
610
626
611
627
if ( fs . existsSync ( `src/_icons/${ file } .svg` ) ) {
612
628
let svgFile = fs . readFileSync ( `src/_icons/${ file } .svg` ) . toString ( ) ;
613
-
629
+
614
630
if ( ! svgFile . match ( / v e r s i o n : ( [ 0 - 9 . ] + ) / i) ) {
615
631
svgFile = svgFile . replace ( / - - - \n < s v g > / i, function ( m ) {
616
632
return `version: ${ version } \n${ m } ` ;
617
633
} ) ;
618
-
634
+
619
635
fs . writeFileSync ( `src/_icons/${ file } .svg` , svgFile ) ;
620
636
} else {
621
637
console . log ( `File ${ file } already has version` ) ;
622
638
}
623
-
639
+
624
640
} else {
625
641
console . log ( `File ${ file } doesn't exists` ) ;
626
642
}
@@ -636,7 +652,7 @@ gulp.task('update-icons-version', function (cb) {
636
652
cp . exec ( `git diff v${ version } HEAD --name-status` , function ( err , ret ) {
637
653
638
654
let newIcons = [ ] ;
639
-
655
+
640
656
ret . replace ( / [ A ] \s + s r c \/ _ i c o n s \/ ( [ a - z 0 - 9 - ] + ) \. s v g / g, function ( m , fileName ) {
641
657
newIcons . push ( fileName ) ;
642
658
} ) ;
@@ -650,6 +666,35 @@ gulp.task('update-icons-version', function (cb) {
650
666
cb ( ) ;
651
667
} ) ;
652
668
669
+ gulp . task ( 'import-tags' , function ( cb ) {
670
+ fs . createReadStream ( './_import.csv' )
671
+ . pipe ( csv ( {
672
+ headers : false ,
673
+ separator : "\t"
674
+ } ) )
675
+ . on ( 'data' , ( row ) => {
676
+ console . log ( row [ 0 ] , row [ 1 ] ) ;
677
+
678
+ const filename = `src/_icons/${ row [ 0 ] } .svg` ;
679
+
680
+ let data = fs . readFileSync ( filename ) . toString ( ) ;
681
+ data = data . replace ( / ( - - - [ \s \S ] + ?- - - ) / , function ( m , headerContent ) {
682
+
683
+ headerContent = headerContent . replace ( / t a g s : .* \n / , '' ) ;
684
+ headerContent = headerContent . replace ( / - - - / , `---\ntags: [${ row [ 1 ] } ]` ) ;
685
+
686
+ return headerContent ;
687
+ } ) ;
688
+
689
+ fs . writeFileSync ( filename , data ) ;
690
+
691
+ } )
692
+ . on ( 'end' , ( ) => {
693
+ console . log ( 'CSV file successfully processed' ) ;
694
+ } ) ;
695
+ cb ( ) ;
696
+ } ) ;
697
+
653
698
gulp . task ( "build-react" , function ( cb ) {
654
699
cp . exec ( "npm run build-react" , function ( ) {
655
700
cb ( ) ;
0 commit comments