Dax Raad 8 месяцев назад
Родитель
Сommit
6d21525e71

+ 61 - 0
packages/opencode/bin/opencode

@@ -0,0 +1,61 @@
+#!/bin/sh
+set -e
+
+if [ -n "$OPENCODE_BIN_PATH" ]; then
+    resolved="$OPENCODE_BIN_PATH"
+else
+    # Get the real path of this script, resolving any symlinks
+    script_path="$0"
+    while [ -L "$script_path" ]; do
+        link_target="$(readlink "$script_path")"
+        case "$link_target" in
+            /*) script_path="$link_target" ;;
+            *) script_path="$(dirname "$script_path")/$link_target" ;;
+        esac
+    done
+    script_dir="$(dirname "$script_path")"
+    script_dir="$(cd "$script_dir" && pwd)"
+    
+    # Map platform names
+    case "$(uname -s)" in
+        Darwin) platform="darwin" ;;
+        Linux) platform="linux" ;;
+        MINGW*|CYGWIN*|MSYS*) platform="win32" ;;
+        *) platform="$(uname -s | tr '[:upper:]' '[:lower:]')" ;;
+    esac
+    
+    # Map architecture names  
+    case "$(uname -m)" in
+        x86_64|amd64) arch="x64" ;;
+        aarch64) arch="arm64" ;;
+        armv7l) arch="arm" ;;
+        *) arch="$(uname -m)" ;;
+    esac
+    
+    name="opencode-${platform}-${arch}"
+    binary="opencode"
+    [ "$platform" = "win32" ] && binary="opencode.exe"
+    
+    # Search for the binary starting from real script location
+    resolved=""
+    current_dir="$script_dir"
+    while [ "$current_dir" != "/" ]; do
+        candidate="$current_dir/node_modules/$name/bin/$binary"
+        if [ -f "$candidate" ]; then
+            resolved="$candidate"
+            break
+        fi
+        current_dir="$(dirname "$current_dir")"
+    done
+    
+    if [ -z "$resolved" ]; then
+        printf "It seems that your package manager failed to install the right version of the SST CLI for your platform. You can try manually installing the \"%s\" package\n" "$name" >&2
+        exit 1
+    fi
+fi
+
+# Handle SIGINT gracefully
+trap '' INT
+
+# Execute the binary with all arguments
+exec "$resolved" "$@"

+ 0 - 29
packages/opencode/bin/opencode.mjs

@@ -1,29 +0,0 @@
-#!/usr/bin/env node
-import { createRequire } from "node:module"
-const require = createRequire(import.meta.url)
-import path from "path"
-import { execFileSync } from "child_process"
-let resolved = process.env.SST_BIN_PATH
-if (!resolved) {
-  const name = `opencode-${process.platform}-${process.arch}`
-  const binary = process.platform === "win32" ? "opencode.exe" : "opencode"
-  try {
-    resolved = require.resolve(path.join(name, "bin", binary))
-  } catch (ex) {
-    console.error(
-      `It seems that your package manager failed to install the right version of the SST CLI for your platform. You can try manually installing the "${name}" package`,
-    )
-
-    process.exit(1)
-  }
-}
-
-process.on("SIGINT", () => {})
-
-try {
-  execFileSync(resolved, process.argv.slice(2), {
-    stdio: "inherit",
-  })
-} catch (ex) {
-  process.exit(1)
-}

+ 2 - 2
packages/opencode/script/release.ts

@@ -31,7 +31,7 @@ for (const [os, arch] of targets) {
   await $`GOOS=${os} GOARCH=${GOARCH[arch]} go build -ldflags="-s -w -X github.com/sst/opencode/internal/version.Version=${version}" -o ../opencode/dist/${name}/bin/tui ../tui/main.go`.cwd(
     "../tui",
   )
-  await $`bun build --compile --minify --target=bun-${os}-${arch} --outfile=dist/${name}/bin/opencode ./src/index.ts ./dist/${name}/bin/tui`
+  await $`bun build --define OPENCODE_VERSION="'${version}'" --compile --minify --target=bun-${os}-${arch} --outfile=dist/${name}/bin/opencode ./src/index.ts ./dist/${name}/bin/tui`
   await $`rm -rf ./dist/${name}/bin/tui`
   await Bun.file(`dist/${name}/package.json`).write(
     JSON.stringify(
@@ -56,7 +56,7 @@ await Bun.file(`./dist/${pkg.name}/package.json`).write(
     {
       name: pkg.name + "-ai",
       bin: {
-        [pkg.name]: `./bin/${pkg.name}.mjs`,
+        [pkg.name]: `./bin/${pkg.name}`,
       },
       version,
       optionalDependencies,

+ 5 - 2
packages/opencode/src/index.ts

@@ -7,11 +7,14 @@ import { Bus } from "./bus"
 import { Session } from "./session/session"
 import cac from "cac"
 import { Share } from "./share/share"
-import { LLM } from "./llm/llm"
 import { Message } from "./session/message"
 import { Global } from "./global"
 import { Provider } from "./provider/provider"
 
+declare global {
+  const OPENCODE_VERSION: string
+}
+
 const cli = cac("opencode")
 
 cli.command("", "Start the opencode in interactive mode").action(async () => {
@@ -111,6 +114,6 @@ cli
     })
   })
 
+cli.version(typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "dev")
 cli.help()
-cli.version("1.0.0")
 cli.parse()