#!/bin/bash # Builds a self-contained macOS installer: dist/opencode-browser-.pkg # # packaging/build-pkg.sh [--arch universal|arm64|x64] # # The package carries its own Node.js (no Node needed on the target Mac), the # MCP server and native host bundled into single files, and the extension. set -euo pipefail ARCH="universal" while [ $# -gt 0 ]; do case "$1" in --arch) shift; ARCH="$1" ;; *) echo "Unknown option: $1"; exit 1 ;; esac shift done NODE_VERSION="22.13.1" ROOT="$(cd "$(dirname "$0")/.." && pwd)" PKG="$ROOT/packaging" VERSION="$(node -p "require('$ROOT/extension/manifest.json').version")" BUILD="$ROOT/build/pkg" CACHE="$ROOT/build/cache" APP="$BUILD/root/Library/Application Support/opencode-browser" rm -rf "$BUILD" mkdir -p "$APP/bin" "$APP/host" "$CACHE" "$ROOT/dist" # --- Node.js runtime (official binaries, checksum-verified) --- fetch_node() { # arch -> path of the node binary local a="$1" name="node-v$NODE_VERSION-darwin-$1" if [ ! -x "$CACHE/$name/bin/node" ]; then curl -fsSL -o "$CACHE/$name.tar.gz" "https://nodejs.org/dist/v$NODE_VERSION/$name.tar.gz" curl -fsSL -o "$CACHE/SHASUMS256.txt" "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt" (cd "$CACHE" && grep " $name.tar.gz\$" SHASUMS256.txt | shasum -a 256 -c - >/dev/null) tar -xzf "$CACHE/$name.tar.gz" -C "$CACHE" "$name/bin/node" fi echo "$CACHE/$name/bin/node" } echo "Node.js $NODE_VERSION ($ARCH)" if [ "$ARCH" = "universal" ]; then lipo -create "$(fetch_node arm64)" "$(fetch_node x64)" -output "$APP/bin/node" # lipo invalidates the vendor signatures; Apple silicon needs a valid one. codesign --force --sign - "$APP/bin/node" else cp "$(fetch_node "$ARCH")" "$APP/bin/node" fi # --- host: bundle the MCP server and native host into single files --- echo "Bundling the host" (cd "$ROOT/host" && npm install --silent --no-audit --no-fund) ESBUILD="$ROOT/host/node_modules/.bin/esbuild" common=(--bundle --platform=node --target=node22 --format=esm --log-level=warning "--banner:js=import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);") "$ESBUILD" "$ROOT/host/mcp-server.js" "${common[@]}" --outfile="$APP/host/mcp-server.mjs" "$ESBUILD" "$ROOT/host/native-host.js" "${common[@]}" --outfile="$APP/host/native-host.mjs" cp "$ROOT/host/instructions.md" "$APP/host/" # --- extension, scripts, presets --- cp -R "$ROOT/extension" "$APP/extension" cp "$PKG/native-host" "$PKG/setup" "$PKG/uninstall" "$PKG/user-setup.mjs" "$PKG/llama-provider.json" "$APP/" cp "$ROOT/LICENSE" "$APP/" find "$BUILD/root" -name .DS_Store -delete xattr -cr "$BUILD/root" # --- packages --- echo "Packaging" export COPYFILE_DISABLE=1 # no ._AppleDouble files for extended attributes pkgbuild --quiet --root "$BUILD/root" --scripts "$PKG/scripts-main" \ --identifier com.opencode.browser --version "$VERSION" --install-location / \ "$BUILD/main.pkg" pkgbuild --quiet --nopayload --scripts "$PKG/scripts-llama" \ --identifier com.opencode.browser.llama --version "$VERSION" \ "$BUILD/llama.pkg" HOST_ARCHS="arm64,x86_64" [ "$ARCH" = "arm64" ] && HOST_ARCHS="arm64" [ "$ARCH" = "x64" ] && HOST_ARCHS="x86_64" sed -e "s/@VERSION@/$VERSION/g" -e "s/@ARCHS@/$HOST_ARCHS/g" "$PKG/distribution.xml" > "$BUILD/distribution.xml" SUFFIX="" [ "$ARCH" != "universal" ] && SUFFIX="-$ARCH" OUT="$ROOT/dist/opencode-browser-$VERSION$SUFFIX.pkg" productbuild --quiet --distribution "$BUILD/distribution.xml" --package-path "$BUILD" \ --resources "$PKG/resources" "$OUT" echo "Built $OUT ($(du -h "$OUT" | cut -f1))"