Fix Only Committed Files with Prettier and lint-staged

Jason Brown
InstructorJason Brown
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 years ago

In this lesson we'll use prettier and lint-staged to run prettier only on files that have been changed and committed to git. This will allow you to prettify files as you change them, and prevent massive lint only git check ins.

[00:00] Prettier is an opinionated formatting tool that allows you to format JavaScript of any kind as well as CSS. It is rivaled because it has many sane defaults and not many configurations. To get started, we'll install Prettier, lint-staged, and Husky to our devDependencies.

[00:22] Once complete, we'll go to our package.json, and we'll set up a script. The script will be the pre-commit hook, and we'll run lint-staged. This pre-commit hook is configured by Husky, and is configured inside of the Git script so that when a commit happens, this command will be run.

[00:45] Now, we'll set up our lint-staged configuration. We'll add lint-staged here, which will receive an object. Then it will ask for what types of files that we want to format. We'll say *.js, which will then also receive an array.

[01:02] This array will be the commands that we want to run. There'll be two. We'll want to run prettier--right. Then the final command will be git add, which will then add the files after Prettier has run. With this configuration, we'll only run Prettier on the files that have changed.

[01:22] This means that we will gradually be able to prettify our code base, rather than having a huge Git check-in of a bunch of prettified code. To prove that it is only working on our stage files that we're committing, we have created two files.

[01:37] One, index.js, with a bunch of white space here, as well as a separate file with a bunch of white space. If we go into our Git with VS code, we can see that there's two files that have changed. We will simply stage this index.js, add a commit message, and then when we commit it.

[01:59] If we go back, we can see that the white space is now gone, however this other file that we did not commit still has its white space.

egghead
egghead
~ 14 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today