Skip to content

Commit

Permalink
chore: add pre-commit hook for conventional commits and linting (#127)
Browse files Browse the repository at this point in the history
- Integrated Commitizen for enforcing conventional commit messages.
- Added pre-commit hook to run lint-staged for code linting before commit.
- Configured commit-msg hook to validate commit messages using commitlint.
  • Loading branch information
rahulyadav-57 authored Oct 17, 2024
1 parent 5503143 commit 1387a35
Show file tree
Hide file tree
Showing 9 changed files with 2,715 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
ecmaVersion: 2020,
project: "./tsconfig.json",
},
ignorePatterns: ["*.cjs"],
ignorePatterns: ["*.cjs", "!.lintstagedrc.js", ".lintstagedrc.js"],
plugins: ["@typescript-eslint"],
root: true,
env: {
Expand Down
3 changes: 3 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

npx commitlint --edit $1
3 changes: 3 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

npx lint-staged
14 changes: 14 additions & 0 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# Check if commit message is provided with -m flag
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2

# Check if commit is being created from the message source (e.g., with `git commit -m`)
if echo "$HUSKY_GIT_PARAMS" | grep -qE '^-m\s'; then
if [ "$COMMIT_SOURCE" = "message" ]; then
exit 0
fi
fi

exec < /dev/tty && npx git-cz --hook || true
8 changes: 8 additions & 0 deletions .lintstagedrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require("path");
const buildEslintCommand = (filenames) =>
`next lint --fix --file ${filenames
.map((f) => path.relative(process.cwd(), f))
.join(" --file ")}`;
module.exports = {
"*.{js,jsx,ts,tsx}": [buildEslintCommand],
};
47 changes: 47 additions & 0 deletions commitlint.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { UserConfig } from '@commitlint/types';

const Configuration: UserConfig = {
extends: ['@commitlint/config-conventional'],
parserPreset: 'conventional-changelog-atom',
formatter: '@commitlint/format',
rules: {
'type-enum': [
2,
'always',
[
'feat', // New feature
'fix', // Bug fix
'docs', // Documentation changes
'style', // Changes that do not affect the meaning of the code (white-space, formatting, etc.)
'refactor', // Code changes that neither fix a bug nor add a feature
'perf', // Performance improvement
'test', // Adding missing tests or correcting existing tests
'build', // Changes that affect the build system or external dependencies (example scopes: npm)
'ci', // Changes to CI configuration files and scripts
'chore', // Other changes that don't modify src or test files
'revert', // Reverts a previous commit
],
],
'scope-enum': [
2,
'always',
[
'setup', // Project setup
'config', // Configuration files
'deps', // Dependency updates
'feature', // Feature-specific changes
'bug', // Bug fixes
'docs', // Documentation
'style', // Code style/formatting
'refactor', // Code refactoring
'test', // Tests
'build', // Build scripts or configuration
'ci', // Continuous integration
'release', // Release related changes
'other', // Other changes
],
],
},
};

export default Configuration;
Loading

0 comments on commit 1387a35

Please sign in to comment.