build-bundle.sh 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. DIR="$(cd "$(dirname "$0")/.." && pwd)"
  74. exec "$DIR/node" "$DIR/lib/dist/bin/codegraph.js" "$@"
  75. LAUNCH
  76. chmod +x "$STAGE/bin/codegraph"
  77. fi
  78. # 5. Archive (.zip for Windows, .tar.gz otherwise).
  79. mkdir -p "$OUT"
  80. if [ "$OSFAM" = "win32" ]; then
  81. ARCHIVE="$OUT/codegraph-${TARGET}.zip"
  82. rm -f "$ARCHIVE"
  83. ( cd "$WORK" && zip -rqX "$ARCHIVE" "codegraph-${TARGET}" )
  84. else
  85. ARCHIVE="$OUT/codegraph-${TARGET}.tar.gz"
  86. # --no-xattrs: don't embed macOS xattrs that make GNU tar warn on Linux.
  87. tar --no-xattrs -czf "$ARCHIVE" -C "$WORK" "codegraph-${TARGET}"
  88. fi
  89. echo "[bundle] wrote ${ARCHIVE} ($(du -h "$ARCHIVE" | cut -f1))"