#!/usr/bin/env bash
# Sync the currently-running Keet AppImage into this repo as a new commit.
#
# Default: auto-detects the running Keet's AppImage mount, infers version
# from its package.json, runs the audit, replaces the tracked tree with
# the new pretty/ snapshot, and commits + tags.
#
# Usage:
# tools/sync.sh # auto-detect everything
# tools/sync.sh --src <mount> # explicit mount path
# tools/sync.sh --version <v> # override version tag
# tools/sync.sh --no-tag # commit but don't tag
# tools/sync.sh --dry-run # show what would happen, no writes
#
# Pre-reqs: a running Keet AppImage so we can find its mount, OR pass --src.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
AUDIT_TOOL="$REPO_ROOT/../keet-cli/tools/keet-audit.cjs"
SRC=""
VERSION=""
TAG=1
DRY_RUN=0
while [ $# -gt 0 ]; do
case "$1" in
--src) SRC="$2"; shift 2 ;;
--version) VERSION="$2"; shift 2 ;;
--no-tag) TAG=0; shift ;;
--dry-run) DRY_RUN=1; shift ;;
-h|--help) sed -n '2,15p' "$0"; exit 0 ;;
*) echo "unknown arg: $1" >&2; exit 1 ;;
esac
done
# Auto-detect mount from running Keet if --src not given
if [ -z "$SRC" ]; then
SRC=$(pgrep -af '/tmp/\.mount_Keet\..*/Keet' \
| grep -oE '/tmp/\.mount_Keet\.[^ /]+' \
| head -1)
if [ -z "$SRC" ]; then
echo "no running Keet AppImage found; pass --src <mount>" >&2
exit 1
fi
SRC="$SRC/resources/app/node_modules"
fi
if [ ! -d "$SRC/@holepunchto" ]; then
echo "src does not look like a Keet node_modules dir: $SRC" >&2
exit 1
fi
# Read version from app's own package.json
PKG_JSON="$(dirname "$SRC")/package.json"
if [ -z "$VERSION" ]; then
VERSION=$(grep -oE '"version"[[:space:]]*:[[:space:]]*"[^"]+"' "$PKG_JSON" \
| head -1 | grep -oE '"[^"]+"$' | tr -d '"')
[ -z "$VERSION" ] && { echo "could not infer version from $PKG_JSON" >&2; exit 1; }
fi
AUDIT_OUT="$REPO_ROOT/../keet-audit-$VERSION"
echo "src: $SRC"
echo "version: $VERSION"
echo "audit out: $AUDIT_OUT"
echo "tag: $([ $TAG -eq 1 ] && echo "v$VERSION" || echo "(skipped)")"
echo "dry-run: $([ $DRY_RUN -eq 1 ] && echo yes || echo no)"
echo
# Refuse if tag already exists
if [ $TAG -eq 1 ] && git -C "$REPO_ROOT" rev-parse "v$VERSION" >/dev/null 2>&1; then
echo "tag v$VERSION already exists in this repo — nothing to do." >&2
exit 0
fi
# Refuse if working tree dirty
if [ -n "$(git -C "$REPO_ROOT" status --porcelain)" ]; then
echo "working tree is dirty; commit or stash first" >&2
exit 1
fi
if [ $DRY_RUN -eq 1 ]; then
echo "(dry-run) would run: node $AUDIT_TOOL --src $SRC --out $AUDIT_OUT"
echo "(dry-run) would replace tracked tree from $AUDIT_OUT/pretty/"
echo "(dry-run) would commit + tag v$VERSION"
exit 0
fi
# 1. Run audit (idempotent — overwrites OUT)
if [ -d "$AUDIT_OUT" ]; then
echo "[1/4] audit dir already exists, reusing: $AUDIT_OUT"
else
echo "[1/4] running audit…"
node "$AUDIT_TOOL" --src "$SRC" --out "$AUDIT_OUT" >/dev/null
fi
# 2. Wipe tracked content (keep .git, keep tools/, keep README files at root)
echo "[2/4] wiping tracked tree…"
cd "$REPO_ROOT"
git ls-files -z \
| grep -zvE '^(tools/|README\.md$|\.gitignore$)' \
| xargs -0 -r rm -f
find . -type d -empty -not -path './.git*' -not -path './tools*' -delete
# 3. Copy fresh snapshot in
echo "[3/4] copying $VERSION snapshot…"
cp -a "$AUDIT_OUT/pretty/." ./
cp "$AUDIT_OUT/MANIFEST.md" ./MANIFEST.md
# 4. Commit + tag
echo "[4/4] committing…"
git add -A
if git diff --cached --quiet; then
echo "no changes vs prior commit — nothing to do."
exit 0
fi
PREV_TAG=$(git tag --sort=-v:refname | head -1)
git commit -q -m "keet $VERSION — OTA update from ${PREV_TAG:-initial}
Captured: $(date -u +%Y-%m-%dT%H:%M:%SZ)
Source: $SRC
Run \`git diff $PREV_TAG v$VERSION\` for the full changelog.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
if [ $TAG -eq 1 ]; then
git tag "v$VERSION"
fi
echo
echo "done. summary:"
git log --oneline -3
echo
git diff --shortstat "${PREV_TAG:-$(git rev-list --max-parents=0 HEAD)}" HEAD