Skip to content

Commit c05cb08

Browse files
Apply PR #21947: wip: node shim signals
2 parents b28b53a + 699563e commit c05cb08

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

packages/opencode/bin/opencode

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,33 @@ const path = require("path")
66
const os = require("os")
77

88
function run(target) {
9-
const result = childProcess.spawnSync(target, process.argv.slice(2), {
9+
const child = childProcess.spawn(target, process.argv.slice(2), {
1010
stdio: "inherit",
1111
})
12-
if (result.error) {
13-
console.error(result.error.message)
12+
child.on("error", (err) => {
13+
console.error(err.message)
1414
process.exit(1)
15+
})
16+
const forward = (sig) => {
17+
if (!child.killed) {
18+
try { child.kill(sig) } catch {}
19+
}
1520
}
16-
const code = typeof result.status === "number" ? result.status : 0
17-
process.exit(code)
21+
;["SIGINT", "SIGTERM", "SIGHUP"].forEach((sig) => {
22+
process.on(sig, () => forward(sig))
23+
})
24+
child.on("exit", (code, signal) => {
25+
if (signal) {
26+
process.kill(process.pid, signal)
27+
return
28+
}
29+
process.exit(typeof code === "number" ? code : 1)
30+
})
1831
}
1932

2033
const envPath = process.env.OPENCODE_BIN_PATH
2134
if (envPath) {
22-
run(envPath)
35+
return run(envPath)
2336
}
2437

2538
const scriptPath = fs.realpathSync(__filename)
@@ -28,7 +41,7 @@ const scriptDir = path.dirname(scriptPath)
2841
//
2942
const cached = path.join(scriptDir, ".opencode")
3043
if (fs.existsSync(cached)) {
31-
run(cached)
44+
return run(cached)
3245
}
3346

3447
const platformMap = {

0 commit comments

Comments
 (0)