Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions packages/opencode/bin/opencode
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@ const path = require("path")
const os = require("os")

function run(target) {
const result = childProcess.spawnSync(target, process.argv.slice(2), {
const child = childProcess.spawn(target, process.argv.slice(2), {
stdio: "inherit",
})
if (result.error) {
console.error(result.error.message)
child.on("error", (err) => {
console.error(err.message)
process.exit(1)
})
const forward = (sig) => {
if (!child.killed) {
try { child.kill(sig) } catch {}
}
}
const code = typeof result.status === "number" ? result.status : 0
process.exit(code)
;["SIGINT", "SIGTERM", "SIGHUP"].forEach((sig) => {
process.on(sig, () => forward(sig))
})
child.on("exit", (code, signal) => {
if (signal) {
process.kill(process.pid, signal)
return
}
process.exit(typeof code === "number" ? code : 1)
})
}

const envPath = process.env.OPENCODE_BIN_PATH
if (envPath) {
run(envPath)
return run(envPath)
}

const scriptPath = fs.realpathSync(__filename)
Expand All @@ -28,7 +41,7 @@ const scriptDir = path.dirname(scriptPath)
//
const cached = path.join(scriptDir, ".opencode")
if (fs.existsSync(cached)) {
run(cached)
return run(cached)
}

const platformMap = {
Expand Down
Loading