Trailing spaces and Missing Newlines (OSX)

I frequently work on projects where various developers are using various editors or IDEs. Some of these tools, and it would be rude to name names, have completely broken notions of how files and lines ought to end. Those of us who been working with text files for many years mostly agree on the following.

  1. Every line, including the last one, ends in a newline character.
  2. No trailing white space.

Happily, git will point out problems of this nature, and many editors can easily fix a file once open. But I often need to fix a whole tree of files at once. I have various snippets and batch files around to do this, here is the relevant one-liner for OSX:

find . \( -name '*.js' -or -name '*.ts' -or -name '*.html' \) -exec sed -E -i '' -e's/[[:space:]]*$//' -e '$a\' {} \;

As usual, I recommend understanding such a line before running it, editing it to meet your specific needs, and using backups and/or source control to avoid data loss.