Skip to content

Commit 01cd981

Browse files
committed
cmd: fix usage of --match and --sync simultaneously; --match applies to all filename inputs not just under directories
1 parent cd66364 commit 01cd981

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

cmd/minify/main.go

+55-49
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ var (
6060
type Task struct {
6161
srcs []string
6262
dst string
63+
sync bool
6364
}
6465

6566
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+
}
6771
if 0 < len(output) && output[len(output)-1] == '/' {
6872
rel, err := filepath.Rel(root, input)
6973
if err != nil {
@@ -214,6 +218,15 @@ func run() int {
214218
}
215219
}
216220

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+
217230
if (useStdin || output == "") && (watch || sync || recursive) {
218231
if watch {
219232
Error.Println("--watch doesn't work on stdin and stdout, specify input and output")
@@ -226,18 +239,12 @@ func run() int {
226239
}
227240
return 1
228241
}
229-
if mimetype == "" && filetype == "" && useStdin {
242+
if mimetype == "" && useStdin {
230243
Error.Println("must specify --mime or --type for stdin")
231244
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
241248
}
242249
if verbose {
243250
if mimetype == "" {
@@ -305,6 +312,7 @@ func run() int {
305312

306313
// concatenate
307314
if 1 < len(tasks) && !dirDst {
315+
// Task.sync == false because dirDst == false
308316
for _, task := range tasks[1:] {
309317
tasks[0].srcs = append(tasks[0].srcs, task.srcs[0])
310318
}
@@ -451,30 +459,28 @@ func sanitizePath(p string) string {
451459
}
452460

453461
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] != '.')
472463
}
473464

474465
func validDir(info os.FileInfo) bool {
475466
return info.Mode().IsDir() && len(info.Name()) > 0 && (hidden || info.Name()[0] != '.')
476467
}
477468

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+
478484
func createTasks(inputs []string, output string) ([]Task, []string, error) {
479485
tasks := []Task{}
480486
roots := []string{}
@@ -486,11 +492,13 @@ func createTasks(inputs []string, output string) ([]Task, []string, error) {
486492
}
487493

488494
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)
492501
}
493-
tasks = append(tasks, task)
494502
} else if info.Mode().IsDir() {
495503
roots = append(roots, input)
496504
if !recursive {
@@ -499,7 +507,7 @@ func createTasks(inputs []string, output string) ([]Task, []string, error) {
499507
return nil, nil, err
500508
}
501509
for _, info := range infos {
502-
if validFile(info) {
510+
if validFile(info) && (sync || fileMatches(info.Name())) {
503511
task, err := NewTask(input, path.Join(input, info.Name()), output)
504512
if err != nil {
505513
return nil, nil, err
@@ -513,7 +521,7 @@ func createTasks(inputs []string, output string) ([]Task, []string, error) {
513521
return err
514522
}
515523
path = sanitizePath(path)
516-
if validFile(info) {
524+
if validFile(info) && (sync || fileMatches(info.Name())) {
517525
task, err := NewTask(input, path, output)
518526
if err != nil {
519527
return err
@@ -573,13 +581,11 @@ func openOutputFile(output string) (*os.File, error) {
573581
}
574582

575583
func minify(mimetype string, t Task) bool {
576-
if mimetype == "" {
584+
if mimetype == "" && !t.sync {
577585
for _, src := range t.srcs {
578586
if len(path.Ext(src)) > 0 {
579587
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 {
583589
Error.Println("cannot infer mimetype from extension in", src)
584590
return false
585591
}
@@ -594,7 +600,7 @@ func minify(mimetype string, t Task) bool {
594600
}
595601

596602
// 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 {
598604
return true
599605
}
600606

@@ -634,23 +640,15 @@ func minify(mimetype string, t Task) bool {
634640
if mimetype == filetypeMime["js"] {
635641
fr.SetSeparator([]byte("\n"))
636642
}
637-
r := NewCountingReader(fr)
638-
639643
fw, err := openOutputFile(t.dst)
640644
if err != nil {
641645
Error.Println(err)
642646
fr.Close()
643647
return false
644648
}
645-
var w *countingWriter
646-
if fw == os.Stdout {
647-
w = NewCountingWriter(fw)
648-
} else {
649-
w = NewCountingWriter(bufio.NewWriter(fw))
650-
}
651649

652650
// synchronize file
653-
if sync && mimetype == "" {
651+
if t.sync {
654652
_, err = io.Copy(fw, fr)
655653
fr.Close()
656654
fw.Close()
@@ -662,6 +660,14 @@ func minify(mimetype string, t Task) bool {
662660
return true
663661
}
664662

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+
665671
success := true
666672
startTime := time.Now()
667673
if err = m.Minify(mimetype, w, r); err != nil {

0 commit comments

Comments
 (0)