doctor.sh

Project health check

Tier 2designengineering

A broken local setup or missing generated files that a new contributor has to find out by hand. This gives them a list of what to fix.

DOCTOR.SH FRESH CLONE READY TO WORK FIX LIST
Add this to your project
Add "Project health check" from ntent to this repo.

Fetch https://ntent.app/r/f/doctor as plain text. Write it verbatim to scripts/doctor.sh. Then make it executable.

Then check it: Run it on a fresh clone. Every line is a pass, or names the one thing to fix.

Reads https://ntent.app/r/f/doctor

Read the code
doctor.shscripts/doctor.sh · make it executable
283 lines
#!/usr/bin/env bash
#
# Preflight for a fresh clone. Reports what is installed, what is missing, and
# the exact command that fixes each gap.
#
# THREE RULES, and they are what make the difference between a doctor script
# people run and one they ignore.
#
# 1. It never exits non-zero. A doctor that fails is a gate, and this is not a
#    gate. It is a diagnostic you run when something is wrong, and it has to
#    finish and print everything even when half the machine is missing.
#
# 2. Sections are labelled by role. A designer who only runs the web preview
#    should be able to see at a glance that the database section does not apply
#    to them. Without this, every missing item reads as "you are broken".
#
# 3. Every failure line contains its remedy. Not "pnpm missing", but
#    "pnpm missing. Run: npm i -g pnpm". The whole point is that the person
#    reading it does not have to go and ask someone.
#
# Run:  bash scripts/doctor.sh   (a package.json "doctor" script, if the repo has one, runs
#       the same thing; with pnpm 11 write `pnpm run doctor`, because `pnpm doctor` is pnpm's own)

cd "$(git rev-parse --show-toplevel 2>/dev/null || echo .)" || exit 0

# ---------------------------------------------------- THE REPO'S TOOLING ---
# Print only commands that exist here. A banner that says `pnpm run check` in a
# repo with no such script, or `cp .env.example .env.local` with no example file,
# sends someone to a "command not found" and costs the rest of it its credit.
# (Same block in doctor.sh and session-status.sh: each stays a single file.)

# The package manager: the packageManager field first, then the lockfile.
pm=""
if [ -f package.json ]; then
  pm=$(sed -n 's/.*"packageManager"[[:space:]]*:[[:space:]]*"\([a-z]*\)@.*/\1/p' package.json | head -n 1)
fi
if [ -z "$pm" ]; then
  if   [ -f pnpm-lock.yaml ] || [ -f pnpm-workspace.yaml ]; then pm=pnpm
  elif [ -f yarn.lock ]; then pm=yarn
  elif [ -f bun.lockb ] || [ -f bun.lock ]; then pm=bun
  elif [ -f package-lock.json ]; then pm=npm
  elif [ -f package.json ]; then pm=npm
  fi
fi

# Does package.json define this script? node when there is one, grep when not.
has_script() {
  [ -f package.json ] || return 1
  if command -v node >/dev/null 2>&1; then
    node -e 'try { process.exit(require("./package.json").scripts?.[process.argv[1]] ? 0 : 1) } catch { process.exit(1) }' "$1" 2>/dev/null
  else
    grep -q "\"$1\"[[:space:]]*:" package.json 2>/dev/null
  fi
}

# `pnpm dev`, `yarn dev`, `npm run dev`, `bun run dev`. npm and bun need `run`.
run_cmd() {
  case "$pm" in
    npm|bun) echo "$pm run $1" ;;
    *)       echo "$pm $1" ;;
  esac
}

# Is the ntent hook reached from the hook this repo actually runs? Prints how,
# and prints nothing when it is not. Three ways it is: core.hooksPath is ours;
# a hook in the folder core.hooksPath names (Husky's .husky/pre-commit, a
# .githooks folder) calls scripts/git-hooks/pre-commit; or the Python
# pre-commit framework has a local hook that does.
hook_wired() {
  hp="$(git config core.hooksPath 2>/dev/null)"
  if [ "$hp" = "scripts/git-hooks" ]; then echo "core.hooksPath = scripts/git-hooks"; return 0; fi
  for f in .husky/pre-commit .githooks/pre-commit "${hp:+$hp/pre-commit}" "${hp:+$hp/../pre-commit}" .git/hooks/pre-commit; do
    [ -n "$f" ] && [ -f "$f" ] && grep -q 'scripts/git-hooks/pre-commit' "$f" 2>/dev/null \
      && { echo "chained from $f"; return 0; }
  done
  if [ -f .pre-commit-config.yaml ] && grep -q 'scripts/git-hooks/pre-commit' .pre-commit-config.yaml 2>/dev/null; then
    echo "a local hook in .pre-commit-config.yaml"; return 0
  fi
  return 1
}

# What to say when it is not. Never a command that would switch the repo's own
# hooks off: setting core.hooksPath over Husky's disables Husky.
hook_fix() {
  hp="$(git config core.hooksPath 2>/dev/null)"
  if [ -n "$hp" ]; then
    echo "core.hooksPath is $hp, so add one line to the pre-commit hook there: bash scripts/git-hooks/pre-commit || exit 1"
  elif [ -f .pre-commit-config.yaml ]; then
    echo "add a local hook to .pre-commit-config.yaml whose entry is scripts/git-hooks/pre-commit"
  else
    echo "run: git config core.hooksPath scripts/git-hooks"
  fi
}


# ---------------------------------------------------------------- CONFIG ---
# Build outputs that are GENERATED and NOT committed, so a fresh clone does not
# have them.
#
# This is the nastiest gap on the whole list and it earns its own section. The
# app STARTS without them. It just renders wrong, so the person debugging reads
# it as a broken app and goes looking in the components, not at a build step
# they never ran. Nothing else here fails that quietly.
#
# One entry per line, four fields separated by | :
#
#   path | a string every real build contains | the command that builds it | the symptom when it is absent
#
# The second field is what separates "the file is there" from "the file is a
# build". A half-written or truncated output passes an existence test and fails
# everything after it.
#
# WHAT THIS DOES NOT CHECK. Whether the build is CURRENT. A file generated
# before the last change to its source is present, valid and wrong, and that is
# session-status.sh's job, because it is a thing that goes stale during a
# session rather than a thing a fresh clone is missing.
#
# An empty list is fine and normal. The section prints nothing at all when there
# is nothing in it, so a repo with no generated artefacts sees exactly what it
# saw before.
GENERATED=(
  # "src/styles/tokens.css|@theme|pnpm tokens:build|every colour, size and radius resolves to nothing and every page renders as unstyled text on white"
  # "src/generated/schema.ts|export type|pnpm codegen|every typed query is an implicit any and typecheck passes on nonsense"
)

ok()   { printf "  [ ok ] %s\n" "$1"; }
warn() { printf "  [note] %s\n" "$1"; }
bad()  { printf "  [MISS] %s\n" "$1"; }

echo ""
echo "Checking your machine"
echo ""

# ------------------------------------------------------------------ CORE ---
echo "Core, needed for everything:"

if command -v node >/dev/null 2>&1; then
  ver=$(node -v); major=${ver#v}; major=${major%%.*}
  if [ "${major:-0}" -ge 20 ]; then ok "node $ver"
  else warn "node $ver is old. Install 20 or newer: https://nodejs.org or use nvm"; fi
else
  bad "node missing. Install Node 20+: https://nodejs.org"
fi

# Only the tool this repo uses. Saying pnpm in an npm repo is a wrong answer.
if [ -n "$pm" ]; then
  if command -v "$pm" >/dev/null 2>&1; then
    ok "$pm $("$pm" --version 2>/dev/null | head -n 1)"
  else
    case "$pm" in
      pnpm|yarn) bad "$pm missing. Run: corepack enable (ships with Node), or npm i -g $pm" ;;
      bun)       bad "bun missing. Install it: https://bun.sh" ;;
      *)         bad "npm missing. It ships with Node: reinstall Node 20+ from https://nodejs.org" ;;
    esac
  fi
fi

command -v git >/dev/null 2>&1 \
  && ok "git $(git --version | awk '{print $3}')" \
  || bad "git missing. Install Xcode command line tools: xcode-select --install"

# The secrets gate wraps gitleaks. Without it the hook warns and passes, so a
# missing binary is easy to live with for months, and it is worth saying once.
if [ -f scripts/check-secrets.mjs ]; then
  command -v gitleaks >/dev/null 2>&1 \
    && ok "gitleaks $(gitleaks version 2>/dev/null)" \
    || warn "gitleaks missing, so commits are not scanned for credentials. Run: brew install gitleaks"
fi

if [ -d node_modules ]; then ok "dependencies installed"
elif [ -n "$pm" ]; then warn "node_modules missing. Run: $pm install"; fi

# Hooks. This one is easy to miss and it silently disables every gate, so it is
# worth checking explicitly rather than assuming `prepare` ran. Only when the
# hook is in this repo: nothing to wire otherwise.
if [ -f scripts/git-hooks/pre-commit ]; then
  if how="$(hook_wired)"; then
    # A hook chained from Husky or another hooks folder is wired. Calling it
    # unwired, and offering a fix that repoints core.hooksPath, would have
    # disabled the repo's own hooks.
    ok "git hooks wired ($how)"
    # Old installs chained the hook with `sh`. The hook is bash, and where sh is
    # dash (Debian, Ubuntu, most CI) that line fails every commit.
    case "$how" in
      "chained from "*)
        if grep -q 'sh scripts/git-hooks/pre-commit' "${how#chained from }" 2>/dev/null \
          && ! grep -q 'bash scripts/git-hooks/pre-commit' "${how#chained from }" 2>/dev/null; then
          warn "${how#chained from } runs the hook with sh, which breaks where sh is dash (Debian, Ubuntu, CI). Change it to: bash scripts/git-hooks/pre-commit || exit 1"
        fi ;;
    esac
  else
    bad "git hooks NOT wired, so no checks run on commit. $(hook_fix)"
  fi
fi

# Installed under a path .gitignore hides, the install is never committed and
# the next clone has none of it. Names what is hidden and the line that fixes it.
if [ -d scripts ] || [ -f AGENTS.md ]; then
  hidden=""
  for f in scripts/ AGENTS.md scripts/git-hooks/pre-commit; do
    [ -e "$f" ] && git check-ignore -q "$f" 2>/dev/null && hidden="$hidden $f"
  done
  if [ -z "$hidden" ]; then ok "nothing the install added is hidden by .gitignore"
  else bad ".gitignore hides:$hidden. A commit leaves it out. Add a negation line for each to .gitignore (for example: !scripts/), then run: git check-ignore -v$hidden"; fi
fi

# ------------------------------------------------------------- GENERATED ---
# Silent when the CONFIG list above is empty. See rule 2: a section that does
# not apply to you should not read as something you are missing.
if [ "${#GENERATED[@]}" -gt 0 ]; then
  echo ""
  echo "Generated files. Not committed, so a fresh clone has to build them once:"

  for entry in "${GENERATED[@]}"; do
    IFS='|' read -r gen_path gen_marker gen_cmd gen_symptom <<EOF
$entry
EOF
    if [ ! -f "$gen_path" ]; then
      bad "$gen_path missing. The app will still start, and $gen_symptom. Run: $gen_cmd"
    elif ! grep -q -- "$gen_marker" "$gen_path" 2>/dev/null; then
      # Present but not a build: an interrupted or failed generator leaves a
      # file behind, and every existence check in the world says it is fine.
      bad "$gen_path exists but does not contain '$gen_marker', so it is empty or truncated rather than built. Run: $gen_cmd"
    else
      ok "$gen_path built ($(wc -l < "$gen_path" | tr -d ' ') lines)"
    fi
  done
fi

# --------------------------------------------------------------- SECRETS ---
# Only when the repo documents its environment. No .env.example means there is
# nothing to compare against and nothing to copy, so this says nothing.
if [ -f .env.example ]; then
  echo ""
  echo "Environment, needed to boot the app:"

  envfile=""
  [ -f .env.local ] && envfile=.env.local
  [ -z "$envfile" ] && [ -f .env ] && envfile=.env
  if [ -n "$envfile" ]; then
    missing=""
    while IFS= read -r line; do
      case "$line" in ''|\#*) continue;; esac
      name="${line%%=*}"
      grep -q "^${name}=" "$envfile" 2>/dev/null || missing="${missing} ${name}"
    done < .env.example
    if [ -z "$missing" ]; then ok "$envfile has every variable from .env.example"
    else bad "$envfile is missing:${missing}. Ask the team, or see .env.example for where each one comes from"; fi
  else
    bad ".env.local missing. Run: cp .env.example .env.local, then fill it in"
  fi
fi

# -------------------------------------------------------------- OPTIONAL ---
echo ""
echo "Optional, only if you work on these:"

command -v docker >/dev/null 2>&1 \
  && ok "docker present (local database)" \
  || warn "docker not found. Only needed to run the database locally"

command -v gh >/dev/null 2>&1 \
  && ok "gh present (pull requests from the terminal)" \
  || warn "gh not found. Only needed for PR commands. brew install gh"

if [ -f package.json ] && grep -q '"playwright"' package.json 2>/dev/null; then
  [ -d ~/Library/Caches/ms-playwright ] || [ -d ~/.cache/ms-playwright ] \
    && ok "playwright browsers installed" \
    || warn "playwright browsers not installed. Run: $(case "$pm" in pnpm) echo 'pnpm exec' ;; yarn) echo yarn ;; bun) echo bunx ;; *) echo npx ;; esac) playwright install"
fi

# ------------------------------------------------------------------ HINT ---
echo ""
if has_script dev; then
  echo "If everything above is [ ok ] or [note], run: $(run_cmd dev)"
else
  echo "If everything above is [ ok ] or [note], you are set."
fi
echo "For design work you only need the Core section."
echo ""

exit 0

How to tell it works: Run it on a fresh clone. Every line is a pass, or names the one thing to fix.

Included in