Skip to content

Instantly share code, notes, and snippets.

@styledev
Last active August 26, 2026 15:27
Show Gist options
  • Select an option

  • Save styledev/10ef04dc4402a75f2ab4d9751e0a419f to your computer and use it in GitHub Desktop.

Select an option

Save styledev/10ef04dc4402a75f2ab4d9751e0a419f to your computer and use it in GitHub Desktop.
Universal Run Command for Zshrc
# Universal project run function
# Checks for a Runfile first, then auto-detects by project type.
run() {
# 1. Explicit Runfile
if [[ -f "Runfile" ]]; then
echo "▶ Running via Runfile"
local cmd
cmd=$(cat Runfile)
eval "$cmd"
return
fi
# 2. run.sh / start.sh
if [[ -x "run.sh" ]]; then
echo "▶ Running ./run.sh"
./run.sh
return
fi
if [[ -x "start.sh" ]]; then
echo "▶ Running ./start.sh"
./start.sh
return
fi
# 3. Makefile — prefer 'run' target, fall back to 'start'
if [[ -f "Makefile" ]]; then
if grep -qE '^run:' Makefile; then
echo "▶ Running make run"
make run
return
fi
if grep -qE '^start:' Makefile; then
echo "▶ Running make start"
make start
return
fi
fi
# 4. package.json — detect yarn vs pnpm vs npm, prefer 'dev' script, fall back to 'start'
if [[ -f "package.json" ]]; then
local pkg_cmd="npm"
[[ -f "yarn.lock" ]] && pkg_cmd="yarn"
[[ -f "pnpm-lock.yaml" ]] && pkg_cmd="pnpm"
if node -e "process.exit(require('./package.json').scripts?.dev ? 0 : 1)" 2>/dev/null; then
echo "▶ Running $pkg_cmd run dev"
$pkg_cmd run dev
return
fi
if node -e "process.exit(require('./package.json').scripts?.start ? 0 : 1)" 2>/dev/null; then
echo "▶ Running $pkg_cmd start"
$pkg_cmd start
return
fi
fi
# Nothing found
echo "✗ No run target found. Checked:"
echo " Runfile · run.sh · start.sh · Makefile (run/start) · package.json via yarn/pnpm/npm (dev/start)"
}
# Regenerate local mkcert cert for all ~/Sites folders.
ssl-sites() {
local sites_dir="$HOME/Sites"
local cert_dir="$HOME/dev-cert"
local cert_file="$cert_dir/local-sites.test.pem"
local key_file="$cert_dir/local-sites.test-key.pem"
if ! command -v mkcert >/dev/null 2>&1; then
echo "✗ mkcert is not installed or not on PATH."
return 1
fi
mkdir -p "$cert_dir" || return 1
local -a hosts
local dir base
for dir in "$sites_dir"/*(/N); do
base="${dir:t}"
if [[ "$base" =~ '^[A-Za-z0-9][A-Za-z0-9-]*$' ]]; then
hosts+=("${base}.test")
else
echo "Skipping folder (invalid hostname label): $base"
fi
done
# Preserve any existing .test SANs already present in the current shared cert.
if [[ -f "$cert_file" ]]; then
local -a existing_hosts
existing_hosts=("${(@f)$(openssl x509 -in "$cert_file" -noout -ext subjectAltName 2>/dev/null | tr ',' '\n' | sed -n 's/.*DNS:\([^[:space:]]*\).*/\1/p')}")
local host
for host in "${existing_hosts[@]}"; do
[[ "$host" == *.test ]] && hosts+=("$host")
done
fi
typeset -U hosts
if (( ${#hosts[@]} == 0 )); then
echo "✗ No valid site folders found in $sites_dir"
return 1
fi
echo "▶ Regenerating local-sites cert for:"
printf ' - %s\n' "${hosts[@]}"
mkcert -cert-file "$cert_file" -key-file "$key_file" "${hosts[@]}" || return 1
echo "▶ Restarting Apache (httpd)"
brew services restart httpd || return 1
echo "✓ Updated:"
echo " $cert_file"
echo " $key_file"
echo "✓ Apache restarted"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment