Skip to content

Commit 0c2b7cf

Browse files
committedMar 22, 2025··
refactor: result of discussion in #1101
1 parent f0a1f0c commit 0c2b7cf

File tree

8 files changed

+98
-45
lines changed

8 files changed

+98
-45
lines changed
 

‎.version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.847
1+
0.3.850

‎cmd/templ/fmtcmd/main.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fmtcmd
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"io"
78
"log/slog"
@@ -66,6 +67,7 @@ func NewFormatter(log *slog.Logger, dir string, process func(fileName string) (e
6667
}
6768

6869
func (f *Formatter) Run() (err error) {
70+
var errs []error
6971
changesMade := 0
7072
start := time.Now()
7173
results := make(chan processor.Result)
@@ -79,6 +81,7 @@ func (f *Formatter) Run() (err error) {
7981
if r.Error != nil {
8082
f.Log.Error(r.FileName, slog.Any("error", r.Error))
8183
errorCount++
84+
errs = append(errs, r.Error)
8285
continue
8386
}
8487
f.Log.Debug(r.FileName, slog.Duration("duration", r.Duration))
@@ -92,11 +95,11 @@ func (f *Formatter) Run() (err error) {
9295

9396
f.Log.Info("Format Complete", slog.Int("count", successCount+errorCount), slog.Int("errors", errorCount), slog.Int("changed", changesMade), slog.Duration("duration", time.Since(start)))
9497

95-
if errorCount > 0 {
96-
return fmt.Errorf("formatting failed")
98+
if err = errors.Join(errs...); err != nil {
99+
return fmt.Errorf("formatting failed: %w", err)
97100
}
98101

99-
return
102+
return nil
100103
}
101104

102105
type reader func() (fileName, src string, err error)

‎cmd/templ/generatecmd/watcher/watch.go

+3-19
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"context"
55
"io/fs"
66
"os"
7-
"path"
87
"path/filepath"
98
"regexp"
10-
"strings"
119
"sync"
1210
"time"
1311

12+
"github.com/a-h/templ/internal/skipdir"
1413
"github.com/fsnotify/fsnotify"
1514
)
1615

@@ -54,7 +53,7 @@ func WalkFiles(ctx context.Context, path string, watchPattern *regexp.Regexp, ou
5453
if err != nil {
5554
return nil
5655
}
57-
if info.IsDir() && shouldSkipDir(absPath) {
56+
if info.IsDir() && skipdir.ShouldSkip(absPath) {
5857
return filepath.SkipDir
5958
}
6059
if !watchPattern.MatchString(absPath) {
@@ -143,24 +142,9 @@ func (w *RecursiveWatcher) Add(dir string) error {
143142
if !info.IsDir() {
144143
return nil
145144
}
146-
if shouldSkipDir(dir) {
145+
if skipdir.ShouldSkip(dir) {
147146
return filepath.SkipDir
148147
}
149148
return w.w.Add(dir)
150149
})
151150
}
152-
153-
func shouldSkipDir(dir string) bool {
154-
if dir == "." {
155-
return false
156-
}
157-
if dir == "vendor" || dir == "node_modules" {
158-
return true
159-
}
160-
_, name := path.Split(dir)
161-
// These directories are ignored by the Go tool.
162-
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
163-
return true
164-
}
165-
return false
166-
}

‎cmd/templ/processor/processor.go

+3-17
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package processor
22

33
import (
44
"io/fs"
5-
"path"
65
"path/filepath"
76
"strings"
87
"sync"
98
"time"
9+
10+
"github.com/a-h/templ/internal/skipdir"
1011
)
1112

1213
type Result struct {
@@ -27,27 +28,12 @@ func Process(dir string, f func(fileName string) (error, bool), workerCount int,
2728
ProcessChannel(templates, dir, f, workerCount, results)
2829
}
2930

30-
func shouldSkipDir(dir string) bool {
31-
if dir == "." {
32-
return false
33-
}
34-
if dir == "vendor" || dir == "node_modules" {
35-
return true
36-
}
37-
_, name := path.Split(dir)
38-
// These directories are ignored by the Go tool.
39-
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
40-
return true
41-
}
42-
return false
43-
}
44-
4531
func FindTemplates(srcPath string, output chan<- string) (err error) {
4632
return filepath.WalkDir(srcPath, func(currentPath string, info fs.DirEntry, err error) error {
4733
if err != nil {
4834
return err
4935
}
50-
if info.IsDir() && shouldSkipDir(currentPath) {
36+
if info.IsDir() && skipdir.ShouldSkip(currentPath) {
5137
return filepath.SkipDir
5238
}
5339
if !info.IsDir() && strings.HasSuffix(currentPath, ".templ") {

‎examples/counter/go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/a-h/templ/examples/counter
22

3-
go 1.23
3+
go 1.23.0
44

5-
toolchain go1.23.3
5+
toolchain go1.23.6
66

77
require (
88
github.com/a-h/templ v0.2.234-0.20230427112944-80f0dc03a8a8
@@ -46,7 +46,7 @@ require (
4646
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
4747
golang.org/x/mod v0.20.0 // indirect
4848
golang.org/x/sync v0.10.0 // indirect
49-
golang.org/x/sys v0.28.0 // indirect
49+
golang.org/x/sys v0.31.0 // indirect
5050
golang.org/x/tools v0.24.0 // indirect
5151
)
5252

‎examples/counter/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
9595
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9696
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9797
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
98-
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
99-
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
98+
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
99+
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
100100
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
101101
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
102102
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=

‎internal/skipdir/skipdir.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package skipdir
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
)
7+
8+
func ShouldSkip(path string) (skip bool) {
9+
if path == "." {
10+
return false
11+
}
12+
_, name := filepath.Split(path)
13+
if name == "vendor" || name == "node_modules" {
14+
return true
15+
}
16+
if strings.HasPrefix(name, ".") || strings.HasPrefix(name, "_") {
17+
return true
18+
}
19+
return false
20+
}

‎internal/skipdir/skipdir_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package skipdir
2+
3+
import "testing"
4+
5+
func TestSkipDir(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
dir string
9+
expected bool
10+
}{
11+
{
12+
name: "current directory is not skipped",
13+
dir: ".",
14+
expected: false,
15+
},
16+
{
17+
name: "standard paths are not skipped",
18+
dir: "/home/user/adrian/github.com/a-h/templ/examples",
19+
expected: false,
20+
},
21+
{
22+
name: "vendor directories are skipped",
23+
dir: "/home/user/adrian/github.com/a-h/templ/examples/vendor",
24+
expected: true,
25+
},
26+
{
27+
name: "node_modules directories are skipped",
28+
dir: "/home/user/adrian/github.com/a-h/templ/examples/node_modules",
29+
expected: true,
30+
},
31+
{
32+
name: "dot directories are skipped",
33+
dir: "/home/user/adrian/github.com/a-h/templ/examples/.git",
34+
expected: true,
35+
},
36+
{
37+
name: "underscore directories are skipped",
38+
dir: "/home/user/adrian/github.com/a-h/templ/examples/_build",
39+
expected: true,
40+
},
41+
{
42+
name: "relative paths are normalised",
43+
dir: "examples",
44+
expected: false,
45+
},
46+
{
47+
name: "relative paths are normalised",
48+
dir: "examples/vendor",
49+
expected: true,
50+
},
51+
}
52+
for _, test := range tests {
53+
t.Run(test.name, func(t *testing.T) {
54+
actual := ShouldSkip(test.dir)
55+
if test.expected != actual {
56+
t.Errorf("expected %v, got %v", test.expected, actual)
57+
}
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)
Please sign in to comment.