commitlint + Husky 사용법
commitlint + Husky 사용법을 정리한 페이지입니다.
Posted By HyunJinNo
Tags
commitlint, Husky, VSCode
1. 개요
commitlint + Husky 사용법에 대해 정리한 페이지입니다.
2. commitlint 사용하기
2.1. commitlint 설치하기
먼저 다음 명령어를 입력하여 commitlint를 설치합니다.
bash
1pnpm add -D @commitlint/cli @commitlint/config-conventional @commitlint/typesInfo.
@commitlint/types는 TypeScript로 설정 파일을 만들 때 필요한 패키지입니다. 만약 json, js, yml 등 다른 포맷을 사용하는 경우 필요하지 않습니다.
2.2. commitlint 설정하기
프로젝트 루트에 commitlint.config.ts 파일을 생성하고 다음과 같이 작성합니다.
typescript
1import type { UserConfig } from "@commitlint/types";
2
3const Configuration: UserConfig = {
4 extends: ["@commitlint/config-conventional"],
5};설정 이후 Conventional Commits 규칙을 바로 사용할 수 있습니다.
2.3. Rules 설정하기
다음과 같이 Commit 규칙 커스텀이 가능합니다.
typescript
1import type { UserConfig } from "@commitlint/types";
2
3const Configuration: UserConfig = {
4 extends: ["@commitlint/config-conventional"],
5 rules: {
6 "subject-case": [
7 2,
8 "never",
9 ["sentence-case", "start-case", "pascal-case", "upper-case"],
10 ],
11 "subject-empty": [2, "never"],
12 "subject-full-stop": [2, "never", "."],
13 "subject-max-length": [2, "always", Infinity],
14 "subject-min-length": [2, "always", 0],
15 "type-case": [2, "always", "lower-case"],
16 "type-empty": [2, "never"],
17 "type-enum": [
18 2,
19 "always",
20 [
21 "build",
22 "chore",
23 "ci",
24 "docs",
25 "feat",
26 "fix",
27 "perf",
28 "refactor",
29 "revert",
30 "style",
31 "test",
32 ],
33 ],
34 "type-max-length": [2, "always", Infinity],
35 "type-min-length": [2, "always", 0],
36 },
37};
38
39export default Configuration;규칙 설정 방법과 지원하는 규칙은 다음 링크를 참고하시길 바랍니다.
2.4. Husky 설치하기
Husky를 사용하여 Commit할 때 자동으로 Commit message를 검사할 수 있습니다.
다음 명령어를 입력하여 Husky를 설치합니다.
bash
1pnpm add -D husky
2pnpm husky init이후 .husky 디렉토리에서 pre-commit 파일을 삭제합니다.
2.5. commit-msg hook 추가하기
다음 명령어를 입력하여 commit-msg hook을 추가합니다.
macOS:
bash
1echo "pnpm dlx commitlint --edit \$1" > .husky/commit-msgWindows:
bash
1echo "pnpm dlx commitlint --edit `$1`" > .husky/commit-msgcommit-msg hook 추가 이후 commit 시 commitlint 가 실행되어 commit message를 자동으로 검증합니다.
유효한 commit message가 작성되면 커밋을 진행하고, 유효하지 않은 commit message가 작성되면 commit을 취소하게 됩니다.