opencode-drive 0.1.0 → 0.1.1
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.
- package/package.json +1 -1
- package/src/cli/index.ts +7 -1
- package/src/cli/stop.ts +31 -0
package/package.json
CHANGED
package/src/cli/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { send } from "./send.js"
|
|
|
6
6
|
import { describe } from "./describe.js"
|
|
7
7
|
import { extractCommands } from "./parse.js"
|
|
8
8
|
import { start } from "./start.js"
|
|
9
|
+
import { stop } from "./stop.js"
|
|
9
10
|
import type { DriveCommand, SendOptions, StartOptions } from "./types.js"
|
|
10
11
|
|
|
11
12
|
const extracted = extract()
|
|
@@ -58,9 +59,14 @@ const describeCommand = Command.make("describe", { name }, (config) =>
|
|
|
58
59
|
Command.withDescription("Describe a registered OpenCode instance"),
|
|
59
60
|
)
|
|
60
61
|
|
|
62
|
+
const stopCommand = Command.make("stop", { name }, (config) =>
|
|
63
|
+
execute(() => stop(Option.getOrUndefined(config.name)))).pipe(
|
|
64
|
+
Command.withDescription("Stop a registered headless OpenCode instance"),
|
|
65
|
+
)
|
|
66
|
+
|
|
61
67
|
const root = Command.make("opencode-drive").pipe(
|
|
62
68
|
Command.withDescription("Drive real and simulated OpenCode instances"),
|
|
63
|
-
Command.withSubcommands([startCommand, sendCommand, describeCommand]),
|
|
69
|
+
Command.withSubcommands([startCommand, sendCommand, describeCommand, stopCommand]),
|
|
64
70
|
)
|
|
65
71
|
|
|
66
72
|
Command.runWith(root, { version: "0.1.0" })(extracted.args).pipe(
|
package/src/cli/stop.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { resolveInstance, unregisterInstance } from "./registry.js"
|
|
2
|
+
|
|
3
|
+
export async function stop(name?: string) {
|
|
4
|
+
const manifest = await resolveInstance(name ?? "default")
|
|
5
|
+
if (!manifest.headless) throw new Error(`drive instance "${manifest.name}" is visible`)
|
|
6
|
+
process.kill(manifest.pid, "SIGTERM")
|
|
7
|
+
await Promise.race([waitForExit(manifest.pid), Bun.sleep(1_000)])
|
|
8
|
+
if (alive(manifest.pid)) process.kill(manifest.pid, "SIGKILL")
|
|
9
|
+
await waitForExit(manifest.pid)
|
|
10
|
+
await unregisterInstance(manifest.name, manifest.pid)
|
|
11
|
+
console.log("success")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function waitForExit(pid: number) {
|
|
15
|
+
return new Promise<void>((resolve) => {
|
|
16
|
+
const check = () => {
|
|
17
|
+
if (!alive(pid)) return resolve()
|
|
18
|
+
setTimeout(check, 25)
|
|
19
|
+
}
|
|
20
|
+
check()
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function alive(pid: number) {
|
|
25
|
+
try {
|
|
26
|
+
process.kill(pid, 0)
|
|
27
|
+
return true
|
|
28
|
+
} catch {
|
|
29
|
+
return false
|
|
30
|
+
}
|
|
31
|
+
}
|