Skip to content

Commit e2121a2

Browse files
committed
improve output in the event of server start failure
1 parent 5d61dd1 commit e2121a2

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

epicshop/mcp-dev/dev.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,28 @@ async function waitForServerReady({ process: childProcess, textMatch, name }) {
112112
if (!childProcess) return
113113

114114
return new Promise((resolve, reject) => {
115+
const outputBuffer = []
116+
117+
function addToBuffer(channel, data) {
118+
outputBuffer.push({ channel, data })
119+
}
120+
121+
function printAndReject(reason) {
122+
// Print all buffered output in sequence
123+
for (const { channel, data } of outputBuffer) {
124+
const str = data.toString()
125+
if (channel === 'stdout') {
126+
process.stdout.write(styleText('blue', `${name} `) + str)
127+
} else if (channel === 'stderr') {
128+
process.stderr.write(styleText('red', `${name} `) + str)
129+
}
130+
}
131+
reject(reason)
132+
}
133+
115134
const timeout = setTimeout(() => {
116135
process.kill()
117-
reject(new Error(`${name} failed to start within 10 seconds`))
136+
printAndReject(new Error(`${name} failed to start within 10 seconds`))
118137
}, 10_000)
119138

120139
function searchForMatch(data) {
@@ -124,21 +143,34 @@ async function waitForServerReady({ process: childProcess, textMatch, name }) {
124143
// Remove the listeners after finding the match
125144
childProcess.stdout.removeListener('data', searchForMatch)
126145
childProcess.stderr.removeListener('data', searchForMatch)
146+
childProcess.stdout.removeListener('data', bufferStdout)
147+
childProcess.stderr.removeListener('data', bufferStderr)
127148
resolve()
128149
}
129150
}
130-
childProcess.stdout.on('data', searchForMatch)
131-
childProcess.stderr.on('data', searchForMatch)
151+
152+
function bufferStdout(data) {
153+
addToBuffer('stdout', data)
154+
searchForMatch(data)
155+
}
156+
157+
function bufferStderr(data) {
158+
addToBuffer('stderr', data)
159+
searchForMatch(data)
160+
}
161+
162+
childProcess.stdout.on('data', bufferStdout)
163+
childProcess.stderr.on('data', bufferStderr)
132164

133165
childProcess.on('error', (err) => {
134166
clearTimeout(timeout)
135-
reject(err)
167+
printAndReject(err)
136168
})
137169

138170
childProcess.on('exit', (code) => {
139171
if (code !== 0) {
140172
clearTimeout(timeout)
141-
reject(new Error(`${name} exited with code ${code}`))
173+
printAndReject(new Error(`${name} exited with code ${code}`))
142174
}
143175
})
144176
})

0 commit comments

Comments
 (0)