wip: node shim signals
This commit is contained in:
parent
ce26120205
commit
c3dc5a3466
1 changed files with 20 additions and 7 deletions
|
|
@ -6,20 +6,33 @@ const path = require("path")
|
||||||
const os = require("os")
|
const os = require("os")
|
||||||
|
|
||||||
function run(target) {
|
function run(target) {
|
||||||
const result = childProcess.spawnSync(target, process.argv.slice(2), {
|
const child = childProcess.spawn(target, process.argv.slice(2), {
|
||||||
stdio: "inherit",
|
stdio: "inherit",
|
||||||
})
|
})
|
||||||
if (result.error) {
|
child.on("error", (err) => {
|
||||||
console.error(result.error.message)
|
console.error(err.message)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
})
|
||||||
|
const forward = (sig) => {
|
||||||
|
if (!child.killed) {
|
||||||
|
try { child.kill(sig) } catch {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const code = typeof result.status === "number" ? result.status : 0
|
;["SIGINT", "SIGTERM", "SIGHUP"].forEach((sig) => {
|
||||||
process.exit(code)
|
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
|
const envPath = process.env.OPENCODE_BIN_PATH
|
||||||
if (envPath) {
|
if (envPath) {
|
||||||
run(envPath)
|
return run(envPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
const scriptPath = fs.realpathSync(__filename)
|
const scriptPath = fs.realpathSync(__filename)
|
||||||
|
|
@ -28,7 +41,7 @@ const scriptDir = path.dirname(scriptPath)
|
||||||
//
|
//
|
||||||
const cached = path.join(scriptDir, ".opencode")
|
const cached = path.join(scriptDir, ".opencode")
|
||||||
if (fs.existsSync(cached)) {
|
if (fs.existsSync(cached)) {
|
||||||
run(cached)
|
return run(cached)
|
||||||
}
|
}
|
||||||
|
|
||||||
const platformMap = {
|
const platformMap = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue