(
command,
args,
options,
{ terminateProcessTree = false, captureOutput } = {},
)
| 197 | } |
| 198 | |
| 199 | export async function spawnDevServer( |
| 200 | command, |
| 201 | args, |
| 202 | options, |
| 203 | { terminateProcessTree = false, captureOutput } = {}, |
| 204 | ) { |
| 205 | const managePosixProcessGroup = |
| 206 | process.platform !== "win32" && terminateProcessTree; |
| 207 | const child = spawn( |
| 208 | command, |
| 209 | args, |
| 210 | managePosixProcessGroup ? { ...options, detached: true } : options, |
| 211 | ); |
| 212 | const signalHandlers = new Map(); |
| 213 | let forwardedSignal = null; |
| 214 | const terminationPromises = []; |
| 215 | |
| 216 | const removeSignalHandlers = () => { |
| 217 | for (const [signal, handler] of signalHandlers) { |
| 218 | process.off(signal, handler); |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | for (const signal of terminationSignals) { |
| 223 | const handler = () => { |
| 224 | if ( |
| 225 | !managePosixProcessGroup && |
| 226 | (child.exitCode !== null || child.signalCode !== null) |
| 227 | ) { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | forwardedSignal = signal; |
| 232 | const terminationPromise = terminateProcessTree |
| 233 | ? process.platform === "win32" |
| 234 | ? terminateWindowsProcessTree(child, signal) |
| 235 | : terminatePosixProcessGroup(child, signal) |
| 236 | : Promise.resolve(child.kill(signal)); |
| 237 | terminationPromises.push(terminationPromise); |
| 238 | // The child may take longer to exit than the termination command takes to |
| 239 | // fail. Attach a handler now and rethrow when both operations are awaited. |
| 240 | void terminationPromise.catch(() => {}); |
| 241 | }; |
| 242 | |
| 243 | signalHandlers.set(signal, handler); |
| 244 | process.on(signal, handler); |
| 245 | } |
| 246 | |
| 247 | const childResult = new Promise((resolve, reject) => { |
| 248 | child.once("error", (error) => { |
| 249 | reject(error); |
| 250 | }); |
| 251 | |
| 252 | child.once("exit", (code, signal) => { |
| 253 | resolve({ code, signal }); |
| 254 | }); |
| 255 | }); |
| 256 |
no test coverage detected