|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Sandbox npm Install Script |
| 5 | +# Installs node_modules on local ext4 filesystem and symlinks into the workspace. |
| 6 | +# This avoids native binary crashes (esbuild, lightningcss, rollup) on virtiofs. |
| 7 | + |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" |
| 10 | + |
| 11 | +# Local ext4 base directory where node_modules is installed to avoid virtiofs crashes. |
| 12 | +# Change this path if your sandbox uses a different local filesystem location. |
| 13 | +readonly DEPS_BASE="/home/agent/project-deps" |
| 14 | +WORKSPACE_CLIENT="" |
| 15 | +INSTALL_PLAYWRIGHT="false" |
| 16 | + |
| 17 | +usage() { |
| 18 | + cat <<EOF |
| 19 | +Usage: $(basename "$0") [options] |
| 20 | +
|
| 21 | +Options: |
| 22 | + --workspace <path> Client workspace containing package.json |
| 23 | + --playwright Install Playwright Chromium browser |
| 24 | + --help Show this help message |
| 25 | +
|
| 26 | +Examples: |
| 27 | + bash scripts/install.sh |
| 28 | + bash scripts/install.sh --workspace app/client --playwright |
| 29 | +EOF |
| 30 | +} |
| 31 | + |
| 32 | +while [[ $# -gt 0 ]]; do |
| 33 | + case "$1" in |
| 34 | + --workspace) |
| 35 | + if [[ -z "${2:-}" ]]; then |
| 36 | + echo "Error: --workspace requires a path argument" |
| 37 | + usage |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | + WORKSPACE_CLIENT="$2" |
| 41 | + shift 2 |
| 42 | + ;; |
| 43 | + --playwright) |
| 44 | + INSTALL_PLAYWRIGHT="true" |
| 45 | + shift |
| 46 | + ;; |
| 47 | + --help|-h) |
| 48 | + usage |
| 49 | + exit 0 |
| 50 | + ;; |
| 51 | + *) |
| 52 | + echo "Unknown option: $1" |
| 53 | + usage |
| 54 | + exit 1 |
| 55 | + ;; |
| 56 | + esac |
| 57 | +done |
| 58 | + |
| 59 | +if [[ -z "$WORKSPACE_CLIENT" ]]; then |
| 60 | + if [[ -f "$PWD/package.json" ]]; then |
| 61 | + WORKSPACE_CLIENT="$PWD" |
| 62 | + elif [[ -f "$REPO_ROOT/package.json" ]]; then |
| 63 | + WORKSPACE_CLIENT="$REPO_ROOT" |
| 64 | + fi |
| 65 | +fi |
| 66 | + |
| 67 | +WORKSPACE_CLIENT="$(cd "$WORKSPACE_CLIENT" 2>/dev/null && pwd || true)" |
| 68 | + |
| 69 | +if [[ -z "$WORKSPACE_CLIENT" || ! -f "$WORKSPACE_CLIENT/package.json" ]]; then |
| 70 | + echo "Could not find a valid workspace client path containing package.json." |
| 71 | + echo "Use --workspace <path> to specify it explicitly." |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +echo "=== Sandbox npm Install ===" |
| 76 | +echo "Workspace: $WORKSPACE_CLIENT" |
| 77 | + |
| 78 | +# Derive a unique subdirectory from the workspace path relative to the repo root. |
| 79 | +# e.g. /repo/apps/web -> apps-web, /repo -> <repo-basename> |
| 80 | +REL_PATH="${WORKSPACE_CLIENT#"$REPO_ROOT"}" |
| 81 | +REL_PATH="${REL_PATH#/}" |
| 82 | +if [[ -z "$REL_PATH" ]]; then |
| 83 | + REL_PATH="$(basename "$REPO_ROOT")" |
| 84 | +fi |
| 85 | +# Sanitize: replace path separators with hyphens |
| 86 | +DEPS_SUBDIR="${REL_PATH//\//-}" |
| 87 | +DEPS_DIR="${DEPS_BASE}/${DEPS_SUBDIR}" |
| 88 | + |
| 89 | +echo "Deps dir: $DEPS_DIR" |
| 90 | + |
| 91 | +# Step 1: Prepare local deps directory |
| 92 | +echo "→ Preparing $DEPS_DIR..." |
| 93 | +rm -rf "$DEPS_DIR" |
| 94 | +mkdir -p "$DEPS_DIR" |
| 95 | +cp "$WORKSPACE_CLIENT/package.json" "$DEPS_DIR/" |
| 96 | + |
| 97 | +# Copy .npmrc if present (needed for private registries / scoped packages) |
| 98 | +if [[ -f "$WORKSPACE_CLIENT/.npmrc" ]]; then |
| 99 | + cp "$WORKSPACE_CLIENT/.npmrc" "$DEPS_DIR/" |
| 100 | +fi |
| 101 | + |
| 102 | +if [[ -f "$WORKSPACE_CLIENT/package-lock.json" ]]; then |
| 103 | + cp "$WORKSPACE_CLIENT/package-lock.json" "$DEPS_DIR/" |
| 104 | + INSTALL_CMD=(npm ci) |
| 105 | +else |
| 106 | + echo "! package-lock.json not found; falling back to npm install" |
| 107 | + INSTALL_CMD=(npm install) |
| 108 | +fi |
| 109 | + |
| 110 | +# Step 2: Install on local ext4 |
| 111 | +echo "→ Running ${INSTALL_CMD[*]} on local ext4..." |
| 112 | +cd "$DEPS_DIR" && "${INSTALL_CMD[@]}" |
| 113 | + |
| 114 | +# Step 3: Symlink into workspace |
| 115 | +echo "→ Symlinking node_modules into workspace..." |
| 116 | +cd "$WORKSPACE_CLIENT" |
| 117 | +rm -rf node_modules |
| 118 | +ln -s "$DEPS_DIR/node_modules" node_modules |
| 119 | + |
| 120 | +has_dep() { |
| 121 | + local dep="$1" |
| 122 | + node -e " |
| 123 | + const pkg=require(process.argv[1]); |
| 124 | + const deps={...(pkg.dependencies||{}),...(pkg.devDependencies||{})}; |
| 125 | + process.exit(deps[process.argv[2]] ? 0 : 1); |
| 126 | + " "$WORKSPACE_CLIENT/package.json" "$dep" |
| 127 | +} |
| 128 | + |
| 129 | +verify_one() { |
| 130 | + local label="$1" |
| 131 | + shift |
| 132 | + if "$@" >/dev/null 2>&1; then |
| 133 | + echo " ✓ $label OK" |
| 134 | + return 0 |
| 135 | + fi |
| 136 | + |
| 137 | + echo " ✗ $label FAIL" |
| 138 | + return 1 |
| 139 | +} |
| 140 | + |
| 141 | +# Step 4: Verify native binaries when present in this project |
| 142 | +echo "→ Verifying native binaries..." |
| 143 | +FAIL=0 |
| 144 | + |
| 145 | +if has_dep esbuild; then |
| 146 | + verify_one "esbuild" node -e "require('esbuild').transform('const x: number = 1',{loader:'ts'}).catch(()=>process.exit(1))" || FAIL=1 |
| 147 | +fi |
| 148 | + |
| 149 | +if has_dep rollup; then |
| 150 | + verify_one "rollup" node -e "import('rollup').catch(()=>process.exit(1))" || FAIL=1 |
| 151 | +fi |
| 152 | + |
| 153 | +if has_dep lightningcss; then |
| 154 | + verify_one "lightningcss" node -e "try{require('lightningcss')}catch(_){process.exit(1)}" || FAIL=1 |
| 155 | +fi |
| 156 | + |
| 157 | +if has_dep vite; then |
| 158 | + verify_one "vite" node -e "import('vite').catch(()=>process.exit(1))" || FAIL=1 |
| 159 | +fi |
| 160 | + |
| 161 | +if [ "$FAIL" -ne 0 ]; then |
| 162 | + echo "✗ Binary verification failed. Try running the script again (crashes can be intermittent)." |
| 163 | + exit 1 |
| 164 | +fi |
| 165 | + |
| 166 | +# Step 5: Optionally install Playwright |
| 167 | +if [[ "$INSTALL_PLAYWRIGHT" == "true" ]]; then |
| 168 | + echo "→ Installing Playwright browsers..." |
| 169 | + if [[ "${EUID:-$(id -u)}" -eq 0 ]]; then |
| 170 | + npx playwright install --with-deps chromium |
| 171 | + else |
| 172 | + npx playwright install chromium |
| 173 | + fi |
| 174 | +fi |
| 175 | + |
| 176 | +echo "" |
| 177 | +echo "=== ✓ Sandbox npm install complete ===" |
| 178 | +echo "Run 'npm run dev' to start the dev server." |
0 commit comments