{"slug": "one-claude-md-doesn-t-fit-every-project-split-your-templates-by-situation-not-by", "title": "One CLAUDE.md Doesn't Fit Every Project: Split Your Templates by Situation, Not by Stack", "summary": "A freelance developer reorganized their Claude Code CLAUDE.md templates around project situations — new projects, inherited codebases, and billable client work — rather than tech stacks, arguing that working rules depend on risk profile rather than framework. They also published a PreToolUse Bash hook that blocks commands touching .env, .envrc, credentials.json and similar secret files, since settings.json deny rules only cover file tools, not shell commands. The hook strips .env.example from the command string before matching so template reads pass while real secrets files are blocked.", "body_md": "Most CLAUDE.md templates you'll find are organized by stack: one for React, one for Laravel, one for Rails. When I started using Claude Code for freelance client work, I collected a few of those — and then barely used them.\n\nThe problem wasn't the stack sections. It was that the *working rules* I actually needed had almost nothing to do with the framework. They depended on the situation the project was in:\n\nSame stack, three completely different rule sets. So I reorganized my templates around situations instead. This post walks through what changes between them, and then covers a second, more mechanical safeguard: a hook that stops `.env` files from leaking into the session through Bash.\n\nOn a new project, Claude Code will happily invent conventions if you don't provide any — a different error-handling style in every file, tests for some features and not others. The template's job is to be opinionated early, while decisions are still cheap:\n\nInherited codebases flip most of that on its head. Here the expensive mistake isn't inconsistency, it's breaking something that was working in production. The rules get defensive:\n\nNotice that none of this mentions a framework. A legacy Laravel app and a legacy Next.js app need almost identical rules, because the risk profile is the same.\n\nBillable work adds a layer that has nothing to do with code at all, and it's the layer where freelancers actually lose money. My client-project template carries it alongside the technical rules:\n\nA few habits that made this work in practice:\n\n`<angle brackets>` is a rule Claude can't act on yet. Project name, actual build/test commands, where backups live — the specifics are the point.`~/.claude/CLAUDE.md`. Rules that are about CLAUDE.md is instruction, not enforcement — it guides judgment, but it isn't a lock. Secrets are the clearest example of why that distinction matters.\n\nThe obvious safeguard is permission rules in `.claude/settings.json`: deny `Read` and `Edit` on `.env` files. That covers the file tools. It does **not** cover Bash. Secrets leak through shell commands all the time:\n\n```\ncat .env\ngrep -r \"API_KEY\" .\ncp .env /tmp/backup/\n```\n\nA PreToolUse hook can watch the Bash tool specifically and block any command that touches `.env`-style files. Here's the one I use, in full:\n\n``` bash\n#!/usr/bin/env bash\n# PreToolUse hook: keep .env files and credential files out of Claude's hands.\n#\n# Why a hook in addition to the Read/Edit deny rules in settings.json:\n#   Permission rules govern the file tools, but secrets also leak through Bash —\n#   `cat .env`, `grep -r KEY .`, `cp .env somewhere`. This hook watches Bash commands\n#   that touch .env-style files and blocks them.\n# How .env.example is handled:\n#   The example file (dummy values, meant to be shared) is stripped from the command\n#   string before matching, so `cat .env.example` passes. Anything left that still\n#   mentions a real .env file is blocked — including `cp .env.example .env`. That one\n#   is a legitimate first-run setup step; run it manually in your own terminal.\n#   Creating the real secrets file is a human moment anyway.\n# How it works:\n#   Same pattern as block-dangerous-bash.sh — JSON on stdin, .tool_input.command,\n#   exit code 2 blocks execution.\n# Requires: jq must be installed.\n# How to adjust: if your project stores secrets under a different name (.envrc,\n#         credentials.json, service-account.json), add it to the pattern list below.\n\nset -euo pipefail\n\ninput=$(cat)\ntool=$(printf '%s' \"$input\" | jq -r '.tool_name // empty')\n\n# Only Bash needs this hook; the file tools are covered by settings.json deny rules.\n[ \"$tool\" = \"Bash\" ] || exit 0\n\ncommand=$(printf '%s' \"$input\" | jq -r '.tool_input.command // empty')\n\n# Remove the shareable template name before matching, so it alone never triggers a block.\nrest=\"${command//.env.example/}\"\n\ncase \"$rest\" in\n  *\".env\"* | *\".envrc\"* | *\"credentials.json\"* | *\"service-account\"*.json* | *\".npmrc\"* | *\".aws/credentials\"*)\n    echo \"Blocked by hook (possible secret access): ${command}\" >&2\n    echo \"Commands that touch .env / credential files are blocked. If you need a value from one, ask the user to provide it another way; if you were creating .env from .env.example, run that one command manually.\" >&2\n    exit 2\n    ;;\nesac\n\nexit 0\n```\n\nWire it up in `.claude/settings.json` like this:\n\n```\n{\n  \"hooks\": {\n    \"PreToolUse\": [\n      {\n        \"matcher\": \"Bash\",\n        \"hooks\": [\n          { \"type\": \"command\", \"command\": \"\\\"$CLAUDE_PROJECT_DIR\\\"/.claude/hooks/guard-env-files.sh\" }\n        ]\n      }\n    ]\n  }\n}\n```\n\nSave the script under `.claude/hooks/`, `chmod +x` it, and make sure `jq` is installed. Exit code 2 from a PreToolUse hook blocks the tool call and feeds the stderr message back to Claude, so it understands *why* it was stopped and can suggest an alternative instead of retrying blindly.\n\nTwo design choices worth pointing out:\n\n`.env.example` stays readable.`cat .env.example` passes.`cp .env.example .env` is blocked on purpose.\nAnd when you genuinely need to know what's in an `.env` — usually it's *which keys exist*, not their values — read it yourself and paste the key names into the conversation. The values rarely need to enter the chat at all.\n\nIf your project keeps secrets under different names (`.envrc`, `credentials.json`, a service-account file), add them in both places: the settings.json deny list *and* the hook's pattern list. They guard different doors — file tools versus Bash — and a secrets file needs both doors watched.\n\nThese templates and hooks are the core of a kit I put together for my own client work and now sell as the **Claude Code Freelance Field Kit** ($29 on Gumroad): the four situation-based CLAUDE.md templates, five skills (bug investigation, code review, refactor planning, test-adding, handover documents), three hooks including the one above and a dangerous-command blocker, plus the client-work paperwork — estimate research, progress update, and scope clarification prompts, delivery/security/handover checklists, and work-log and estimate templates.\n\nFull disclosure: I'm the seller. The two ideas in this post — situation-based templates and the .env guard hook — are complete as they stand, whether or not the kit is useful to you.\n\n[https://osakadxlabo.gumroad.com/l/claude-code-freelance-field-kit](https://osakadxlabo.gumroad.com/l/claude-code-freelance-field-kit)\n\n*Disclosure: This post was drafted with AI assistance and reviewed and edited by me. The configurations and hook script are from my own working setup.*", "url": "https://wpnews.pro/news/one-claude-md-doesn-t-fit-every-project-split-your-templates-by-situation-not-by", "canonical_source": "https://dev.to/osaka_dx_engineer/one-claudemd-doesnt-fit-every-project-split-your-templates-by-situation-not-by-stack-1jfi", "published_at": "2026-11-23 14:12:07+00:00", "updated_at": "2026-11-23 14:13:37.762554+00:00", "lang": "en", "topics": ["ai-tools", "ai-agents", "developer-tools", "ai-safety"], "entities": ["Claude Code", "Anthropic", "jq"], "also_reported_by": [], "takeaway": "Permission deny rules on Read/Edit don't cover Bash, so add a PreToolUse hook that inspects .tool_input.command and exits 2 to block any command touching .env-style files, stripping .env.example first so template reads still pass.", "recommendation": {"advice": "Pair settings.json deny rules on .env with a PreToolUse Bash hook that exits 2 on commands touching .env, .envrc or credentials files, stripping .env.example before matching.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/one-claude-md-doesn-t-fit-every-project-split-your-templates-by-situation-not-by", "markdown": "https://wpnews.pro/news/one-claude-md-doesn-t-fit-every-project-split-your-templates-by-situation-not-by.md", "text": "https://wpnews.pro/news/one-claude-md-doesn-t-fit-every-project-split-your-templates-by-situation-not-by.txt", "jsonld": "https://wpnews.pro/news/one-claude-md-doesn-t-fit-every-project-split-your-templates-by-situation-not-by.jsonld"}}