14

I am using prettier-standard because the project uses the standard for linting.

Following the prettier pre-commit hook example I am running prettier on commits. However I would like to ignore the package.json file. I tried adding package.json to a .prettierignore file but this did not work.

Code from the prettier pre-commit hook example that I am using in my package.json

{
  "scripts": {
    "precommit": "lint-staged"
  },
  "lint-staged": {
     "*.{js,json,css}": [
       "prettier --write",
       "git add"
     ]
  }
}

```

Mdd
  • 4,140
  • 12
  • 45
  • 70
  • 1
    I just removed json from the matching e.g. `"*.{js,css}"`. Since it uses minimatch, you might be able to include a negative match to exclude `package.json` but still include other json files. – row1 Oct 02 '17 at 21:25

2 Answers2

12

you can also use a .prettierignore file.

See the prettier project itself for a reference.

xiphe
  • 496
  • 6
  • 10
  • I missed that part in your question, sorry. Maybe the tool you use in your precommit hook does not take that file into account. – xiphe Nov 21 '17 at 19:39
7

The limitation here is due to how lint-staged is used. I personally end up using a simple command (fast enough for me), without lint-staged (but still using husky+precommit).

prettier --write "**/*.{js,json,css,md}" !package.json

This command is in my package.json as a "format" script.

"precommit": "yarn format", // can be "npm run format"
"format": "prettier --write \"**/*.{js,json,css,md}\" \"!package.json\""

Please note the escaped quotes.

MoOx
  • 8,423
  • 5
  • 40
  • 39
  • 3
    You can use single quote instead of the backslash, which is tidy. `"format": "prettier --write '**/*.{ts,md,json}' '!package.json'"` – Mist Sep 05 '19 at 11:05