* chore(deps): upgrade prettier to v1.8.2 * chore: setup lint-docs script * docs: add `<!-- prettier-ignore -->`s * docs: format docs * Revert "docs: format docs" This reverts commit 6dba903987dddc498cb589ca311905f25b6aee53. * chore(prettierrc): set `--no-prose-wrap` * docs: format docs * Revert "docs: format docs" This reverts commit 139bd744b6921916be8fac4fa3bab83bae5d12df. * docs: tweak * docs: format docs * chore: rename .prettierrc * chore: rename back to .prettierrc * chore: split out third-party * refactor: fix linting * chore: do not split out third-party on website
1.6 KiB
1.6 KiB
id, title
| id | title |
|---|---|
| precommit | Pre-commit Hook |
You can use Prettier with a pre-commit tool. This can re-format your files that are marked as "staged" via git add before you commit.
Option 1. lint-staged
Install it along with husky:
yarn add lint-staged husky --dev
and add this config to your package.json:
{
"scripts": {
"precommit": "lint-staged"
},
"lint-staged": {
"*.{js,json,css}": ["prettier --write", "git add"]
}
}
There is a limitation where if you stage specific lines this approach will stage the whole file after regardless. See this issue for more info.
See https://github.com/okonet/lint-staged#configuration for more details about how you can configure lint-staged.
Option 2. pre-commit
Copy the following config into your .pre-commit-config.yaml file:
- repo: https://github.com/prettier/prettier
sha: '' # Use the sha or tag you want to point at
hooks:
- id: prettier
Find more info from here.
Option 3. bash script
Alternately you can save this script as .git/hooks/pre-commit and give it execute permission:
#!/bin/sh
jsfiles=$(git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" | tr '\n' ' ')
[ -z "$jsfiles" ] && exit 0
# Prettify all staged .js files
echo "$jsfiles" | xargs ./node_modules/.bin/prettier --write
# Add back the modified/prettified files to staging
echo "$jsfiles" | xargs git add
exit 0