Skip to content

Commit

Permalink
feat(#128): templ fmt: don't overwrite the file if it hasn't changed
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Aug 28, 2023
1 parent 109dec9 commit 9b00e0c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmd/templ/fmtcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ func formatDir(dir string) (err error) {
}

func format(fileName string) (err error) {
t, err := parser.Parse(fileName)
contents, err := os.ReadFile(fileName)
if err != nil {
return fmt.Errorf("failed to read file %q: %w", fileName, err)
}
t, err := parser.ParseString(string(contents))
if err != nil {
return fmt.Errorf("%s parsing error: %w", fileName, err)
}
Expand All @@ -67,6 +71,9 @@ func format(fileName string) (err error) {
if err != nil {
return fmt.Errorf("%s formatting error: %w", fileName, err)
}
if string(contents) == w.String() {
return nil
}
err = atomic.WriteFile(fileName, w)
if err != nil {
return fmt.Errorf("%s file write error: %w", fileName, err)
Expand Down

0 comments on commit 9b00e0c

Please sign in to comment.