Skip to main content
hooksSource-backedReview first Safety · Privacy ·

Doc Auto Generator

Automatically generates or updates project documentation when session ends.

by JSONbored·added 2025-09-19·
Claude Code
HarnessClaude Code
Trigger:Stop
Review first review before installing

Open the source and read safety notes before installing.

Schema details

Install type
cli
Reading time
1 min
Difficulty score
0
Troubleshooting
Yes
Breaking changes
No
Runtime and command metadata
Trigger
Stop
Script language
bash
Script body
#!/usr/bin/env bash

echo "📚 Starting documentation generation..." >&2

# Create docs directory if it doesn't exist
mkdir -p ./docs

# Generate timestamp for session
SESSION_DATE=$(date +"%Y-%m-%d")
SESSION_TIME=$(date +"%H:%M:%S")
TIMESTAMP="$SESSION_DATE $SESSION_TIME"

# Count modified files if in git repo
MODIFIED_COUNT=0
if command -v git &> /dev/null && git rev-parse --git-dir > /dev/null 2>&1; then
  MODIFIED_COUNT=$(git diff --name-only 2>/dev/null | wc -l | xargs)
fi

echo "📊 Session summary: $MODIFIED_COUNT files modified" >&2

# JavaScript/TypeScript projects
if [ -f "package.json" ]; then
  echo "🟡 JavaScript/TypeScript project detected" >&2
  
  # Try TypeDoc first for TypeScript projects
  if ls *.ts src/**/*.ts 2>/dev/null | head -1 > /dev/null; then
    if command -v npx &> /dev/null && npx typedoc --version &> /dev/null 2>&1; then
      echo "📝 Generating TypeDoc documentation..." >&2
      npx typedoc --out ./docs/api src 2>/dev/null && echo "✅ TypeDoc documentation generated" >&2
    else
      echo "💡 Install TypeDoc for better TypeScript docs: npm install -g typedoc" >&2
    fi
  fi
  
  # Try JSDoc for JavaScript projects
  if [ -f "jsdoc.json" ] || [ -f "jsdoc.conf.json" ]; then
    if command -v npx &> /dev/null && npx jsdoc --version &> /dev/null 2>&1; then
      echo "📝 Generating JSDoc documentation..." >&2
      npx jsdoc -c jsdoc.json 2>/dev/null || npx jsdoc -c jsdoc.conf.json 2>/dev/null
      [ $? -eq 0 ] && echo "✅ JSDoc documentation generated" >&2
    fi
  fi
  
  # Try documentation.js as fallback
  if command -v npx &> /dev/null; then
    if npx documentation --version &> /dev/null 2>&1; then
      echo "📝 Generating documentation.js docs..." >&2
      npx documentation build './src/**/*.js' -f md -o ./docs/api.md 2>/dev/null
      [ $? -eq 0 ] && echo "✅ Documentation.js docs generated" >&2
    fi
  fi
fi

# Python projects
if [ -f "setup.py" ] || [ -f "pyproject.toml" ] || [ -f "requirements.txt" ]; then
  echo "🐍 Python project detected" >&2
  
  # Try pdoc for simple API docs
  if command -v pdoc &> /dev/null; then
    echo "📝 Generating pdoc documentation..." >&2
    pdoc --html --output-dir ./docs . 2>/dev/null && echo "✅ pdoc documentation generated" >&2
  elif command -v python &> /dev/null; then
    if python -c "import pdoc" 2>/dev/null; then
      echo "📝 Generating pdoc documentation..." >&2
      python -m pdoc --html --output-dir ./docs . 2>/dev/null && echo "✅ pdoc documentation generated" >&2
    fi
  fi
  
  # Try Sphinx for comprehensive docs
  if [ -f "docs/conf.py" ]; then
    if command -v sphinx-build &> /dev/null; then
      echo "📝 Building Sphinx documentation..." >&2
      sphinx-build -b html docs ./docs/_build 2>/dev/null && echo "✅ Sphinx documentation built" >&2
    fi
  elif command -v sphinx-quickstart &> /dev/null; then
    echo "📝 Setting up Sphinx documentation..." >&2
    PROJECT_NAME=$(basename "$(pwd)")
    sphinx-quickstart -q -p "$PROJECT_NAME" -a "Claude" --ext-autodoc --makefile docs 2>/dev/null
    [ $? -eq 0 ] && echo "✅ Sphinx project initialized in docs/" >&2
  fi
fi

# Go projects
if [ -f "go.mod" ]; then
  echo "🐹 Go project detected" >&2
  
  if command -v go &> /dev/null; then
    echo "📝 Generating Go documentation..." >&2
    go doc -all > ./docs/api.txt 2>/dev/null && echo "✅ Go documentation generated" >&2
    
    # Try godoc if available
    if command -v godoc &> /dev/null; then
      echo "💡 Run 'godoc -http=:6060' to serve documentation locally" >&2
    fi
  fi
fi

# Rust projects
if [ -f "Cargo.toml" ]; then
  echo "🦀 Rust project detected" >&2
  
  if command -v cargo &> /dev/null; then
    echo "📝 Generating Rust documentation..." >&2
    cargo doc --no-deps --target-dir ./docs/rust 2>/dev/null && echo "✅ Rust documentation generated" >&2
  fi
fi

# Update CHANGELOG.md
echo "📝 Updating changelog..." >&2
CHANGELOG_ENTRY="## Session $SESSION_DATE at $SESSION_TIME\n\n- Files modified: $MODIFIED_COUNT\n- Documentation updated automatically\n- Session completed\n\n"

if [ -f "CHANGELOG.md" ]; then
  # Prepend to existing changelog
  echo -e "$CHANGELOG_ENTRY$(cat CHANGELOG.md)" > CHANGELOG.md.tmp && mv CHANGELOG.md.tmp CHANGELOG.md
else
  # Create new changelog
  echo -e "# Changelog\n\n$CHANGELOG_ENTRY" > CHANGELOG.md
fi

echo "✅ Changelog updated" >&2

# Generate or update README.md if it doesn't exist
if [ ! -f "README.md" ]; then
  echo "📝 Creating basic README.md..." >&2
  PROJECT_NAME=$(basename "$(pwd)")
  cat > README.md << EOF
# $PROJECT_NAME

Project documentation generated automatically.

## Documentation

API documentation can be found in the \`docs/\` directory.

## Last Updated

$TIMESTAMP
EOF
  echo "✅ README.md created" >&2
fi

# Create documentation index
echo "📋 Creating documentation index..." >&2
cat > ./docs/index.md << EOF
# Project Documentation

Generated on: $TIMESTAMP

## Available Documentation

EOF

# List available documentation files
find ./docs -name "*.md" -o -name "*.html" -o -name "index.html" 2>/dev/null | while read -r file; do
  echo "- [$(basename "$file")]($(basename "$file"))" >> ./docs/index.md
done

echo "" >&2
echo "📚 Documentation generation completed!" >&2
echo "📁 Check the ./docs/ directory for generated documentation" >&2
echo "📋 Documentation index available at ./docs/index.md" >&2

exit 0
Full copyable content
{
  "hooks": {
    "stop": {
      "script": "./.claude/hooks/documentation-auto-generator-on-stop.sh"
    }
  }
}

About this resource

Features

  • Automatic API documentation generation for multiple languages (TypeScript, JavaScript, Python, Go, Rust) with TypeDoc 0.26.0+, JSDoc 4.0.0+, Sphinx 7.0+, pdoc 14.0+, Go doc, and Cargo doc support
  • Changelog updates with session summaries including file modification counts, timestamps, and automatic session tracking for project history
  • Support for JSDoc 4.0.0+, TypeDoc 0.26.0+, Sphinx 7.0+, pdoc 14.0+, documentation.js, and other doc generators with automatic tool detection and configuration
  • Project structure analysis and documentation with automatic detection of project type and appropriate documentation generator selection
  • README file updates and maintenance with automatic generation of basic README.md if missing and project metadata inclusion
  • Multi-format output (HTML, Markdown, PDF) supporting various documentation formats and export options for different use cases
  • Documentation index generation with automatic creation of documentation index files listing all available documentation resources
  • Session-based documentation updates with automatic timestamp tracking and session metadata for documentation versioning

Use Cases

  • Automated API documentation maintenance ensuring API documentation stays synchronized with code changes automatically after each development session
  • End-of-session project documentation updates providing automatic documentation generation when development sessions end, keeping documentation current
  • Multi-language documentation generation supporting TypeScript, JavaScript, Python, Go, and Rust projects with appropriate documentation tools
  • Changelog automation and project tracking maintaining automatic changelog updates with session summaries and file modification tracking
  • Development workflow documentation integration seamlessly integrating documentation generation into development workflows without manual intervention
  • Project onboarding and documentation setup automatically generating initial documentation structure for new projects and maintaining documentation completeness

Installation

  1. Create hooks directory: mkdir -p .claude/hooks
  2. Create hook file: touch .claude/hooks/documentation-auto-generator-on-stop.sh
  3. Make executable: chmod +x .claude/hooks/documentation-auto-generator-on-stop.sh
  4. Add configuration from Hook Configuration section above to .claude/settings.json or ~/.claude/settings.json
  5. Alternative: Use the interactive /hooks command in Claude Code

Config paths

  • Local (not committed): .claude/settings.local.json
  • User settings (global): ~/.claude/settings.json
  • Project-wide (committed): .claude/settings.json

Requirements

  • Claude Code CLI installed
  • Project directory initialized
  • Bash shell available
  • Documentation generator: TypeDoc 0.26.0+ (TypeScript), JSDoc 4.0.0+ (JavaScript), Sphinx 7.0+ or pdoc 14.0+ (Python), Go doc (Go), Cargo doc (Rust)
  • Git repository (optional, for changelog tracking)
  • Runtime environment: Node.js 18+ and npm/yarn/pnpm (for TypeDoc/JSDoc) or Python 3.8+ and pip (for Sphinx/pdoc) or Go toolchain (for Go doc) or Rust toolchain (for Cargo doc) depending on project language

Hook Configuration

{
  "hooks": {
    "stop": {
      "script": "./.claude/hooks/documentation-auto-generator-on-stop.sh"
    }
  }
}

Hook Script

#!/usr/bin/env bash

echo "📚 Starting documentation generation..." >&2

# Create docs directory if it doesn't exist
mkdir -p ./docs

# Generate timestamp for session
SESSION_DATE=$(date +"%Y-%m-%d")
SESSION_TIME=$(date +"%H:%M:%S")
TIMESTAMP="$SESSION_DATE $SESSION_TIME"

# Count modified files if in git repo
MODIFIED_COUNT=0
if command -v git &> /dev/null && git rev-parse --git-dir > /dev/null 2>&1; then
  MODIFIED_COUNT=$(git diff --name-only 2>/dev/null | wc -l | xargs)
fi

echo "📊 Session summary: $MODIFIED_COUNT files modified" >&2

# JavaScript/TypeScript projects
if [ -f "package.json" ]; then
  echo "🟡 JavaScript/TypeScript project detected" >&2

  # Try TypeDoc first for TypeScript projects
  if ls *.ts src/**/*.ts 2>/dev/null | head -1 > /dev/null; then
    if command -v npx &> /dev/null && npx typedoc --version &> /dev/null 2>&1; then
      echo "📝 Generating TypeDoc documentation..." >&2
      npx typedoc --out ./docs/api src 2>/dev/null && echo "✅ TypeDoc documentation generated" >&2
    else
      echo "💡 Install TypeDoc for better TypeScript docs: npm install -g typedoc" >&2
    fi
  fi

  # Try JSDoc for JavaScript projects
  if [ -f "jsdoc.json" ] || [ -f "jsdoc.conf.json" ]; then
    if command -v npx &> /dev/null && npx jsdoc --version &> /dev/null 2>&1; then
      echo "📝 Generating JSDoc documentation..." >&2
      npx jsdoc -c jsdoc.json 2>/dev/null || npx jsdoc -c jsdoc.conf.json 2>/dev/null
      [ $? -eq 0 ] && echo "✅ JSDoc documentation generated" >&2
    fi
  fi

  # Try documentation.js as fallback
  if command -v npx &> /dev/null; then
    if npx documentation --version &> /dev/null 2>&1; then
      echo "📝 Generating documentation.js docs..." >&2
      npx documentation build './src/**/*.js' -f md -o ./docs/api.md 2>/dev/null
      [ $? -eq 0 ] && echo "✅ Documentation.js docs generated" >&2
    fi
  fi
fi

# Python projects
if [ -f "setup.py" ] || [ -f "pyproject.toml" ] || [ -f "requirements.txt" ]; then
  echo "🐍 Python project detected" >&2

  # Try pdoc for simple API docs
  if command -v pdoc &> /dev/null; then
    echo "📝 Generating pdoc documentation..." >&2
    pdoc --html --output-dir ./docs . 2>/dev/null && echo "✅ pdoc documentation generated" >&2
  elif command -v python &> /dev/null; then
    if python -c "import pdoc" 2>/dev/null; then
      echo "📝 Generating pdoc documentation..." >&2
      python -m pdoc --html --output-dir ./docs . 2>/dev/null && echo "✅ pdoc documentation generated" >&2
    fi
  fi

  # Try Sphinx for comprehensive docs
  if [ -f "docs/conf.py" ]; then
    if command -v sphinx-build &> /dev/null; then
      echo "📝 Building Sphinx documentation..." >&2
      sphinx-build -b html docs ./docs/_build 2>/dev/null && echo "✅ Sphinx documentation built" >&2
    fi
  elif command -v sphinx-quickstart &> /dev/null; then
    echo "📝 Setting up Sphinx documentation..." >&2
    PROJECT_NAME=$(basename "$(pwd)")
    sphinx-quickstart -q -p "$PROJECT_NAME" -a "Claude" --ext-autodoc --makefile docs 2>/dev/null
    [ $? -eq 0 ] && echo "✅ Sphinx project initialized in docs/" >&2
  fi
fi

# Go projects
if [ -f "go.mod" ]; then
  echo "🐹 Go project detected" >&2

  if command -v go &> /dev/null; then
    echo "📝 Generating Go documentation..." >&2
    go doc -all > ./docs/api.txt 2>/dev/null && echo "✅ Go documentation generated" >&2

    # Try godoc if available
    if command -v godoc &> /dev/null; then
      echo "💡 Run 'godoc -http=:6060' to serve documentation locally" >&2
    fi
  fi
fi

# Rust projects
if [ -f "Cargo.toml" ]; then
  echo "🦀 Rust project detected" >&2

  if command -v cargo &> /dev/null; then
    echo "📝 Generating Rust documentation..." >&2
    cargo doc --no-deps --target-dir ./docs/rust 2>/dev/null && echo "✅ Rust documentation generated" >&2
  fi
fi

# Update CHANGELOG.md
echo "📝 Updating changelog..." >&2
CHANGELOG_ENTRY="## Session $SESSION_DATE at $SESSION_TIME\n\n- Files modified: $MODIFIED_COUNT\n- Documentation updated automatically\n- Session completed\n\n"

if [ -f "CHANGELOG.md" ]; then
  # Prepend to existing changelog
  echo -e "$CHANGELOG_ENTRY$(cat CHANGELOG.md)" > CHANGELOG.md.tmp && mv CHANGELOG.md.tmp CHANGELOG.md
else
  # Create new changelog
  echo -e "# Changelog\n\n$CHANGELOG_ENTRY" > CHANGELOG.md
fi

echo "✅ Changelog updated" >&2

# Generate or update README.md if it doesn't exist
if [ ! -f "README.md" ]; then
  echo "📝 Creating basic README.md..." >&2
  PROJECT_NAME=$(basename "$(pwd)")
  cat > README.md << EOF
# $PROJECT_NAME

Project documentation generated automatically.

## Documentation

API documentation can be found in the \`docs/\` directory.

## Last Updated

$TIMESTAMP
EOF
  echo "✅ README.md created" >&2
fi

# Create documentation index
echo "📋 Creating documentation index..." >&2
cat > ./docs/index.md << EOF
# Project Documentation

Generated on: $TIMESTAMP

## Available Documentation

EOF

# List available documentation files
find ./docs -name "*.md" -o -name "*.html" -o -name "index.html" 2>/dev/null | while read -r file; do
  echo "- [$(basename "$file")]($(basename "$file"))" >> ./docs/index.md
done

echo "" >&2
echo "📚 Documentation generation completed!" >&2
echo "📁 Check the ./docs/ directory for generated documentation" >&2
echo "📋 Documentation index available at ./docs/index.md" >&2

exit 0

Examples

Documentation Auto Generator Hook Script

Complete hook script that performs automatic documentation generation when session ends

#!/usr/bin/env bash
echo "Starting documentation generation..." >&2
mkdir -p ./docs
SESSION_DATE=$(date +"%Y-%m-%d")
SESSION_TIME=$(date +"%H:%M:%S")
MODIFIED_COUNT=0
if command -v git &> /dev/null && git rev-parse --git-dir > /dev/null 2>&1; then
  MODIFIED_COUNT=$(git diff --name-only 2>/dev/null | wc -l | xargs)
fi
echo "Session summary: $MODIFIED_COUNT files modified" >&2
if [ -f "package.json" ]; then
  if ls *.ts src/**/*.ts 2>/dev/null | head -1 > /dev/null; then
    if command -v npx &> /dev/null && npx typedoc --version &> /dev/null 2>&1; then
      echo "Generating TypeDoc documentation..." >&2
      npx typedoc --out ./docs/api src 2>/dev/null && echo "TypeDoc documentation generated" >&2
    fi
  fi
fi
echo "Documentation generation completed!" >&2
exit 0

TypeDoc Documentation Generation

Enhanced hook script using TypeDoc 0.26.0+ with entry points and configuration options

#!/usr/bin/env bash
INPUT=$(cat)
if [ -f "package.json" ]; then
  if ls *.ts src/**/*.ts 2>/dev/null | head -1 > /dev/null; then
    if command -v npx &> /dev/null && npx typedoc --version &> /dev/null 2>&1; then
      echo "Generating TypeDoc documentation..." >&2
      npx typedoc --entryPoints "./src/index.ts" --out ./docs/api --readme README.md --excludePrivate --excludeInternal 2>/dev/null && echo "TypeDoc documentation generated" >&2
    fi
  fi
fi
exit 0

Python pdoc Documentation Generation

Enhanced hook script using pdoc 14.0+ for Python project documentation generation

#!/usr/bin/env bash
if [ -f "setup.py" ] || [ -f "pyproject.toml" ]; then
  if command -v pdoc &> /dev/null; then
    echo "Generating pdoc documentation..." >&2
    pdoc --html --output-dir ./docs . 2>/dev/null && echo "pdoc documentation generated" >&2
  elif command -v python &> /dev/null; then
    if python -c "import pdoc" 2>/dev/null; then
      echo "Generating pdoc documentation..." >&2
      python -m pdoc --html --output-dir ./docs . 2>/dev/null && echo "pdoc documentation generated" >&2
    fi
  fi
fi
exit 0

Changelog Auto-Update with Session Tracking

Enhanced hook script for automatic changelog updates with session summaries and file modification tracking

#!/usr/bin/env bash
SESSION_DATE=$(date +"%Y-%m-%d")
SESSION_TIME=$(date +"%H:%M:%S")
MODIFIED_COUNT=0
if command -v git &> /dev/null && git rev-parse --git-dir > /dev/null 2>&1; then
  MODIFIED_COUNT=$(git diff --name-only 2>/dev/null | wc -l | xargs)
fi
CHANGELOG_ENTRY="## Session $SESSION_DATE at $SESSION_TIME\n\n- Files modified: $MODIFIED_COUNT\n- Documentation updated automatically\n- Session completed\n\n"
if [ -f "CHANGELOG.md" ]; then
  echo -e "$CHANGELOG_ENTRY$(cat CHANGELOG.md)" > CHANGELOG.md.tmp && mv CHANGELOG.md.tmp CHANGELOG.md
else
  echo -e "# Changelog\n\n$CHANGELOG_ENTRY" > CHANGELOG.md
fi
echo "Changelog updated" >&2
exit 0

Troubleshooting

TypeDoc generation fails with 'unable to resolve entry point' configuration error

Create tsconfig.json with explicit include paths or add 'entryPoints' to typedoc.json config. Use 'npx typedoc --entryPoints src/index.ts' to specify entry point directly in command. Verify TypeScript project structure and ensure entry points exist. Check TypeDoc version is 0.26.0+ for latest features.

CHANGELOG.md grows unbounded as every session appends duplicate timestamp entries

Implement changelog rotation by keeping only last 50 entries or use date-based sections. Archive old entries to CHANGELOG.archive.md when main file exceeds size threshold like 10KB. Use date-based grouping to consolidate multiple sessions per day. Consider using semantic versioning for changelog organization.

Documentation generation completes but docs directory remains empty after stop hook

Check documentation tool exit codes and stderr output for generation failures. Ensure write permissions on docs directory and verify sufficient disk space for generated HTML and asset files. Verify documentation generator is installed and accessible. Check for errors in documentation generator output.

Sphinx autodoc fails to import modules during documentation build process

Add project root to PYTHONPATH in hook script: 'export PYTHONPATH="${PYTHONPATH}:$(pwd)"'. Install project dependencies in documentation build environment before running sphinx-build command. Verify Sphinx version is 7.0+ for latest features. Check sphinx configuration in docs/conf.py.

Hook execution timeout when building large documentation sets on session stop

Move heavy documentation builds to separate CI job instead of stop hook. Use lightweight generators like pdoc 14.0+ for stop hook, reserving Sphinx or comprehensive builds for scheduled documentation updates. Set timeout limits for documentation generation. Consider incremental documentation updates.

JSDoc generation fails with 'Cannot find module' errors

Verify JSDoc version is 4.0.0+ and project dependencies are installed. Check jsdoc.json configuration file exists and is valid. Ensure source files are accessible and paths in configuration are correct. Install missing npm dependencies: npm install.

pdoc fails to generate documentation for Python packages

Verify pdoc version is 14.0+ and Python environment is correct. Check Python package structure and ensure init.py files exist. Verify module imports work correctly: python -c 'import your_module'. Use --html flag for HTML output: pdoc --html --output-dir ./docs .

Multiple documentation generators conflict or generate duplicate content

Configure hook to use only one documentation generator per project type. Check project type detection logic and ensure appropriate generator is selected. Use conditional logic to prevent multiple generators from running. Verify documentation output directories are separate for each generator.

#documentation#stop-hook#automation#markdown#jsdoc

Source citations

Signals

Loading live community signals…

More like this, weekly

A short, calm digest of reviewed Claude resources. Unsubscribe any time.