package.json Scripts Organization
Two techniques for keeping large scripts sections readable and maintainable.
Technique 1: Comment Separator Keys
Add visual section dividers using unused JSON keys:
{
"scripts": {
"// ── Core ─────────────────────────────────────────": "",
"dev": "next dev",
"build": "next build",
"// ── Testing ─────────────────────────────────────": "",
"test": "jest",
"test:e2e": "playwright test"
}
}
Format: "// ── Section Name ──────..." with ─ padding to ~50 chars, value "".
Technique 2: External Shell Scripts for Multi-Process Commands
When a command starts 2+ background processes, extract to scripts/*.sh.
Template available at scripts/multi-process-dev.sh.template. Key pattern:
#!/bin/bash
set -e
cleanup() {
kill $PID_1 $PID_2 2>/dev/null
wait $PID_1 $PID_2 2>/dev/null
}
trap cleanup EXIT INT TERM
if [ "$MODE" = "local" ]; then
backend-server &
PID_1=$!
sleep 3
fi
pnpm dev &
PID_2=$!
wait
Call from package.json: "dev:full": "MODE=local ./scripts/dev-full.sh"
Make executable: chmod +x scripts/*.sh
Multi-Environment Pattern
For apps with local/preview/production API targets:
{
"// ── Dev with API (3 environments) ───────────────": "",
"dev:full": "API_MODE=local ./scripts/dev-full.sh",
"dev:full:preview": "API_MODE=preview pnpm dev",
"dev:full:prod": "API_MODE=production pnpm dev"
}
Detailed Patterns
See references/patterns.md for:
- •Full list of suggested section names
- •Namespace prefixing for monorepo sub-packages
- •Internal/private script conventions (
_prefix) - •Complete multi-process shell script template with explanations