Skip to content

Commit 7b6010f

Browse files
authored
chore(phrocs): Auto-update phrocs binary on pnpm install (#3458)
1 parent 7f14e20 commit 7b6010f

3 files changed

Lines changed: 57 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ cp .env.example .env
3434

3535
### Running in Development
3636

37-
By default, `pnpm dev` uses phrocs (our custom process runner) to run the agent and code app in parallel. phrocs auto-installs on first run and reads the `mprocs.yaml` config file. The binary is downloaded to `bin/phrocs` and is git-ignored.
37+
By default, `pnpm dev` uses phrocs (our custom process runner) to run the agent and code app in parallel. phrocs auto-installs and keeps itself up to date: on every `pnpm install` the local binary is checked against the latest `phrocs-latest` release checksums and re-downloaded if it differs (skipped when offline or in CI). `pnpm dev` only downloads it if it is missing entirely. It reads the `mprocs.yaml` config file. The binary lives at `bin/phrocs` and is git-ignored.
3838

3939
```bash
4040
# Run both agent (watch mode) and code app in parallel
@@ -44,8 +44,6 @@ pnpm dev
4444
pnpm dev:agent # Run agent in watch mode
4545
pnpm dev:code # Run code app
4646

47-
# Manually update phrocs to the latest version
48-
pnpm update:phrocs
4947

5048
# Use mprocs instead of phrocs
5149
pnpm dev:mprocs

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"packageManager": "pnpm@10.23.0+sha512.21c4e5698002ade97e4efe8b8b4a89a8de3c85a37919f957e7a0f30f38fbc5bbdd05980ffe29179b2fb6e6e691242e098d945d1601772cad0fef5fb6411e2a4b",
1010
"scripts": {
1111
"preinstall": "node scripts/enforce-pnpm.mjs",
12+
"postinstall": "bash scripts/ensure-phrocs.sh --update",
1213
"setup": "bash apps/code/bin/setup",
1314
"prepare": "husky",
1415
"dev": "pnpm build:deps && bash scripts/ensure-phrocs.sh && bin/phrocs --config mprocs.yaml",
@@ -34,8 +35,7 @@
3435
"format": "biome format --write",
3536
"clean": "pnpm -r clean",
3637
"knip": "knip",
37-
"skills:pull": "node scripts/pull-skills.mjs",
38-
"update:phrocs": "rm -f bin/phrocs && bash scripts/ensure-phrocs.sh"
38+
"skills:pull": "node scripts/pull-skills.mjs"
3939
},
4040
"keywords": [
4141
"posthog",

scripts/ensure-phrocs.sh

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
set -euo pipefail
33

44
PHROCS_BIN="bin/phrocs"
5+
RELEASE_URL="https://github.com/PostHog/posthog/releases/download/phrocs-latest"
56

6-
if [ -x "$PHROCS_BIN" ]; then
7+
MODE="ensure"
8+
[ "${1:-}" = "--update" ] && MODE="update"
9+
10+
if [ "$MODE" = "update" ] && [ -n "${CI:-}" ]; then
711
exit 0
812
fi
913

10-
echo "phrocs not found, downloading..."
14+
if [ "$MODE" = "ensure" ] && [ -x "$PHROCS_BIN" ]; then
15+
exit 0
16+
fi
1117

1218
ARCH=$(uname -m)
1319
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
@@ -24,10 +30,51 @@ case "$OS" in
2430
esac
2531

2632
BINARY="phrocs-${OS}-${ARCH}"
27-
URL="https://github.com/PostHog/posthog/releases/download/phrocs-latest/${BINARY}"
2833

29-
mkdir -p bin
30-
curl -fSL "$URL" -o "$PHROCS_BIN"
31-
chmod +x "$PHROCS_BIN"
34+
sha256() {
35+
if command -v sha256sum >/dev/null 2>&1; then
36+
sha256sum "$1" | awk '{print $1}'
37+
else
38+
shasum -a 256 "$1" | awk '{print $1}'
39+
fi
40+
}
41+
42+
bail() {
43+
if [ -x "$PHROCS_BIN" ]; then
44+
echo "phrocs: $1, keeping existing binary" >&2
45+
exit 0
46+
fi
47+
echo "phrocs: $1 and no local binary exists" >&2
48+
if [ "$MODE" = "update" ]; then
49+
echo "phrocs: will retry on next pnpm install or pnpm dev" >&2
50+
exit 0
51+
fi
52+
exit 1
53+
}
54+
55+
TMP_DIR=$(mktemp -d)
56+
trap 'rm -rf "$TMP_DIR"' EXIT
57+
58+
curl -fsSL --max-time 10 "${RELEASE_URL}/checksums.txt" -o "$TMP_DIR/checksums.txt" \
59+
|| bail "could not fetch checksums"
60+
61+
EXPECTED=$(awk -v bin="$BINARY" '{name = $2; sub(/^\*/, "", name); if (name == bin) {print $1; exit}}' "$TMP_DIR/checksums.txt")
62+
[ -n "$EXPECTED" ] || bail "no checksum for ${BINARY} in latest release"
3263

33-
echo "phrocs installed to $PHROCS_BIN"
64+
if [ -x "$PHROCS_BIN" ] && [ "$(sha256 "$PHROCS_BIN")" = "$EXPECTED" ]; then
65+
exit 0
66+
fi
67+
68+
echo "phrocs: downloading latest release..."
69+
curl -fsSL --max-time 120 "${RELEASE_URL}/${BINARY}" -o "$TMP_DIR/phrocs" \
70+
|| bail "download failed"
71+
72+
ACTUAL=$(sha256 "$TMP_DIR/phrocs")
73+
if [ "$ACTUAL" != "$EXPECTED" ]; then
74+
bail "checksum mismatch on download (expected ${EXPECTED}, got ${ACTUAL})"
75+
fi
76+
77+
mkdir -p bin
78+
chmod +x "$TMP_DIR/phrocs"
79+
mv "$TMP_DIR/phrocs" "$PHROCS_BIN"
80+
echo "phrocs: updated to latest (${PHROCS_BIN})"

0 commit comments

Comments
 (0)