Skip to content

Add TypeScript-native hooks support #2

@drernie

Description

@drernie

Summary

Allow hooks to be written as TypeScript files instead of shell commands, leveraging Deno's native TypeScript support for type-safe, composable hooks.

Motivation

Current hooks are shell commands which limits:

  • Type safety
  • Code reuse and composition
  • Access to Deno APIs
  • Debugging capabilities
  • IDE support

Proposed Solution

Support TypeScript files as hook implementations:

// .deno-hooks/pre-commit.ts
import type { HookContext, HookResult } from "jsr:@theswanfactory/deno-hooks";

export default async function(ctx: HookContext): Promise<HookResult> {
  const files = ctx.files.filter(f => f.endsWith('.ts'));
  
  for (const file of files) {
    const content = await Deno.readTextFile(file);
    if (content.includes('console.log')) {
      return {
        success: false,
        message: `Found console.log in ${file}`,
      };
    }
  }
  
  return { success: true };
}

Configuration:

hooks:
  pre-commit:
    - id: no-console-log
      run: .deno-hooks/pre-commit.ts
      pass_filenames: true

Benefits

  • ✅ Type-safe hooks with full IDE support
  • ✅ Access to all Deno APIs (fs, network, subprocess, etc.)
  • ✅ Composable - import utilities and JSR packages
  • ✅ Debuggable with Deno's debugger
  • Impossible in Husky/pre-commit - unique to Deno

Implementation

  1. Detect .ts files in run field
  2. Use dynamic import to load TypeScript hooks
  3. Pass standardized HookContext object
  4. Validate return type matches HookResult

Files to modify:

  • src/executor.ts - Add TypeScript hook execution
  • src/hook.ts - Export HookContext and HookResult types

Priority

HIGH - Game-changing feature that differentiates from Node.js alternatives

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions