Skip to content

[DO NOT MERGE] [FEAT] Pre Commit Hook #4

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
10 changes: 10 additions & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');

module.exports = {
'./src/**/*' : (filenames) => [
`next lint --fix --file ${filenames
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А просто по директории src нельзя?

Copy link
Collaborator Author

@leendrew leendrew Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вся фишка именно в том, чтобы линтились только закомиченные файлы, если в прехуке просто прописать линтинг, все отлинченные файлы надо будет добавлять вторым комитом. Можно скостылить прехук:

yarn eslint --fix --quiet \"./src/**/*\"
yarn prettier --write \"./src/**/*\"
git add .
git commit -m 'chore: linted'

но это будет доп коммит на каждый коммит с неконтроллируемым выбором файлов, мне такое не нравится.
/* пройдет по первому уровню вложенности, /**/* пройдет по всем файлам во всех подпапках
В статьях и туторах у всех все работает, я как обычно об стену долблюсь 😢

.map((file) => path.relative(process.cwd(), file))
.join(' --file ')}`,
`yarn prettier --write ${filenames.join(' ')}`,
],
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start --port ${PORT:-3000}",
"lint": "next lint"
"lint": "next lint",
"prepare": "husky install"
},
"dependencies": {
"@emotion/cache": "^11.9.3",
Expand Down Expand Up @@ -36,6 +37,8 @@
"eslint": "8.19.0",
"eslint-config-next": "12.2.2",
"eslint-config-prettier": "^8.5.0",
"husky": "^8.0.0",
"prettier": "^2.7.1",
"typescript": "4.7.4"
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,11 @@ html-tokenize@^2.0.0:
readable-stream "~1.0.27-1"
through2 "~0.4.1"

husky@^8.0.0:
version "8.0.1"
resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.1.tgz#511cb3e57de3e3190514ae49ed50f6bc3f50b3e9"
integrity sha512-xs7/chUH/CKdOCs7Zy0Aev9e/dKOMZf3K1Az1nar3tzlv0jfqnYtu235bstsWTmXOR0EfINrPa97yy4Lz6RiKw==

ignore@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a"
Expand Down Expand Up @@ -2020,6 +2025,11 @@ prelude-ls@^1.2.1:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==

prettier@^2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==

process-nextick-args@~2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
Expand Down