Automatically format a project on commit using be-pretty

Simon Visser
2 min readDec 9, 2018

--

image credit: ZURB at https://medium.com/@ZURB/beautiful-code-by-design-5cc675c3cef6

If you’ve ever worked on a project without lint rules set up, then subsequently found yourself reviewing a pull request, well no doubt you’re aware of the struggle of having to comb through each line of changes, trying to separate the formatting noise from actual changes to code.

In fact, when a pull request doesn’t concisely show what has changed, and perhaps the branch you’re working on feeds into your CI/CD pipeline, the extra noise around the Pull Request doesn’t just mean a little bit more effort to find what you’re looking for; that extra noise can in fact be really dangerous.

Obviously the solution is to set up a linter. But you don’t want to burden your development team with things like “installing software” and “running a linter manually.” So you decide to make the linter run before each commit. How clever of you! Luckily, this can be done with husky.

But the task of figuring out a shell script that somehow interacts with git and only runs your formatter against any staged files sounds like a chore. So you decide to use pretty-quick to run prettier against only your staged files. Good choice.

And you get this far, you have your pre-commit formatter set up and realise, damn, I need to do this for multiple projects, kill me now. Enter be-pretty.

Be-pretty distills our entire flow for prettifying a project to one line.

npm install -g be-pretty

Followed by, in your project,

be-pretty

Be-pretty will add a husky commit hook, running pretty-quick on staged files, as well as use your custom .prettierrc file that you can specify before the time with

be-pretty setDefault -p="~/code/.prettierrc"

Be-pretty will also run pretty-quick against all files in the project, staged or not. If you ever find yourself wanting to format all projects in your file again, go ahead and run

be-pretty formatAll

And there you have it. 5 Minutes to properly format all of your code forever.

--

--