Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit ff462df

Browse files
fix: windows install (anomalyco#4293)
Co-authored-by: GitHub Action <action@github.com>
1 parent 7344358 commit ff462df

5 files changed

Lines changed: 86 additions & 182 deletions

File tree

.github/workflows/snapshot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches:
66
- dev
7-
- opentui
7+
- windows
88
- v0
99

1010
concurrency: ${{ github.workflow }}-${{ github.ref }}

packages/opencode/bin/opencode

Lines changed: 84 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,84 @@
1-
#!/bin/sh
2-
set -e
3-
4-
if [ -n "$OPENCODE_BIN_PATH" ]; then
5-
resolved="$OPENCODE_BIN_PATH"
6-
else
7-
# Get the real path of this script, resolving any symlinks
8-
script_path="$0"
9-
while [ -L "$script_path" ]; do
10-
link_target="$(readlink "$script_path")"
11-
case "$link_target" in
12-
/*) script_path="$link_target" ;;
13-
*) script_path="$(dirname "$script_path")/$link_target" ;;
14-
esac
15-
done
16-
script_dir="$(dirname "$script_path")"
17-
script_dir="$(cd "$script_dir" && pwd)"
18-
19-
# Map platform names
20-
case "$(uname -s)" in
21-
Darwin) platform="darwin" ;;
22-
Linux) platform="linux" ;;
23-
MINGW*|CYGWIN*|MSYS*) platform="win32" ;;
24-
*) platform="$(uname -s | tr '[:upper:]' '[:lower:]')" ;;
25-
esac
26-
27-
# Map architecture names
28-
case "$(uname -m)" in
29-
x86_64|amd64) arch="x64" ;;
30-
aarch64) arch="arm64" ;;
31-
armv7l) arch="arm" ;;
32-
*) arch="$(uname -m)" ;;
33-
esac
34-
35-
name="opencode-${platform}-${arch}"
36-
binary="opencode"
37-
[ "$platform" = "win32" ] && binary="opencode.exe"
38-
39-
# Search for the binary starting from real script location
40-
resolved=""
41-
current_dir="$script_dir"
42-
while [ "$current_dir" != "/" ]; do
43-
candidate="$current_dir/node_modules/$name/bin/$binary"
44-
if [ -f "$candidate" ]; then
45-
resolved="$candidate"
46-
break
47-
fi
48-
current_dir="$(dirname "$current_dir")"
49-
done
50-
51-
if [ -z "$resolved" ]; then
52-
printf "It seems that your package manager failed to install the right version of the opencode CLI for your platform. You can try manually installing the \"%s\" package\n" "$name" >&2
53-
exit 1
54-
fi
55-
fi
56-
57-
# Handle SIGINT gracefully
58-
trap '' INT
59-
60-
# Execute the binary with all arguments
61-
exec "$resolved" "$@"
1+
#!/usr/bin/env node
2+
3+
const childProcess = require("child_process")
4+
const fs = require("fs")
5+
const path = require("path")
6+
const os = require("os")
7+
8+
function run(target) {
9+
const result = childProcess.spawnSync(target, process.argv.slice(2), {
10+
stdio: "inherit",
11+
})
12+
if (result.error) {
13+
console.error(result.error.message)
14+
process.exit(1)
15+
}
16+
const code = typeof result.status === "number" ? result.status : 0
17+
process.exit(code)
18+
}
19+
20+
const envPath = process.env.OPENCODE_BIN_PATH
21+
if (envPath) {
22+
run(envPath)
23+
}
24+
25+
const scriptPath = fs.realpathSync(__filename)
26+
const scriptDir = path.dirname(scriptPath)
27+
28+
const platformMap = {
29+
darwin: "darwin",
30+
linux: "linux",
31+
win32: "windows",
32+
}
33+
const archMap = {
34+
x64: "x64",
35+
arm64: "arm64",
36+
arm: "arm",
37+
}
38+
39+
let platform = platformMap[os.platform()]
40+
if (!platform) {
41+
platform = os.platform()
42+
}
43+
let arch = archMap[os.arch()]
44+
if (!arch) {
45+
arch = os.arch()
46+
}
47+
const base = "opencode-" + platform + "-" + arch
48+
const binary = platform === "windows" ? "opencode.exe" : "opencode"
49+
50+
function findBinary(startDir) {
51+
let current = startDir
52+
for (;;) {
53+
const modules = path.join(current, "node_modules")
54+
if (fs.existsSync(modules)) {
55+
const entries = fs.readdirSync(modules)
56+
for (const entry of entries) {
57+
if (!entry.startsWith(base)) {
58+
continue
59+
}
60+
const candidate = path.join(modules, entry, "bin", binary)
61+
if (fs.existsSync(candidate)) {
62+
return candidate
63+
}
64+
}
65+
}
66+
const parent = path.dirname(current)
67+
if (parent === current) {
68+
return
69+
}
70+
current = parent
71+
}
72+
}
73+
74+
const resolved = findBinary(scriptDir)
75+
if (!resolved) {
76+
console.error(
77+
'It seems that your package manager failed to install the right version of the opencode CLI for your platform. You can try manually installing the "' +
78+
base +
79+
'" package',
80+
)
81+
process.exit(1)
82+
}
83+
84+
run(resolved)

packages/opencode/bin/opencode.cmd

Lines changed: 0 additions & 58 deletions
This file was deleted.

packages/opencode/script/preinstall.mjs

Lines changed: 0 additions & 44 deletions
This file was deleted.

packages/opencode/script/publish.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { $ } from "bun"
33
import pkg from "../package.json"
44
import { Script } from "@opencode-ai/script"
55
import { fileURLToPath } from "url"
6-
import fs from "fs"
76

87
const dir = fileURLToPath(new URL("..", import.meta.url))
98
process.chdir(dir)
@@ -17,32 +16,16 @@ const { binaries } = await import("./build.ts")
1716

1817
await $`mkdir -p ./dist/${pkg.name}`
1918
await $`cp -r ./bin ./dist/${pkg.name}/bin`
20-
21-
// Copy Windows .exe if any Windows binaries were built
22-
let hasWindowsBinary = false
23-
for (const binaryName of Object.keys(binaries)) {
24-
if (binaryName.includes("win32")) {
25-
const winBinaryPath = `./dist/${binaryName}/bin/opencode.exe`
26-
if (fs.existsSync(winBinaryPath)) {
27-
await $`cp ${winBinaryPath} ./dist/${pkg.name}/bin/opencode.exe`
28-
hasWindowsBinary = true
29-
break
30-
}
31-
}
32-
}
33-
34-
await $`cp ./script/preinstall.mjs ./dist/${pkg.name}/preinstall.mjs`
3519
await $`cp ./script/postinstall.mjs ./dist/${pkg.name}/postinstall.mjs`
3620

3721
await Bun.file(`./dist/${pkg.name}/package.json`).write(
3822
JSON.stringify(
3923
{
4024
name: pkg.name + "-ai",
4125
bin: {
42-
[pkg.name]: hasWindowsBinary ? `./bin/${pkg.name}.exe` : `./bin/${pkg.name}`,
26+
[pkg.name]: `./bin/${pkg.name}`,
4327
},
4428
scripts: {
45-
preinstall: "bun ./preinstall.mjs || node ./preinstall.mjs",
4629
postinstall: "bun ./postinstall.mjs || node ./postinstall.mjs",
4730
},
4831
version: Script.version,

0 commit comments

Comments
 (0)