build: kill previous watch instance when starting pnpm watch

Track the tsdown watcher pid in .tmp/watch.pid; a new watch kills the
previous instance first and the pid file is cleaned up on exit.
This commit is contained in:
bowenliang123 committed 2026-08-29 00:46:32 +08:00
1 parent 48e5efa7b3
commit de4d6f327a
2 files changed
+37 -1

No files matched your search

+1 -1
View File
@@ -34,7 +34,7 @@
"build": "tsdown",
"lint": "oxlint",
"lint:fix": "oxlint --fix",
"watch": "tsdown --watch",
"watch": "bash scripts/watch.sh",
"register": "bash scripts/register.sh",
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.tests.json",
"test": "pnpm run typecheck && vitest run --coverage",
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env bash
# Starts tsdown --watch, killing the previous watch instance (pid tracked in .tmp/watch.pid) first.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
PID_FILE="$ROOT/.tmp/watch.pid"
mkdir -p "$ROOT/.tmp"
if [[ -f "$PID_FILE" ]]; then
old_pid="$(cat "$PID_FILE" 2>/dev/null || true)"
if [[ "$old_pid" =~ ^[0-9]+$ ]] && kill -0 "$old_pid" 2>/dev/null; then
echo "killing previous watch (pid $old_pid)"
kill "$old_pid" 2>/dev/null || true
for _ in {1..50}; do
kill -0 "$old_pid" 2>/dev/null || break
sleep 0.1
done
fi
rm -f "$PID_FILE"
fi
tsdown --watch &
child=$!
echo "$child" > "$PID_FILE"
cleanup() {
kill "$child" 2>/dev/null || true
if [[ "$(cat "$PID_FILE" 2>/dev/null)" == "$child" ]]; then
rm -f "$PID_FILE"
fi
}
trap cleanup EXIT INT TERM
wait "$child"