avh4 / elm-format

elm-format formats Elm source code according to a standard set of rules based on the official Elm Style Guide
BSD 3-Clause "New" or "Revised" License
1.31k stars 148 forks source link

No error on formatting a directory with no Elm files #116

Open Janiczek opened 8 years ago

Janiczek commented 8 years ago

A bit of motivation: I have a git hook formatting every .elm file before commiting it. As long as there are .elm files it works. But when there's no .elm file, then I get this error:

Could not find any .elm files on the specified paths: /var/www/html/elm/projector Please check the given paths.

It's intended, the error message is correct, it's tested, etc. I'm just asking: should it be that way? Shouldn't elm-format just exit peacefully when there's nothing to do? (In the directory case. I'm not so sure about file/STDIN case.)


The hook: (.git/hooks/pre-commit)

#!/bin/sh
#
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.

# create a temporary file for the errors
TMP="$(mktemp)"

# find the absolute path to the repository
ROOT="$(git rev-parse --show-toplevel)"

# try to format the whole directory
elm-format --yes "$ROOT" >"$TMP" 2>/dev/null || {
  echo '[PRE-COMMIT HOOK] elm-format failed:'
  cat "$TMP"
  rm -f "$TMP"
  exit 1
}

# delete the temporary errors file
rm -f "$TMP"
avh4 commented 8 years ago

I think giving an error is the right thing. If a developer or a script is running elm-format then I think it's safe to assume that they expect at least one elm file to be formatted.

Janiczek commented 8 years ago

Well, the git pre-commit hook is one usecase where you'd want to "format all elm files that you can find" - even if that's zero of them.

Of course, I can change the hook to see if any elm files are there and exit early if there are none. But still, it makes sense to me for elm-format to exit peacefully when there's nothing to do and the command is not invalid.

avh4 commented 8 years ago

This is a pre-commit hook that you copy into all your new projects by default?

Janiczek commented 8 years ago

This is the first time I've used it. But it seems very convenient for Elm projects :)

renatorozas commented 8 years ago

Nice @Janiczek, I'll borrow your script to use it in my toy project if you don't mind.

I agree giving an error sounds like a good idea.