opencode-node 0.0.0-next-15760

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/opencode2-node +133 -0
  2. package/package.json +28 -0
@@ -0,0 +1,133 @@
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
+ const forwardedSignals = ["SIGINT", "SIGTERM", "SIGHUP"]
9
+
10
+ function run(target) {
11
+ const child = childProcess.spawn(target, process.argv.slice(2), { stdio: "inherit" })
12
+ child.on("error", (error) => {
13
+ console.error(error.message)
14
+ process.exit(1)
15
+ })
16
+ const forwarders = {}
17
+ for (const signal of forwardedSignals) {
18
+ forwarders[signal] = () => {
19
+ try {
20
+ child.kill(signal)
21
+ } catch {}
22
+ }
23
+ process.on(signal, forwarders[signal])
24
+ }
25
+ child.on("exit", (code, signal) => {
26
+ for (const forwardedSignal of forwardedSignals) process.removeListener(forwardedSignal, forwarders[forwardedSignal])
27
+ if (signal) return process.kill(process.pid, signal)
28
+ process.exit(typeof code === "number" ? code : 0)
29
+ })
30
+ }
31
+
32
+ const envPath = process.env.OPENCODE_BIN_PATH
33
+ const scriptDir = path.dirname(fs.realpathSync(__filename))
34
+ const command = path.basename(__filename).replace(/\.cjs$/, "")
35
+ const nodeBuild = command === "opencode2-node"
36
+ const cached = path.join(scriptDir, `.${command}`)
37
+ const platform = { darwin: "darwin", linux: "linux", win32: "windows" }[os.platform()] || os.platform()
38
+ const arch = { x64: "x64", arm64: "arm64", arm: "arm" }[os.arch()] || os.arch()
39
+ const base = `@opencode-ai/cli${nodeBuild ? "-node" : ""}-` + platform + "-" + arch
40
+ const binary = platform === "windows" ? `${command}.exe` : command
41
+
42
+ function supportsAvx2() {
43
+ if (arch !== "x64") return false
44
+ if (platform === "linux") {
45
+ try {
46
+ return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
47
+ } catch {
48
+ return false
49
+ }
50
+ }
51
+ if (platform === "darwin") {
52
+ try {
53
+ const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], { encoding: "utf8", timeout: 1500 })
54
+ return result.status === 0 && (result.stdout || "").trim() === "1"
55
+ } catch {
56
+ return false
57
+ }
58
+ }
59
+ if (platform === "windows") {
60
+ const command =
61
+ '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
62
+ for (const executable of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) {
63
+ try {
64
+ const result = childProcess.spawnSync(executable, ["-NoProfile", "-NonInteractive", "-Command", command], {
65
+ encoding: "utf8",
66
+ timeout: 3000,
67
+ windowsHide: true,
68
+ })
69
+ if (result.status !== 0) continue
70
+ const output = (result.stdout || "").trim().toLowerCase()
71
+ if (output === "true" || output === "1") return true
72
+ if (output === "false" || output === "0") return false
73
+ } catch {
74
+ continue
75
+ }
76
+ }
77
+ }
78
+ return false
79
+ }
80
+
81
+ const names = (() => {
82
+ if (nodeBuild) return [base]
83
+ const baseline = arch === "x64" && !supportsAvx2()
84
+ if (platform === "linux") {
85
+ const musl = (() => {
86
+ try {
87
+ if (fs.existsSync("/etc/alpine-release")) return true
88
+ const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
89
+ return ((result.stdout || "") + (result.stderr || "")).toLowerCase().includes("musl")
90
+ } catch {
91
+ return false
92
+ }
93
+ })()
94
+ if (musl)
95
+ return arch === "x64"
96
+ ? baseline
97
+ ? [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
98
+ : [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
99
+ : [`${base}-musl`, base]
100
+ return arch === "x64"
101
+ ? baseline
102
+ ? [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
103
+ : [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
104
+ : [base, `${base}-musl`]
105
+ }
106
+ return arch === "x64" ? (baseline ? [`${base}-baseline`, base] : [base, `${base}-baseline`]) : [base]
107
+ })()
108
+
109
+ function findBinary(startDir) {
110
+ let current = startDir
111
+ for (;;) {
112
+ const modules = path.join(current, "node_modules")
113
+ if (fs.existsSync(modules))
114
+ for (const name of names) {
115
+ const candidate = path.join(modules, name, "bin", binary)
116
+ if (fs.existsSync(candidate)) return candidate
117
+ }
118
+ const parent = path.dirname(current)
119
+ if (parent === current) return
120
+ current = parent
121
+ }
122
+ }
123
+
124
+ const resolved = envPath || (fs.existsSync(cached) ? cached : findBinary(scriptDir))
125
+ if (!resolved) {
126
+ console.error(
127
+ `It seems that your package manager failed to install the right ${command} CLI package. Try manually installing ` +
128
+ names.map((name) => `"${name}"`).join(" or ") +
129
+ " package",
130
+ )
131
+ process.exit(1)
132
+ }
133
+ run(resolved)
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "opencode-node",
3
+ "bin": {
4
+ "opencode2-node": "./bin/opencode2-node"
5
+ },
6
+ "version": "0.0.0-next-15760",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/anomalyco/opencode.git"
11
+ },
12
+ "os": [
13
+ "darwin",
14
+ "linux",
15
+ "win32"
16
+ ],
17
+ "cpu": [
18
+ "arm64",
19
+ "x64"
20
+ ],
21
+ "optionalDependencies": {
22
+ "@opencode-ai/cli-node-linux-arm64": "0.0.0-next-15760",
23
+ "@opencode-ai/cli-node-windows-arm64": "0.0.0-next-15760",
24
+ "@opencode-ai/cli-node-darwin-arm64": "0.0.0-next-15760",
25
+ "@opencode-ai/cli-node-linux-x64": "0.0.0-next-15760",
26
+ "@opencode-ai/cli-node-windows-x64": "0.0.0-next-15760"
27
+ }
28
+ }