- π Zero dependencies - Lightweight and fast
- π¦ TypeScript native - Full type safety out of the box
- π― Simple API - Register and run hooks with minimal boilerplate
- π§ Cross-platform - Works on Windows, macOS, and Linux
- β‘ Modern Node.js - Built for Node.js 18+
npm install --save-dev git-hooks-cli
# or
yarn add -D git-hooks-cli
# or
pnpm add -D git-hooks-cliimport { createHookRunner, GIT_HOOKS } from 'git-hooks-cli'
// Create a hook runner
const runner = createHookRunner()
// Register a pre-commit hook
runner.register({
name: 'pre-commit',
command: 'npm run lint',
})
// Run the hook
await runner.run('pre-commit', ['file1.ts', 'file2.ts'])Creates a new HookRunner instance.
import { createHookRunner } from 'git-hooks-cli'
const runner = createHookRunner()Register a new hook.
runner.register({
name: 'pre-commit',
command: 'npm run lint',
args: ['--fix'],
condition: (files) => files.some(f => f.endsWith('.ts')),
})Execute a registered hook.
const success = await runner.run('pre-commit', ['src/index.ts'])List all registered hooks.
const hooks = runner.list()
console.log(hooks)Remove a registered hook.
runner.unregister('pre-commit')Clear all registered hooks.
runner.clear()This package provides a CLI tool for managing git hooks:
# Install the CLI globally
npm install -g git-hooks-cli
# Use npx to run directly
npx git-hooks
# Or use the local installation after npm install
./node_modules/.bin/git-hooks install| Command | Description |
|---|---|
git-hooks install [hook-name] |
Install git hooks from config |
git-hooks uninstall [hook-name] |
Remove installed hooks |
git-hooks list |
List configured hooks |
git-hooks status |
Show installed vs configured hooks |
git-hooks run <hook-name> |
Run a hook manually |
Configure hooks in package.json:
{
"name": "my-project",
"git-hooks": {
"pre-commit": "npm run lint",
"pre-push": "npm run test"
}
}Or in .git-hooksrc:
{
"pre-commit": "npm run lint",
"pre-push": "npm run test"
}All standard git hooks are supported:
| Hook | Description |
|---|---|
pre-commit |
Before a commit is created |
prepare-commit-msg |
After prepare-message but before editor |
commit-msg |
After commit message is set |
post-commit |
After a commit is created |
pre-push |
Before pushing to remote |
post-merge |
After a merge completes |
pre-rebase |
Before a rebase starts |
| And more... |
See the Git hooks documentation for the full list.
runner.register({
name: 'pre-commit',
command: 'npm run lint && npm run typecheck',
})runner.register({
name: 'pre-commit',
command: 'npm run test',
condition: (files) => files.some(f => f.includes('test')),
})runner.register({
name: 'pre-commit',
command: 'npx lint-staged',
})Contributions are welcome! Please read our Contributing Guide for details.
MIT License - see LICENSE for details.
- Inspired by the original git-hooks package
- Built for the modern Node.js ecosystem