@@ -60,10 +60,14 @@ var (
60
60
type Task struct {
61
61
srcs []string
62
62
dst string
63
+ sync bool
63
64
}
64
65
65
66
func NewTask (root , input , output string ) (Task , error ) {
66
- t := Task {[]string {input }, output }
67
+ t := Task {[]string {input }, output , false }
68
+ if input != "" {
69
+ t .sync = ! fileMatches (input )
70
+ }
67
71
if 0 < len (output ) && output [len (output )- 1 ] == '/' {
68
72
rel , err := filepath .Rel (root , input )
69
73
if err != nil {
@@ -214,6 +218,15 @@ func run() int {
214
218
}
215
219
}
216
220
221
+ // detect mimetype, mimetype=="" means we'll infer mimetype from file extensions
222
+ if mimetype == "" && filetype != "" {
223
+ var ok bool
224
+ if mimetype , ok = filetypeMime [filetype ]; ! ok {
225
+ Error .Println ("cannot find mimetype for filetype" , filetype )
226
+ return 1
227
+ }
228
+ }
229
+
217
230
if (useStdin || output == "" ) && (watch || sync || recursive ) {
218
231
if watch {
219
232
Error .Println ("--watch doesn't work on stdin and stdout, specify input and output" )
@@ -226,18 +239,12 @@ func run() int {
226
239
}
227
240
return 1
228
241
}
229
- if mimetype == "" && filetype == "" && useStdin {
242
+ if mimetype == "" && useStdin {
230
243
Error .Println ("must specify --mime or --type for stdin" )
231
244
return 1
232
- }
233
-
234
- // detect mimetype, mimetype=="" means we'll infer mimetype from file extensions
235
- if mimetype == "" && filetype != "" {
236
- var ok bool
237
- if mimetype , ok = filetypeMime [filetype ]; ! ok {
238
- Error .Println ("cannot find mimetype for filetype" , filetype )
239
- return 1
240
- }
245
+ } else if mimetype != "" && sync {
246
+ Error .Println ("must specify either --sync or --mime/--type" )
247
+ return 1
241
248
}
242
249
if verbose {
243
250
if mimetype == "" {
@@ -305,6 +312,7 @@ func run() int {
305
312
306
313
// concatenate
307
314
if 1 < len (tasks ) && ! dirDst {
315
+ // Task.sync == false because dirDst == false
308
316
for _ , task := range tasks [1 :] {
309
317
tasks [0 ].srcs = append (tasks [0 ].srcs , task .srcs [0 ])
310
318
}
@@ -451,30 +459,28 @@ func sanitizePath(p string) string {
451
459
}
452
460
453
461
func validFile (info os.FileInfo ) bool {
454
- if info .Mode ().IsRegular () && len (info .Name ()) > 0 && (hidden || info .Name ()[0 ] != '.' ) {
455
- if pattern != nil && ! pattern .MatchString (info .Name ()) {
456
- return false
457
- }
458
-
459
- if ! sync {
460
- ext := path .Ext (info .Name ())
461
- if len (ext ) > 0 {
462
- ext = ext [1 :]
463
- }
464
-
465
- if _ , ok := filetypeMime [ext ]; ! ok {
466
- return false
467
- }
468
- }
469
- return true
470
- }
471
- return false
462
+ return info .Mode ().IsRegular () && len (info .Name ()) > 0 && (hidden || info .Name ()[0 ] != '.' )
472
463
}
473
464
474
465
func validDir (info os.FileInfo ) bool {
475
466
return info .Mode ().IsDir () && len (info .Name ()) > 0 && (hidden || info .Name ()[0 ] != '.' )
476
467
}
477
468
469
+ func fileMatches (filename string ) bool {
470
+ if pattern != nil && ! pattern .MatchString (filename ) {
471
+ return false
472
+ }
473
+
474
+ ext := path .Ext (filename )
475
+ if len (ext ) > 0 {
476
+ ext = ext [1 :]
477
+ }
478
+ if _ , ok := filetypeMime [ext ]; ! ok {
479
+ return false
480
+ }
481
+ return true
482
+ }
483
+
478
484
func createTasks (inputs []string , output string ) ([]Task , []string , error ) {
479
485
tasks := []Task {}
480
486
roots := []string {}
@@ -486,11 +492,13 @@ func createTasks(inputs []string, output string) ([]Task, []string, error) {
486
492
}
487
493
488
494
if info .Mode ().IsRegular () {
489
- task , err := NewTask ("" , input , output )
490
- if err != nil {
491
- return nil , nil , err
495
+ if sync || fileMatches (info .Name ()) {
496
+ task , err := NewTask ("" , input , output )
497
+ if err != nil {
498
+ return nil , nil , err
499
+ }
500
+ tasks = append (tasks , task )
492
501
}
493
- tasks = append (tasks , task )
494
502
} else if info .Mode ().IsDir () {
495
503
roots = append (roots , input )
496
504
if ! recursive {
@@ -499,7 +507,7 @@ func createTasks(inputs []string, output string) ([]Task, []string, error) {
499
507
return nil , nil , err
500
508
}
501
509
for _ , info := range infos {
502
- if validFile (info ) {
510
+ if validFile (info ) && ( sync || fileMatches ( info . Name ())) {
503
511
task , err := NewTask (input , path .Join (input , info .Name ()), output )
504
512
if err != nil {
505
513
return nil , nil , err
@@ -513,7 +521,7 @@ func createTasks(inputs []string, output string) ([]Task, []string, error) {
513
521
return err
514
522
}
515
523
path = sanitizePath (path )
516
- if validFile (info ) {
524
+ if validFile (info ) && ( sync || fileMatches ( info . Name ())) {
517
525
task , err := NewTask (input , path , output )
518
526
if err != nil {
519
527
return err
@@ -573,13 +581,11 @@ func openOutputFile(output string) (*os.File, error) {
573
581
}
574
582
575
583
func minify (mimetype string , t Task ) bool {
576
- if mimetype == "" {
584
+ if mimetype == "" && ! t . sync {
577
585
for _ , src := range t .srcs {
578
586
if len (path .Ext (src )) > 0 {
579
587
srcMimetype , ok := filetypeMime [path .Ext (src )[1 :]]
580
- if ! ok && sync {
581
- break // is sync==true, then len(t.srcs)==1
582
- } else if ! ok {
588
+ if ! ok {
583
589
Error .Println ("cannot infer mimetype from extension in" , src )
584
590
return false
585
591
}
@@ -594,7 +600,7 @@ func minify(mimetype string, t Task) bool {
594
600
}
595
601
596
602
// synchronizing files that are not minified but just copied to the same directory, no action needed
597
- if sync && mimetype == "" && t .srcs [0 ] == t .dst {
603
+ if t . sync && t .srcs [0 ] == t .dst {
598
604
return true
599
605
}
600
606
@@ -634,23 +640,15 @@ func minify(mimetype string, t Task) bool {
634
640
if mimetype == filetypeMime ["js" ] {
635
641
fr .SetSeparator ([]byte ("\n " ))
636
642
}
637
- r := NewCountingReader (fr )
638
-
639
643
fw , err := openOutputFile (t .dst )
640
644
if err != nil {
641
645
Error .Println (err )
642
646
fr .Close ()
643
647
return false
644
648
}
645
- var w * countingWriter
646
- if fw == os .Stdout {
647
- w = NewCountingWriter (fw )
648
- } else {
649
- w = NewCountingWriter (bufio .NewWriter (fw ))
650
- }
651
649
652
650
// synchronize file
653
- if sync && mimetype == "" {
651
+ if t . sync {
654
652
_ , err = io .Copy (fw , fr )
655
653
fr .Close ()
656
654
fw .Close ()
@@ -662,6 +660,14 @@ func minify(mimetype string, t Task) bool {
662
660
return true
663
661
}
664
662
663
+ r := NewCountingReader (fr )
664
+ var w * countingWriter
665
+ if fw == os .Stdout {
666
+ w = NewCountingWriter (fw )
667
+ } else {
668
+ w = NewCountingWriter (bufio .NewWriter (fw ))
669
+ }
670
+
665
671
success := true
666
672
startTime := time .Now ()
667
673
if err = m .Minify (mimetype , w , r ); err != nil {
0 commit comments