-
Notifications
You must be signed in to change notification settings - Fork 331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Git hook to lint JavaScript and Sass file pre-commit
#2848
Conversation
6a785ae
to
d1450be
Compare
d1450be
to
3cd5374
Compare
3cd5374
to
abf4da9
Compare
.(m)js
and .scss
file pre-commit
pre-commit
abf4da9
to
f8a5ad9
Compare
f8a5ad9
to
15ab4aa
Compare
15ab4aa
to
08e6e06
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved with two little comments
One's a blocker but I'm sure you'll fix it before merging 😆
Cheers @romaricpascal
08e6e06
to
5628946
Compare
5628946
to
8122b84
Compare
8122b84
to
d31b856
Compare
d31b856
to
66cbdd4
Compare
66cbdd4
to
33d1d13
Compare
33d1d13
to
db7b849
Compare
db7b849
to
f4130a1
Compare
f4130a1
to
c0daf11
Compare
c0daf11
to
86aec4f
Compare
86aec4f
to
af80535
Compare
af80535
to
ba01629
Compare
ba01629
to
d75db6b
Compare
d75db6b
to
e36a704
Compare
e36a704
to
801aaef
Compare
Install the hooks in the prepare script through a node call rather than just `husky install` as the package won't be installed on Heroku, which would make the `prepare` script fail. https://typicode.github.io/husky/#/?id=with-a-custom-script
801aaef
to
d9bd3e9
Compare
"lint:js:cli": "eslint --cache --cache-location .cache/eslint --cache-strategy content --color --ignore-path .gitignore --max-warnings 0", | ||
"lint:scss": "npm run lint:scss:cli -- \"{app,src}/**/*.scss\"", | ||
"lint:scss:cli": "stylelint --cache --cache-location .cache/stylelint --cache-strategy content --color --ignore-path .gitignore --max-warnings 0", | ||
"prepare": "node -e \"try { require('husky').install() } catch (e) {if (e.code !== 'MODULE_NOT_FOUND') throw e}\"" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This automatically installs the Git hooks when you npm install
. This is the lifecycle script recommended by Husky's documentation (and we already have postinstall
anyway).
We cannot run the recommended husky install
, though. husky
is a devDependency
, as it's not needed to run the review app. Because of that, it won't be installed on Heroku, (or when running npm install --omit=dev
) which means that prepare
would fail if we were to run husky install
. Instead we can rely on the documented MODULE_NOT_FOUND
exception from Node to ensure a smooth installation, as proposed by Husky's docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving again
You know what would be amazing? Updating the gulp.watch()
lint tasks so they also only lint the "changed files", rather than every single JavaScript and Sass file
What
This PR adds automatic linting of code being commited before
git push
.The relevant linters will run on any JavaScript and/or Scss being commited, and only what's being commited. The rest is stashed temporarily while the linting happens to ensure irrelevant files or hunks don't get in the way. The linters will also try to fix any issue they can fix automatically to make the commit only fail if a human intervention is needed.
The
pre-commit
hook get installed automatically onnpm install
(which we'll need to run after this gets merged inmain
). It can be bypassed if really getting in the way of a commit that really needs to go (though like with disabling linters, best to have a good reason for it... and Github CI will fail anyway so might as well save yourself a round-trip).As a side bonus, you can also lint your staged files without commiting by running
npx lint-staged
after staging content.Why
Linting pre-commit reduces the chance of losing time to only detecting linting issues once Github CI run and is quick enough to not burden the time it takes to commit. Most our editors can likely run ESLint on save, but there's no guarantee everyone has something like that set up so that a little extra safety that gets automatically set up when installing the project dependencies.
Note that it won't run in case of a quick edit-and-commit on Github and we'll have to go fix things manually should the CI pick up a linting issue after such edit.
How
It relies on two tools:
git add -p
.I thought there'd be some caution to have around the
package
anddist
folder to avoid messing with compiled files, butstandard
is already configured to avoid those..husky
(configurable) through itscore.hooksPath
config.Possible future developments
With Git hooks available, there's potentially other automations we could look into, like:
post-checkout
to detect if things likenpm install
need to run and warnpre-push
to run tests locally before hitting CI (not sure if that's a great one though)As linters get run automatically, we could look into linting other kinds of files. I'm thinking Markdown specifically, both for its structure with something like remark-lint and for its content with something like vale.sh that Claire mentionned recently.