|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Common Commands |
| 6 | + |
| 7 | +### Development |
| 8 | + |
| 9 | +- `npm run build` - Build TypeScript to dist/ with dual ESM/CommonJS output |
| 10 | +- `npm test` - Run all tests using Node.js built-in test runner |
| 11 | +- `npm run prepublishOnly` - Build before publishing (runs automatically) |
| 12 | + |
| 13 | +### Release Process |
| 14 | + |
| 15 | +- `npm run release:patch` - Bump patch version and publish |
| 16 | +- `npm run release:minor` - Bump minor version and publish |
| 17 | +- `npm run release:major` - Bump major version and publish |
| 18 | + |
| 19 | +Or use the automated GitHub Actions workflow by pushing tags: |
| 20 | + |
| 21 | +```bash |
| 22 | +npm version patch && git push origin v0.2.4 |
| 23 | +``` |
| 24 | + |
| 25 | +### Testing & Debugging |
| 26 | + |
| 27 | +- Set `BRANCH_PREFIX_DEBUG=1` to enable debug logging |
| 28 | +- Set `BRANCH_PREFIX_DRY_RUN=1` to test without modifying files |
| 29 | +- Tests use temporary directories and file system mocking |
| 30 | + |
| 31 | +## Architecture Overview |
| 32 | + |
| 33 | +This is a **TypeScript CLI tool** that integrates with Husky's `prepare-commit-msg` hook to automatically format Git commit messages based on branch names. |
| 34 | + |
| 35 | +### Core Design Patterns |
| 36 | + |
| 37 | +**Functional Pipeline Architecture**: The main processing follows a functional pipeline pattern in `src/core.ts`: |
| 38 | + |
| 39 | +```typescript |
| 40 | +const pipeline = pipe( |
| 41 | + applyValidationRules, |
| 42 | + logProcessingInfo, |
| 43 | + processMessage, |
| 44 | + writeResult |
| 45 | +); |
| 46 | +``` |
| 47 | + |
| 48 | +**Strategy Pattern**: Used throughout for: |
| 49 | + |
| 50 | +- **Validation Rules**: Declarative array of validation functions |
| 51 | +- **Message Processors**: Different template processing strategies |
| 52 | +- **Init Strategies**: Various Husky installation scenarios |
| 53 | + |
| 54 | +**State Management**: Immutable `ProcessingState` flows through pipeline containing configuration, branch info, messages, and flags. |
| 55 | + |
| 56 | +### Module Structure |
| 57 | + |
| 58 | +- **`src/core.ts`** - Main processing engine with functional pipeline |
| 59 | +- **`src/cli.ts`** - Command routing using declarative handler pattern |
| 60 | +- **`src/init.ts`** - Husky integration with strategy pattern for different scenarios |
| 61 | +- **`src/config.ts`** - Configuration loading from package.json with defaults |
| 62 | +- **`src/tokens.ts`** - Template rendering system with variable substitution |
| 63 | +- **`src/types.ts`** - Comprehensive TypeScript type definitions |
| 64 | + |
| 65 | +### Key Integration Points |
| 66 | + |
| 67 | +**Git Integration**: Executes `git rev-parse --abbrev-ref HEAD` to get branch names. |
| 68 | + |
| 69 | +**Husky Integration**: Creates/modifies `.husky/prepare-commit-msg` hook file. Requires Husky v9 as peer dependency. |
| 70 | + |
| 71 | +**Template System**: Supports variables like `${ticket}`, `${seg0}`, `${branch}`, `${msg}`, `${prefix:n}` for flexible message formatting. |
| 72 | + |
| 73 | +**Ticket Extraction**: Uses regex `/([A-Z]+-\d+)/i` to find Jira-style tickets (e.g., ABC-123). |
| 74 | + |
| 75 | +### Build & Package Structure |
| 76 | + |
| 77 | +**Multi-format Package**: Outputs both ESM and CommonJS with TypeScript declarations: |
| 78 | + |
| 79 | +```json |
| 80 | +{ |
| 81 | + "main": "./dist/core.cjs", |
| 82 | + "module": "./dist/core.js", |
| 83 | + "types": "./dist/core.d.ts", |
| 84 | + "bin": { "cfb": "bin/cfb.js" } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +**Multiple Entry Points**: |
| 89 | + |
| 90 | +- Main API: `import { run } from "@253eosam/commit-from-branch"` |
| 91 | +- Init utility: `import { initHusky } from "@253eosam/commit-from-branch/init"` |
| 92 | + |
| 93 | +### Configuration |
| 94 | + |
| 95 | +Configuration lives in package.json under `commitFromBranch` key: |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "commitFromBranch": { |
| 100 | + "format": "[${ticket}] ${msg}", |
| 101 | + "fallbackFormat": "[${seg0}] ${msg}", |
| 102 | + "includePattern": ["feature/*", "bugfix/*"], |
| 103 | + "exclude": ["merge", "squash", "revert"] |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Testing Architecture |
| 109 | + |
| 110 | +Tests use Node.js built-in test runner with temporary directories. Key patterns: |
| 111 | + |
| 112 | +- File system operations use temp dirs for isolation |
| 113 | +- Command-line testing with mock argv/env |
| 114 | +- Integration tests that simulate real Git hook scenarios |
| 115 | + |
| 116 | +### Release Automation |
| 117 | + |
| 118 | +**GitHub Actions** (`.github/workflows/release.yml`) handles: |
| 119 | + |
| 120 | +- Tag validation (v*.*.* format) |
| 121 | +- Version consistency checks between package.json and Git tags |
| 122 | +- Automated build, test, and NPM publishing |
| 123 | +- GitHub release creation with changelog extraction |
| 124 | + |
| 125 | +**Local Release Script** (`scripts/release.js`) provides manual alternative with GitHub CLI integration. |
| 126 | + |
| 127 | +### Error Handling Philosophy |
| 128 | + |
| 129 | +- Graceful degradation when Git operations fail |
| 130 | +- Comprehensive logging with debug mode |
| 131 | +- Safe file operations with proper error recovery |
| 132 | +- Clear user guidance for setup and troubleshooting |
0 commit comments