API Reference
This page contains the API reference for bun-git-hooks
. It documents all the types, interfaces, and configurations available in the package.
Configuration Types
GitHooksConfig
The main configuration type for the package. It defines the structure of your git hooks configuration.
ts
type GitHooksConfig = {
// Git hook configurations
[hookName in GitHook]?: string | {
'stagedLint'?: StagedLintConfig
}
} & {
// Global options
'preserveUnused'?: boolean | GitHook[]
'verbose'?: boolean
'stagedLint'?: StagedLintConfig
}
Properties
[hookName]: Configuration for individual git hooks
- Type:
string | { stagedLint?: StagedLintConfig }
- Optional
- Example:
'preCommit': 'bun run lint'
- Type:
preserveUnused
- Type:
boolean | GitHook[]
- Optional
- Description: Specifies which hooks should not be managed by bun-git-hooks
- Example:
preserveUnused: ['post-checkout', 'post-merge']
- Type:
verbose
- Type:
boolean
- Optional
- Description: Enables verbose logging
- Default:
false
- Type:
stagedLint
- Type:
StagedLintConfig
- Optional
- Description: Global staged linting configuration
- Type:
StagedLintConfig
Configuration for staged file linting.
ts
interface StagedLintConfig {
[pattern: string]: StagedLintTask
}
type StagedLintTask = string | string[]
Pattern Properties
- [pattern]: Git-style glob pattern for matching files
- Type:
string | string[]
- Description: Command(s) to run on matched files
- Example:
'*.ts': 'eslint --fix'
- Type:
Supported Git Hooks
The following Git hooks are supported:
preCommit
: Run before committing (supports stagedLint)prepareCommitMsg
: Run before the commit message editor is openedcommitMsg
: Run to verify commit messagepostCommit
: Run after committingprePush
: Run before pushingpostCheckout
: Run after checking out a branchpreRebase
: Run before rebasingpostMerge
: Run after mergingpostRewrite
: Run after commands that rewrite commitspreAutoGc
: Run before garbage collection
Environment Variables
Configuration Variables
SKIP_BUN_GIT_HOOKS
- Type:
string
- Description: Skip hook execution when set to "1"
- Example:
SKIP_BUN_GIT_HOOKS=1 git commit -m "quick fix"
- Type:
BUN_GIT_HOOKS_RC
- Type:
string
- Description: Path to custom initialization script
- Example:
BUN_GIT_HOOKS_RC=/path/to/init.sh
- Type:
SKIP_INSTALL_GIT_HOOKS
- Type:
string
- Description: Skip hook installation when set to "1"
- Example:
SKIP_INSTALL_GIT_HOOKS=1 bun install
- Type:
Configuration File
The configuration can be defined in any of these files:
bash
git-hooks.config.ts # TypeScript (recommended)
git-hooks.config.js # JavaScript
git-hooks.config.mjs # ES Modules
git-hooks.config.cjs # CommonJS
git-hooks.config.json # JSON
Or in package.json
:
json
{
"git-hooks": {
"preCommit": "bun run lint"
}
}
Examples
Basic Configuration
ts
const config: GitHooksConfig = {
'preCommit': 'bun run lint',
'commitMsg': 'bun commitlint --edit $1',
'prePush': 'bun run test'
}
Advanced Configuration with Staged Linting
ts
const config: GitHooksConfig = {
'preCommit': {
'stagedLint': {
'*.{js,ts}': ['eslint --fix', 'prettier --write'],
'*.{css,scss}': 'stylelint --fix'
}
},
'verbose': true,
'preserveUnused': ['postCheckout']
}
Dynamic Configuration
ts
const config: GitHooksConfig = {
'preCommit': process.env.CI
? 'bun run test:ci'
: {
'stagedLint': {
'*.ts': 'eslint --fix'
}
},
'verbose': !process.env.CI
}