build-bundle.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env bash
  2. #
  3. # Build a self-contained CodeGraph bundle: an official Node runtime + the
  4. # compiled app + its production deps, so CodeGraph runs with NO system Node and
  5. # NO native build — node:sqlite is built into the bundled Node. One archive per
  6. # platform.
  7. #
  8. # Because dropping better-sqlite3 left zero native addons, the recipe is pure
  9. # file-packaging (download the target's Node, copy the app, archive) — so any
  10. # platform's bundle can be built on any OS. No cross-compile, no native runners.
  11. #
  12. # Usage:
  13. # scripts/build-bundle.sh <target> [node-version]
  14. # target: darwin-arm64 | darwin-x64 | linux-x64 | linux-arm64
  15. # | win32-x64 | win32-arm64
  16. # node-version: e.g. v24.16.0 (default below; pin for reproducible builds)
  17. #
  18. # Output:
  19. # unix: release/codegraph-<target>.tar.gz (launcher: bin/codegraph)
  20. # windows: release/codegraph-<target>.zip (launcher: bin/codegraph.cmd)
  21. set -euo pipefail
  22. TARGET="${1:?usage: build-bundle.sh <target> [node-version]}"
  23. NODE_VERSION="${2:-v24.16.0}"
  24. ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  25. OUT="$ROOT/release"
  26. WORK="$(mktemp -d)"
  27. trap 'rm -rf "$WORK"' EXIT
  28. ARCH="${TARGET##*-}" # x64 | arm64
  29. OSFAM="${TARGET%-*}" # darwin | linux | win32
  30. echo "[bundle] target=${TARGET} node=${NODE_VERSION}"
  31. # 1. Download + extract the official Node runtime for the target platform.
  32. if [ "$OSFAM" = "win32" ]; then
  33. NODE_DIST="node-${NODE_VERSION}-win-${ARCH}"
  34. NODE_URL="https://nodejs.org/dist/${NODE_VERSION}/${NODE_DIST}.zip"
  35. echo "[bundle] downloading ${NODE_URL}"
  36. curl -fsSL "$NODE_URL" -o "$WORK/node.zip"
  37. if command -v unzip >/dev/null 2>&1; then
  38. unzip -q "$WORK/node.zip" -d "$WORK"
  39. else
  40. tar -xf "$WORK/node.zip" -C "$WORK" # bsdtar can read zip
  41. fi
  42. NODE_BIN="$WORK/${NODE_DIST}/node.exe"
  43. else
  44. NODE_DIST="node-${NODE_VERSION}-${TARGET}"
  45. NODE_URL="https://nodejs.org/dist/${NODE_VERSION}/${NODE_DIST}.tar.gz"
  46. echo "[bundle] downloading ${NODE_URL}"
  47. curl -fsSL "$NODE_URL" -o "$WORK/node.tar.gz"
  48. tar -xzf "$WORK/node.tar.gz" -C "$WORK"
  49. NODE_BIN="$WORK/${NODE_DIST}/bin/node"
  50. fi
  51. [ -f "$NODE_BIN" ] || { echo "[bundle] error: node binary not found ($NODE_BIN)" >&2; exit 1; }
  52. # 2. Build the app (compiled JS + copied wasm/schema assets).
  53. echo "[bundle] building app"
  54. ( cd "$ROOT" && npm run build >/dev/null )
  55. # 3. Stage: app + production-only deps (pure JS/wasm → portable across platforms).
  56. STAGE="$WORK/codegraph-${TARGET}"
  57. mkdir -p "$STAGE/lib" "$STAGE/bin"
  58. cp -R "$ROOT/dist" "$STAGE/lib/dist"
  59. cp "$ROOT/package.json" "$ROOT/package-lock.json" "$STAGE/lib/"
  60. echo "[bundle] installing production dependencies"
  61. ( cd "$STAGE/lib" && npm ci --omit=dev --ignore-scripts >/dev/null 2>&1 )
  62. rm -f "$STAGE/lib/package-lock.json"
  63. # 4. Vendored Node + launcher (the launcher uses the bundled Node by relative
  64. # path, so no system Node is ever needed).
  65. if [ "$OSFAM" = "win32" ]; then
  66. cp "$NODE_BIN" "$STAGE/node.exe"
  67. printf '@"%%~dp0..\\node.exe" "%%~dp0..\\lib\\dist\\bin\\codegraph.js" %%*\r\n' \
  68. > "$STAGE/bin/codegraph.cmd"
  69. else
  70. cp "$NODE_BIN" "$STAGE/node"
  71. cat > "$STAGE/bin/codegraph" <<'LAUNCH'
  72. #!/bin/sh
  73. # Resolve symlinks (e.g. the ~/.local/bin/codegraph link install.sh creates) so
  74. # we find the real bundle dir, not the symlink's location.
  75. SELF="$0"
  76. while [ -L "$SELF" ]; do
  77. target="$(readlink "$SELF")"
  78. case "$target" in
  79. /*) SELF="$target" ;;
  80. *) SELF="$(dirname "$SELF")/$target" ;;
  81. esac
  82. done
  83. DIR="$(cd "$(dirname "$SELF")/.." && pwd)"
  84. exec "$DIR/node" "$DIR/lib/dist/bin/codegraph.js" "$@"
  85. LAUNCH
  86. chmod +x "$STAGE/bin/codegraph"
  87. fi
  88. # 5. Archive (.zip for Windows, .tar.gz otherwise).
  89. mkdir -p "$OUT"
  90. if [ "$OSFAM" = "win32" ]; then
  91. ARCHIVE="$OUT/codegraph-${TARGET}.zip"
  92. rm -f "$ARCHIVE"
  93. ( cd "$WORK" && zip -rqX "$ARCHIVE" "codegraph-${TARGET}" )
  94. else
  95. ARCHIVE="$OUT/codegraph-${TARGET}.tar.gz"
  96. # --no-xattrs: don't embed macOS xattrs that make GNU tar warn on Linux.
  97. tar --no-xattrs -czf "$ARCHIVE" -C "$WORK" "codegraph-${TARGET}"
  98. fi
  99. echo "[bundle] wrote ${ARCHIVE} ($(du -h "$ARCHIVE" | cut -f1))"