From 3d478b3bf49345f0a365843b6507f0cb253b4c55 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Mon, 28 Aug 2023 21:04:15 +0100 Subject: [PATCH] feat(#128): templ fmt: don't overwrite the file if it hasn't changed --- cmd/templ/fmtcmd/main.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cmd/templ/fmtcmd/main.go b/cmd/templ/fmtcmd/main.go index ba4e0690a..2517eb1bb 100644 --- a/cmd/templ/fmtcmd/main.go +++ b/cmd/templ/fmtcmd/main.go @@ -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) } @@ -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)