Skip to content

Instantly share code, notes, and snippets.

@amit-gshe
Created June 8, 2026 06:49
Show Gist options
  • Select an option

  • Save amit-gshe/8530fcc9fb876b7873356eedef865da7 to your computer and use it in GitHub Desktop.

Select an option

Save amit-gshe/8530fcc9fb876b7873356eedef865da7 to your computer and use it in GitHub Desktop.
rebuild-pi-lens
#!/usr/bin/env bash
# Rebuild pi-lens local mitigation script
# Purpose: pi-lens is distributed as source (main: index.ts), jiti transpiles 215 ts files
# on every startup, slowing pi launch by ~3.5s. This script precompiles to dist/
# and redirects the extension entry point.
#
# Usage: rebuild-pi-lens.sh # Rebuild after detection (idempotent)
# rebuild-pi-lens.sh --force # Force rebuild, ignore existing dist
# rebuild-pi-lens.sh --revert # Revert to upstream source mode (delete dist, restore index.ts entry)
#
# Will be overwritten after upgrading pi-lens, needs to be re-run.
set -euo pipefail
PILENS="${PI_LENS_DIR:-$HOME/.pi/agent/npm/node_modules/pi-lens}"
PKG="$PILENS/package.json"
TSCONFIG_BUILD="$PILENS/tsconfig.build.json"
DIST="$PILENS/dist"
[ -f "$PKG" ] || { echo "pi-lens not found at $PILENS" >&2; exit 1; }
mode="${1:-build}"
revert() {
echo "[revert] Restoring pi.extensions → ./index.ts, deleting dist/"
python3 - "$PKG" <<'PY'
import json, sys
p = sys.argv[1]
d = json.load(open(p))
d.setdefault("pi", {})["extensions"] = ["./index.ts"]
d["main"] = "index.ts"
json.dump(d, open(p, "w"), indent="\t")
open(p, "a").write("\n")
PY
rm -rf "$DIST" "$TSCONFIG_BUILD"
echo "[revert] done"
}
build() {
# Check if already built and dist is newer than index.ts → skip
if [ "${1:-}" != "--force" ] && [ -f "$DIST/index.js" ] && [ "$DIST/index.js" -nt "$PILENS/index.ts" ]; then
cur=$(python3 -c "import json;print(json.load(open('$PKG')).get('pi',{}).get('extensions',[None])[0])")
if [ "$cur" = "./dist/index.js" ]; then
echo "[build] Already built and dist newer than index.ts, skipping (use --force to override)"
return
fi
fi
echo "[build] Writing tsconfig.build.json"
cat > "$TSCONFIG_BUILD" <<'JSON'
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"outDir": "./dist",
"declaration": false,
"sourceMap": false,
"noUnusedLocals": false,
"noUnusedParameters": false
},
"include": ["index.ts", "i18n.ts", "clients/**/*.ts", "commands/**/*.ts", "tools/**/*.ts"]
}
JSON
echo "[build] tsc --noCheck (ignore type errors, transpile only)"
rm -rf "$DIST"
# tsc will error on missing @types/node but still emit; || true swallows non-zero exit
( cd "$PILENS" && tsc -p tsconfig.build.json --noCheck 2>&1 | grep -v "Cannot find type definition\|allowImportingTsExtensions" || true )
[ -f "$DIST/index.js" ] || { echo "[build] Compilation failed: dist/index.js not generated" >&2; exit 1; }
n=$(find "$DIST" -name "*.js" | wc -l)
echo "[build] Generated $n .js files, $(du -sh "$DIST" | cut -f1)"
echo "[build] Patching package.json: main + pi.extensions → ./dist/index.js"
python3 - "$PKG" <<'PY'
import json, sys
p = sys.argv[1]
d = json.load(open(p))
d["main"] = "./dist/index.js"
d.setdefault("pi", {})["extensions"] = ["./dist/index.js"]
json.dump(d, open(p, "w"), indent="\t")
open(p, "a").write("\n")
PY
echo "[build] done. Test with 'time pi --help' should be ~1.5s"
}
case "$mode" in
--revert|revert) revert ;;
--force|build) build "$mode" ;;
*) echo "Usage: $0 [--force|--revert]" >&2; exit 1 ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment