The best way to control Claude Code, Codex and more from any computer or phone

main
ts 61 lines 1.56 KB
Raw
1 /**
2 * Node-compatible process runner for Effect programs.
3 *
4 * This module provides the shared `runMain` implementation used by
5 * Node-compatible platform packages. It runs one Effect as the main process
6 * fiber, interrupts that fiber on `SIGINT` or `SIGTERM`, and delegates final
7 * exit-code handling to the configured teardown.
8 *
9 * @stability unstable
10 * @since 4.0.0
11 */
12 import type { Effect } from "effect/Effect"
13 import * as Runtime from "effect/Runtime"
14
15 /**
16 * Runs an Effect as the Node process main program, interrupting the fiber on
17 * `SIGINT` or `SIGTERM` and invoking the configured teardown to determine the
18 * process exit code.
19 *
20 * @stability unstable
21 * @category running
22 * @since 4.0.0
23 */
24 export const runMain: {
25 (
26 options?: {
27 readonly disableErrorReporting?: boolean | undefined
28 readonly teardown?: Runtime.Teardown | undefined
29 }
30 ): <E, A>(effect: Effect<A, E>) => void
31 <E, A>(
32 effect: Effect<A, E>,
33 options?: {
34 readonly disableErrorReporting?: boolean | undefined
35 readonly teardown?: Runtime.Teardown | undefined
36 }
37 ): void
38 } = Runtime.makeRunMain(({
39 fiber,
40 teardown
41 }) => {
42 let receivedSignal = false
43
44 fiber.addObserver((exit) => {
45 process.removeListener("SIGINT", onSigint)
46 process.removeListener("SIGTERM", onSigint)
47 teardown(exit, (code) => {
48 if (receivedSignal || code !== 0) {
49 process.exit(code)
50 }
51 })
52 })
53
54 function onSigint() {
55 receivedSignal = true
56 fiber.interruptUnsafe(fiber.id)
57 }
58
59 process.on("SIGINT", onSigint)
60 process.on("SIGTERM", onSigint)
61 })