fix(cli): forward signals from npm shim (#26259)
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
parent
9ca4be62bd
commit
6e47ae769e
1 changed files with 32 additions and 12 deletions
|
|
@ -5,31 +5,51 @@ const fs = require("fs")
|
||||||
const path = require("path")
|
const path = require("path")
|
||||||
const os = require("os")
|
const os = require("os")
|
||||||
|
|
||||||
|
const forwardedSignals = ["SIGINT", "SIGTERM", "SIGHUP"]
|
||||||
|
|
||||||
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) {
|
|
||||||
console.error(result.error.message)
|
child.on("error", (error) => {
|
||||||
|
console.error(error.message)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
const forwarders = {}
|
||||||
|
for (const signal of forwardedSignals) {
|
||||||
|
forwarders[signal] = () => {
|
||||||
|
try {
|
||||||
|
child.kill(signal)
|
||||||
|
} catch {
|
||||||
|
// The child may have already exited.
|
||||||
}
|
}
|
||||||
const code = typeof result.status === "number" ? result.status : 0
|
}
|
||||||
process.exit(code)
|
process.on(signal, forwarders[signal])
|
||||||
|
}
|
||||||
|
|
||||||
|
child.on("exit", (code, signal) => {
|
||||||
|
for (const forwardedSignal of forwardedSignals) {
|
||||||
|
process.removeListener(forwardedSignal, forwarders[forwardedSignal])
|
||||||
|
}
|
||||||
|
|
||||||
|
if (signal) {
|
||||||
|
process.kill(process.pid, signal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
process.exit(typeof code === "number" ? code : 0)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const envPath = process.env.OPENCODE_BIN_PATH
|
const envPath = process.env.OPENCODE_BIN_PATH
|
||||||
if (envPath) {
|
|
||||||
run(envPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
const scriptPath = fs.realpathSync(__filename)
|
const scriptPath = fs.realpathSync(__filename)
|
||||||
const scriptDir = path.dirname(scriptPath)
|
const scriptDir = path.dirname(scriptPath)
|
||||||
|
|
||||||
//
|
//
|
||||||
const cached = path.join(scriptDir, ".opencode")
|
const cached = path.join(scriptDir, ".opencode")
|
||||||
if (fs.existsSync(cached)) {
|
|
||||||
run(cached)
|
|
||||||
}
|
|
||||||
|
|
||||||
const platformMap = {
|
const platformMap = {
|
||||||
darwin: "darwin",
|
darwin: "darwin",
|
||||||
|
|
@ -166,7 +186,7 @@ function findBinary(startDir) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolved = findBinary(scriptDir)
|
const resolved = envPath || (fs.existsSync(cached) ? cached : findBinary(scriptDir))
|
||||||
if (!resolved) {
|
if (!resolved) {
|
||||||
console.error(
|
console.error(
|
||||||
"It seems that your package manager failed to install the right version of the opencode CLI for your platform. You can try manually installing " +
|
"It seems that your package manager failed to install the right version of the opencode CLI for your platform. You can try manually installing " +
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue