File size: 2,016 Bytes
dc7b96e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash
set -euo pipefail

DG_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
VERSION="${DG_OPENCODE_VERSION:-1.17.20}"
PREFIX="$DG_ROOT/.tools/opencode"
BIN="$PREFIX/node_modules/.bin/opencode"
LINUX_BIN="$PREFIX/node_modules/opencode-linux-x64/bin/opencode"
WIN_INSTALL="$DG_ROOT/scripts/install_opencode_windows.ps1"

if [[ "$(uname -s 2>/dev/null || true)" == Linux* && -x "$LINUX_BIN" && "${DG_REFRESH_EXTERNAL_AGENT:-0}" != "1" ]]; then
  "$LINUX_BIN" --version
  exit 0
fi

if [[ -x "$BIN" && "${DG_REFRESH_EXTERNAL_AGENT:-0}" != "1" ]]; then
  "$BIN" --version
  exit 0
fi

case "$(uname -s 2>/dev/null || true)" in
  Linux*)
    node_path_entries=()
    if [[ -x "$DG_ROOT/.tools/node-linux/bin/node" ]]; then
      node_path_entries+=("$DG_ROOT/.tools/node-linux/bin")
    fi
    if [[ -x "$DG_ROOT/.tools/node/bin/node" ]]; then
      node_path_entries+=("$DG_ROOT/.tools/node/bin")
    fi
    if ((${#node_path_entries[@]})); then
      export PATH="$(IFS=:; echo "${node_path_entries[*]}"):$PATH"
    fi
    if ! command -v npm >/dev/null 2>&1; then
      echo "npm is required to install OpenCode." >&2
      exit 1
    fi
    npm install --prefix "$PREFIX" --no-audit --no-fund --ignore-scripts "opencode-ai@$VERSION"
    npm install --prefix "$PREFIX" --no-audit --no-fund --ignore-scripts "opencode-linux-x64@$VERSION" || true
    node "$PREFIX/node_modules/opencode-ai/postinstall.mjs"
    if [[ -x "$LINUX_BIN" ]]; then
      "$LINUX_BIN" --version
    else
      "$BIN" --version
    fi
    ;;
  *)
    if command -v powershell.exe >/dev/null 2>&1 && [[ -f "$WIN_INSTALL" ]]; then
      powershell.exe -NoProfile -ExecutionPolicy Bypass -File "$WIN_INSTALL" -Version "$VERSION"
    elif command -v powershell >/dev/null 2>&1 && [[ -f "$WIN_INSTALL" ]]; then
      powershell -NoProfile -ExecutionPolicy Bypass -File "$WIN_INSTALL" -Version "$VERSION"
    else
      echo "PowerShell installer is required to install OpenCode on this host." >&2
      exit 1
    fi
    ;;
esac