File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -116,23 +116,26 @@ notation) so that you can see them.
116116Follow these steps to automatically format all Javascript files on commit in
117117a project that uses npm.
118118
119- Install git-format-staged, husky, and a formatter (I use prettier-standard):
119+ Install git-format-staged, husky, and a formatter (I use ` prettier-standard ` ):
120120
121121 $ npm install --save-dev git-format-staged husky prettier-standard
122122
123- Add a ` "precommit" ` script in ` package.json ` :
123+ Add a ` prepare ` script to install husky when running ` npm install ` :
124124
125- "scripts": {
126- "precommit": "git-format-staged -f prettier-standard '*.js'"
127- }
125+ $ npm set-script prepare "husky install"
126+ $ npm run prepare
127+
128+ Add the pre-commit hook:
129+
130+ $ npx husky add .husky/pre-commit "git-format-staged --formatter 'prettier-standard --stdin-filepath \"{}\"' '*.js'"
131+ $ git add .husky/pre-commit
128132
129133Once again note that the ` '*.js' ` pattern is quoted! If the formatter command
130134included arguments it would also need to be quoted.
131135
132136That's it! Whenever a file is changed as a result of formatting on commit you
133137will see a message in the output from ` git commit ` .
134138
135-
136139## Comparisons to similar utilities
137140
138141There are other tools that will format or lint staged files. What distinguishes
You can’t perform that action at this time.
0 commit comments