Skip to content

Commit da67d20

Browse files
committed
cmd: directory input needs --recursive and file inputs are copied without considering and recreating its directory path (matchin cp command behavious)
1 parent af90966 commit da67d20

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

cmd/minify/main.go

+25-35
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ func NewTask(root, input, output string) (Task, error) {
7878
}
7979

8080
var (
81-
Error *log.Logger
82-
Info *log.Logger
81+
Error *log.Logger
82+
Warning *log.Logger
83+
Info *log.Logger
8384
)
8485

8586
func main() {
@@ -150,6 +151,7 @@ func run() int {
150151
useStdin := len(inputs) == 0
151152

152153
Error = log.New(os.Stderr, "ERROR: ", 0)
154+
Warning = log.New(os.Stderr, "WARNING: ", 0)
153155
if verbose {
154156
Info = log.New(os.Stderr, "", 0)
155157
} else {
@@ -320,7 +322,7 @@ func run() int {
320322
}
321323

322324
// concatenate
323-
if 1 < len(tasks) && !dirDst {
325+
if 1 < len(tasks) && bundle {
324326
// Task.sync == false because dirDst == false
325327
for _, task := range tasks[1:] {
326328
tasks[0].srcs = append(tasks[0].srcs, task.srcs[0])
@@ -408,9 +410,9 @@ func run() int {
408410
}
409411
}
410412

411-
// skip files in output directory (which is also an input directory) for the first change
412-
// skips files that are not minified and stay put, as they are not explicitly copied, but that's ok
413413
if autoDir && root == output {
414+
// skip files in output directory (which is also an input directory) for the first change
415+
// skips files that are not minified and stay put as they are not explicitly copied, but that's ok
414416
if _, ok := skip[file]; !ok {
415417
skip[file] = true
416418
break
@@ -437,7 +439,7 @@ func run() int {
437439
}
438440

439441
if verbose && !watch {
440-
Info.Println(time.Since(start), "total")
442+
Info.Println("finished in", time.Since(start))
441443
}
442444
if 0 < fails {
443445
return 1
@@ -502,48 +504,36 @@ func createTasks(inputs []string, output string) ([]Task, []string, error) {
502504

503505
if info.Mode().IsRegular() {
504506
if sync || fileMatches(info.Name()) {
505-
task, err := NewTask("", input, output)
507+
task, err := NewTask(filepath.Dir(input), input, output)
506508
if err != nil {
507509
return nil, nil, err
508510
}
509511
tasks = append(tasks, task)
510512
}
511513
} else if info.Mode().IsDir() {
512-
roots = append(roots, input)
513514
if !recursive {
514-
infos, err := ioutil.ReadDir(input)
515+
Warning.Println("--recursive not specified, omitting directory", input)
516+
continue
517+
}
518+
roots = append(roots, input)
519+
err := filepath.Walk(input, func(path string, info os.FileInfo, err error) error {
515520
if err != nil {
516-
return nil, nil, err
517-
}
518-
for _, info := range infos {
519-
if validFile(info) && (sync || fileMatches(info.Name())) {
520-
task, err := NewTask(input, path.Join(input, info.Name()), output)
521-
if err != nil {
522-
return nil, nil, err
523-
}
524-
tasks = append(tasks, task)
525-
}
521+
return err
526522
}
527-
} else {
528-
err := filepath.Walk(input, func(path string, info os.FileInfo, err error) error {
523+
path = sanitizePath(path)
524+
if validFile(info) && (sync || fileMatches(info.Name())) {
525+
task, err := NewTask(input, path, output)
529526
if err != nil {
530527
return err
531528
}
532-
path = sanitizePath(path)
533-
if validFile(info) && (sync || fileMatches(info.Name())) {
534-
task, err := NewTask(input, path, output)
535-
if err != nil {
536-
return err
537-
}
538-
tasks = append(tasks, task)
539-
} else if info.Mode().IsDir() && !validDir(info) && info.Name() != "." && info.Name() != ".." { // check for IsDir, so we don't skip the rest of the directory when we have an invalid file
540-
return filepath.SkipDir
541-
}
542-
return nil
543-
})
544-
if err != nil {
545-
return nil, nil, err
529+
tasks = append(tasks, task)
530+
} else if info.Mode().IsDir() && !validDir(info) && info.Name() != "." && info.Name() != ".." { // check for IsDir, so we don't skip the rest of the directory when we have an invalid file
531+
return filepath.SkipDir
546532
}
533+
return nil
534+
})
535+
if err != nil {
536+
return nil, nil, err
547537
}
548538
} else {
549539
return nil, nil, fmt.Errorf("not a file or directory %s", input)

0 commit comments

Comments
 (0)