A CLI tool to setup Prettier and pre-commit hooks. Heavily inspired by Create React App.
Feedback is welcome. 🙏
Execute in the root directory of your project:
npx prettier-please
This will install the dependencies Prettier, Husky and lint-staged inside your project. It will further setup a pre-commit hook to automatically format your files before committing them. No more discussions on code style during code reviews. 😉
First uninstall the dependencies:
npm uninstall prettier husky lint-staged
Then remove the properties husky
and lint-staged
from your project's package.json
:
{
- "husky": {
- "hooks": {
- "pre-commit": "lint-staged"
- }
- },
- "lint-staged": {
- "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
- "prettier --write",
- "git add"
- ]
- }
}
Hint: You can always return to beautifully formatted code by executing
npx prettier-please
again. 🤫
prettier-please makes some assumptions about where your source code is located and which files to format (namely "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}"
). To fix this, please adapt the path defined in the lint-staged
property inside your project's package.json
to your needs.
Example:
{
"lint-staged": {
- "src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
+ "code/**/*.{php,html,yml}": [
"prettier --write",
"git add"
]
}
}
Good question! While using Prettier inside your editor might be sufficient when developing on your own, it probably won't be when working in a team. Other developers on your team might not have configured their editors to format code automatically. Using Prettier in combination with pre-commit hooks ensures that your code is always formatted correctly.
Nope! Prettier can be configured to work with many languages other than JavaScript (e.g. PHP). For more info, visit the official Prettier documentation.
You'll need a
package.json
file inside your project. This can be generated by executingnpm init
.