-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run prettier and pre-commit hooks only on staged files (#613)
## 🔧 Problem Currently, the pre-commit hook managed by husky run prettier on all the files for each commit, even if the files were not changed. It slows down the dev process unnecessarily most of the time. ## 🍰 Solution Run linting only on staged files when commiting. ## 🚨 Points to watch / comments My first attempt was to keep using husky with https://github.com/lint-staged/lint-staged. It works well for prettier but doesn't work for ruff as it doesn't manage `pipenv run` nor `virtualenv` correctly. It looks very "javascript centric". My second and final attempt was to use https://pre-commit.com/. It's written in Python, it can manage every language (with hooks or using shell commands) and is well suited for our environment as we already use `pipenv` and Python. `npm` commands removed: - `format:check` => replaced by `lint:all` and changed in the CI file `npm` commands added: - `lint:prettier` lint one file using prettier. Used by other npm commands. - `lint:prettier:all` lint all files in the project using prettier. - `lint:ruff:all` lint all python files using ruff check and format (with sort option selected). - `lint:all` prettier and ruff lint on all files. - `fix:prettier` lint and fix the errors using prettier for one file (used in pre-commit hook) - `fix:prettier:all` lint and fix the errors using prettier for all the files. - `fix:ruff:all` lint all python files using ruff check and format (with sort option selected) and fix the errors - `fix:all` prettier and ruff fix on all files A `pyproject.toml` file was added to tell ruff to not check the `data/` directory. I've added some simple checks with pre-commit like yaml format, line-ending checks (avoid mixing windows/linux/macox line ending), trailing-spaces removal and a check to prevent commiting directly on master (I suppose the git repo is already configured to avoid that, but I like to have it locally before I push). ## :desert_island: How to test First, uninstall husky: npx husky uninstall Check that the command `git config --get core.hookspath` returns nothing. If it gives you something like `.husky/\_` unset it manually using `git config --unset-all core.hooksPath` Install pre-commit: pipenv install pipenv run pre-commit install Test that it's working as expected by issuing the following command: pipenv run pre-commit run --all-files You should get something like that: ![image](https://github.com/MTES-MCT/ecobalyse/assets/154904/80f20211-9791-425f-a32c-e293a04ea8d6)
- Loading branch information
Showing
13 changed files
with
301 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.3.0 | ||
hooks: | ||
- id: check-yaml | ||
- id: end-of-file-fixer | ||
exclude: (svg|icomoon|rapidoc-9.3.4.min.js) | ||
- id: trailing-whitespace | ||
exclude: (vendor|rapidoc-9.3.4.min.js) | ||
- id: no-commit-to-branch | ||
name: "don't commit to master" | ||
args: [--branch, master] | ||
|
||
- repo: https://github.com/charliermarsh/ruff-pre-commit | ||
rev: v0.4.7 | ||
hooks: | ||
# Run the linter. | ||
- id: ruff | ||
args: [ --select, I, --fix, --exit-non-zero-on-fix ] | ||
# Run the formatter. | ||
- id: ruff-format | ||
|
||
- repo: local | ||
hooks: | ||
- id: app-prettier | ||
name: run prettier | ||
language: system | ||
files: ^.*$ | ||
types_or: [javascript, json] | ||
entry: | | ||
bash -c 'npm --prefix . run fix:prettier --write "${@}"' -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ public/icomoon/* | |
public/vendor/* | ||
styles.scss | ||
tests/e2e-*-output.json | ||
dist/ | ||
Pipfile.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
/*.csv | ||
/*.zip | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,15 @@ | |
"build:init": "./bin/update-version.sh && mkdir -p dist && cp -r public/* dist/ && npm run db:build", | ||
"processes:build": "elm make src/ComputeAggregated.elm --output=compute-aggregated-app.js && node compute-aggregated.js", | ||
"db:build": "./bin/build-db", | ||
"lint:prettier": "prettier --config .prettierrc --check", | ||
"lint:prettier:all": "npm run lint:prettier -- .", | ||
"lint:ruff:all": "pipenv run ruff check --extend-select I && pipenv run ruff format --check", | ||
"lint:all": "npm run lint:prettier:all && npm run lint:ruff:all", | ||
"fix:prettier": "npm run lint:prettier -- --write", | ||
"fix:prettier:all": "npm run lint:prettier -- --write .", | ||
"fix:ruff:all": "pipenv run ruff check --extend-select I --fix && pipenv run ruff format", | ||
"fix:all": "npm run fix:ruff:all && npm run fix:prettier:all", | ||
"format:json": "npx [email protected] --write . && pipenv run ruff check --select I --fix && pipenv run ruff format", | ||
"format:check": "npx [email protected] --check . && pipenv run ruff check --select I && pipenv run ruff format --check", | ||
"prepare": "husky", | ||
"server:build": "npm run db:build && elm make src/Server.elm --optimize --output=server-app.js", | ||
"server:dev": "npm run server:build && nodemon server.js --config nodemon.json", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[project] | ||
requires-python = ">= 3.11" | ||
|
||
[tool.ruff] | ||
exclude = ["data/"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters