MCPcopy Create free account
hub / github.com/bearlyai/OpenADE / spawn

Function spawn

projects/runtime-node/src/localPty.ts:59–114  ·  view source on GitHub ↗
(params: RuntimeNodePtySpawnParams)

Source from the content-addressed store, hash-verified

57 return () => local.listeners.delete(listener)
58 },
59 async spawn(params: RuntimeNodePtySpawnParams) {
60 await assertDirectory(params.cwd)
61 const ptyId = params.ptyId || `pty-${randomUUID()}`
62 if (local.active.has(ptyId)) return { ok: true }
63
64 const shell = shellCommand()
65 const child = spawn(shell, [], {
66 cwd: params.cwd,
67 env: { ...process.env, ...params.env, TERM: process.env.TERM || "xterm-256color" },
68 detached: process.platform !== "win32",
69 windowsHide: true,
70 })
71 const state: ActivePty = {
72 process: child,
73 output: [],
74 completed: false,
75 exitCode: null,
76 cwd: params.cwd,
77 }
78 local.active.set(ptyId, state)
79
80 child.stdout?.on("data", (data: Buffer) => {
81 const text = data.toString("utf8")
82 bufferOutput(state, text)
83 emit(local, { type: "output", ptyId, chunk: text })
84 })
85 child.stderr?.on("data", (data: Buffer) => {
86 const text = data.toString("utf8")
87 bufferOutput(state, text)
88 emit(local, { type: "output", ptyId, chunk: text })
89 })
90 child.once("exit", (exitCode) => {
91 if (state.completed) return
92 state.completed = true
93 state.exitCode = exitCode ?? 0
94 emit(local, { type: "exit", ptyId, exitCode: state.exitCode })
95 })
96 child.once("error", (error) => {
97 if (state.completed) return
98 state.completed = true
99 state.exitCode = 1
100 emit(local, { type: "output", ptyId, chunk: error.message })
101 emit(local, { type: "exit", ptyId, exitCode: 1 })
102 })
103
104 emit(local, {
105 type: "started",
106 ptyId,
107 pid: child.pid ?? -1,
108 pgid: process.platform === "win32" ? undefined : child.pid,
109 cwd: params.cwd,
110 shell,
111 processStartedAt: new Date().toISOString(),
112 })
113 return { ok: true }
114 },
115 async write(params: RuntimeNodePtyWriteParams) {
116 const state = local.active.get(params.ptyId)

Callers 6

execCommandFunction · 0.85
handleStartCommandFunction · 0.85
handleStartScriptFunction · 0.85
runGitFunction · 0.85
startChildFunction · 0.85
startManagedProcessMethod · 0.85

Calls 7

shellCommandFunction · 0.85
hasMethod · 0.80
assertDirectoryFunction · 0.70
bufferOutputFunction · 0.70
emitFunction · 0.70
setMethod · 0.65
onMethod · 0.45

Tested by

no test coverage detected