Skip to content

Commit 18fb42a

Browse files
authored
fix: don't attempt to apply prettier if it is not available (#1246)
1 parent 70af26b commit 18fb42a

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

internal/format/templ.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Templ(src []byte, fileName string, config Config) (output []byte, changed b
3131
return nil, false, err
3232
}
3333

34-
if err = applyPrettier(t, src, config); err != nil {
34+
if err = applyPrettier(t, config); err != nil {
3535
return nil, false, err
3636
}
3737

@@ -44,13 +44,17 @@ func Templ(src []byte, fileName string, config Config) (output []byte, changed b
4444
return out, changed, nil
4545
}
4646

47-
func applyPrettier(t *parser.TemplateFile, src []byte, config Config) (err error) {
47+
func applyPrettier(t *parser.TemplateFile, config Config) (err error) {
4848
// Check to see if prettier can be run.
4949
if config.PrettierCommand == "" {
5050
config.PrettierCommand = prettier.DefaultCommand
5151
}
52-
if config.PrettierRequired && !prettier.IsAvailable(config.PrettierCommand) {
53-
return fmt.Errorf("prettier command is not available, please install it or set a different command using the -prettier-command flag")
52+
if !prettier.IsAvailable(config.PrettierCommand) {
53+
if config.PrettierRequired {
54+
return fmt.Errorf("prettier command %q is not available, please install it or set a different command using the -prettier-command flag", config.PrettierCommand)
55+
}
56+
// Prettier is not available, skip applying it.
57+
return nil
5458
}
5559

5660
nodeFormatter := visitor.New()

internal/prettier/prettier_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,20 @@ func Test(t *testing.T) {
3131
})
3232
}
3333
}
34+
35+
func TestIsAvailable(t *testing.T) {
36+
if testing.Short() {
37+
t.Skip("Skipping IsAvailable test in short mode")
38+
}
39+
t.Run("non-existent commands return false", func(t *testing.T) {
40+
var nonExistentCommand = "templ_non_existent_command --use-tabs --stdin-filepath $TEMPL_PRETTIER_FILENAME"
41+
if IsAvailable(nonExistentCommand) {
42+
t.Errorf("IsAvailable should return false for non-existent command %q", nonExistentCommand)
43+
}
44+
})
45+
t.Run("existing commands return true", func(t *testing.T) {
46+
if !IsAvailable(DefaultCommand) {
47+
t.Errorf("IsAvailable should return true for existing command %q", DefaultCommand)
48+
}
49+
})
50+
}

0 commit comments

Comments
 (0)