Skip to content

fix: Replace Unix shell commands with cross-platform Node.js equivalents#13

Open
unRekable wants to merge 2 commits intoitz4blitz:mainfrom
unRekable:fix/windows-compatibility-hooks
Open

fix: Replace Unix shell commands with cross-platform Node.js equivalents#13
unRekable wants to merge 2 commits intoitz4blitz:mainfrom
unRekable:fix/windows-compatibility-hooks

Conversation

@unRekable
Copy link
Contributor

Summary

This PR provides 1:1 equivalent Node.js implementations for all Unix-only shell commands in settings.json, ensuring identical behavior on Windows.

New Cross-Platform Hooks

1. bin/hooks/typecheck-limiter.js

Equivalent to: npx tsc --noEmit 2>&1 | head -5 || true

Bash Feature Node.js Equivalent
tsc --noEmit spawn(tsc, ['--noEmit'])
head -5 Line counter, stop at 5 lines
|| true process.exit(0) always
Finds tsc Checks node_modules/.bin/tsc first

2. bin/hooks/similar-doc-detector.js

Equivalent to: The complex bash find/grep/sed command

Bash Feature Node.js Equivalent
find . -name '*.md' Recursive fs.readdirSync()
-not -path './node_modules/*' Skip node_modules in loop
-not -path './.git/*' Skip .git in loop
basename '$FILE' path.basename(filePath)
sed 's/_/ /g' .replace(/_/g, ' ')
sed 's/.md$//' .replace(/\.mdx?$/, '')
head -c 30 .substring(0, 30)
xargs grep -l fs.readFileSync() + includes()
grep -v "$FILE" Skip if same file
head -1 break after first match
$FILE from env stdin JSON tool_input.file_path

3. bin/hooks/phase-tracker.js

Equivalent to: if [ -f .agentful/state.json ]; then jq -r '.current_phase // "idle"' .agentful/state.json 2>/dev/null || echo 'idle'; else echo 'idle'; fi

Bash Feature Node.js Equivalent
[ -f ... ] fs.existsSync()
jq -r '.current_phase' JSON.parse().current_phase
// "idle" || 'idle'
2>/dev/null try/catch block

Technical Details

All hooks:

  • Read input from stdin as per Claude Code hook standard (tool_input.file_path)
  • Exit 0 for informational output (non-blocking)
  • Work on Windows, macOS, and Linux - no shell-specific commands
  • No external dependencies - pure Node.js standard library

Testing

# Test typecheck-limiter.js
node bin/hooks/typecheck-limiter.js
# Output: First 5 TypeScript errors (if any)
# Exit: 0

# Test similar-doc-detector.js  
echo '{"tool_input":{"file_path":"README.md"}}' | node bin/hooks/similar-doc-detector.js
# Output: ⚠️ Similar doc exists: path/to/similar.md
# Exit: 0

# Test phase-tracker.js
node bin/hooks/phase-tracker.js
# Exit: 0

Files Changed

  • bin/hooks/typecheck-limiter.js (new) - TypeScript check with output limiting
  • bin/hooks/similar-doc-detector.js (new) - Detect similar markdown docs
  • bin/hooks/phase-tracker.js (updated) - Track agentful phase
  • template/.claude/settings.json - Use new Node.js hooks instead of shell commands

- Add phase-tracker.js: Cross-platform Node.js replacement for jq/bash UserPromptSubmit hook
- Remove inline Unix shell commands from settings.json:
  - Removed: npx tsc --noEmit 2>&1 | head -5 (uses Unix head command)
  - Removed: Complex bash find/grep/sed command for similar docs detection
  - Removed: if [ -f ... ]; then jq ... bash command for phase tracking
- All hooks now use Node.js which works on Windows, macOS, and Linux

Fixes UserPromptSubmit hook error on Windows.
This PR provides 1:1 equivalent Node.js implementations for all Unix-only
shell commands in settings.json, ensuring identical behavior on Windows.

## New Cross-Platform Hooks

### 1. typecheck-limiter.js
Equivalent to: `npx tsc --noEmit 2>&1 | head -5 || true`
- Runs TypeScript compiler in no-emit mode
- Shows first 5 lines of errors (like `head -5`)
- Always exits 0 (like `|| true`)
- Finds local tsc in node_modules first, then global

### 2. similar-doc-detector.js
Equivalent to the bash find/grep/sed command:
- Reads file path from stdin (Claude Code hook standard)
- Extracts search term from filename (first 30 chars, underscores → spaces)
- Recursively finds all .md/.mdx files
- Warns if similar documentation exists
- Excludes node_modules and .git directories

### 3. phase-tracker.js
Equivalent to: `if [ -f .agentful/state.json ]; then jq -r '.current_phase // "idle"' ...`
- Reads current_phase from .agentful/state.json
- Defaults to "idle" if not found
- Cross-platform file existence check

## Technical Details

All hooks:
- Read input from stdin as per Claude Code hook standard
- Exit 0 for informational output (non-blocking)
- Work on Windows, macOS, and Linux
- No external dependencies (pure Node.js)

## Files Changed
- bin/hooks/typecheck-limiter.js (new)
- bin/hooks/similar-doc-detector.js (new)
- bin/hooks/phase-tracker.js (updated)
- template/.claude/settings.json (use new hooks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments