Skip to content

TheSwanFactory/deno-hooks

Repository files navigation

Deno Hooks

Zero-dependency git hooks for Deno projects. Run your tasks automatically before commits and pushes.

Why Deno Hooks?

  • Pure Deno - No Python (pre-commit) or Node.js (Husky) required
  • Fast - Runs in milliseconds, no complex runtime
  • Simple - Just shell commands, no magic
  • Transparent - Generated hooks are readable bash scripts

Quick Start

Run this command in your Deno project:

deno run -A jsr:@theswanfactory/deno-hooks

That's it! If you don't have a configuration file, it will offer to create one for you with sensible defaults.

What You Get

Hooks run automatically on every commit:

git commit -m "fix: typo"
# Running pre-commit hooks...
#   deno task fmt
#   deno task lint
# ✓ All hooks passed

Make It Easier

Add to your deno.json so you can run deno task hooks:

{
  "imports": {
    "deno-hooks": "jsr:@theswanfactory/deno-hooks@^0.3.0"
  },
  "tasks": {
    "hooks": "deno run -A deno-hooks"
  }
}

Configuration

Minimal Setup (deno-hooks.yml)

hooks:
  pre-commit:
    - deno task fmt
    - deno task lint

  pre-push:
    - deno task test

That's it! Each hook is just a list of commands to run.

Define Your Tasks (deno.json)

{
  "tasks": {
    "fmt": "deno fmt --check",
    "lint": "deno lint",
    "test": "deno test -A"
  }
}

Alternative: Configure in deno.json

You can also configure hooks directly in deno.json:

{
  "tasks": {
    "fmt": "deno fmt --check",
    "lint": "deno lint",
    "test": "deno test -A"
  },
  "deno-hooks": {
    "pre-commit": [
      "deno task fmt",
      "deno task lint"
    ],
    "pre-push": [
      "deno task test"
    ]
  }
}

CLI Options

Installation Flags

# Skip interactive prompts (use defaults)
deno run -A jsr:@theswanfactory/deno-hooks --yes

# Show detailed output during installation
deno run -A jsr:@theswanfactory/deno-hooks --verbose

# Show help
deno run -A jsr:@theswanfactory/deno-hooks --help

Available Options

Option Short Description
--yes -y Skip interactive prompts and use defaults
--verbose -v Show detailed output
--help -h Show help message

How It Works

Generated Hook Scripts

When you install, deno-hooks generates self-contained shell scripts in .git/hooks/:

#!/bin/sh
# Generated by deno-hooks - DO NOT EDIT
# To update, run: deno task hooks

set -e

deno task fmt
deno task lint

echo "✓ All hooks passed"

Key features:

  • Self-contained: No reference to deno-hooks
  • Simple: Just runs commands in order
  • Fail-fast: set -e stops on first error
  • Transparent: Easy to read and debug

Common Use Cases

Format and Lint Before Commit

hooks:
  pre-commit:
    - deno task fmt
    - deno task lint

Run Tests Before Push

hooks:
  pre-push:
    - deno task test

Custom Scripts

hooks:
  pre-commit:
    - deno task fmt
    - deno task lint
    - deno run -A scripts/check-todos.ts
    - ./scripts/validate.sh

Multiple Tasks

hooks:
  pre-commit:
    - deno task fmt
    - deno task lint
    - deno task typecheck

  pre-push:
    - deno task test
    - deno task test:integration
    - deno run -A scripts/build.ts

Available Git Hooks

You can configure any standard git hook:

  • pre-commit - Before commits (most common)
  • pre-push - Before pushing to remote
  • commit-msg - Validate commit messages
  • pre-rebase - Before rebasing
  • And more - see git hooks documentation

Troubleshooting

Hooks not running?

# Reinstall hooks
deno task hooks

# Or with verbose output to see what's happening
deno task hooks --verbose

Permission errors?

# Hooks need -A flag for full permissions
deno run -A jsr:@theswanfactory/deno-hooks

Want to skip hooks temporarily?

# Use --no-verify flag
git commit --no-verify -m "emergency fix"

Need to debug?

# Check what hooks are installed
cat .git/hooks/pre-commit

# Run hooks manually to see output
./.git/hooks/pre-commit

CI/CD Integration

You can run the same tasks in CI/CD:

# GitHub Actions example
- name: Format check
  run: deno task fmt

- name: Lint
  run: deno task lint

- name: Tests
  run: deno task test

Since hooks just run deno task commands, your CI and git hooks stay in sync.

Migration from v0.2.x

Version 1.0.0 is a major simplification. The old format:

# OLD (v0.2.x)
hooks:
  pre-commit:
    - id: deno-fmt
      run: deno-fmt
      glob: "*.{ts,js}"
      pass_filenames: true

Becomes:

# NEW (v1.0.0+)
hooks:
  pre-commit:
    - deno task fmt

Key changes:

  • No more built-in hooks (deno-fmt, deno-lint, deno-test)
  • No more file glob patterns
  • No more pass_filenames logic
  • Just simple shell commands

Why simpler?

  • File filtering is git's job (staged files)
  • Deno tasks handle their own file discovery
  • Less magic = easier to understand and debug
  • Generated hooks work without deno-hooks installed

Learn More

License

MIT

About

Zero-dependency git hooks framework for Deno projects

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages