MCPcopy
hub / github.com/callstack/agent-device / runCmdBackground

Function runCmdBackground

src/utils/exec.ts:359–422  ·  view source on GitHub ↗
(
  cmd: string,
  args: string[],
  options: ExecBackgroundOptions = {},
)

Source from the content-addressed store, hash-verified

357}
358
359export function runCmdBackground(
360 cmd: string,
361 args: string[],
362 options: ExecBackgroundOptions = {},
363): ExecBackgroundResult {
364 const executable = normalizeExecutableCommand(cmd);
365 const execTrace = createExecTraceContext();
366 const child = spawn(executable, args, {
367 cwd: options.cwd,
368 env: options.env,
369 stdio: options.stdio ?? ['ignore', 'pipe', 'pipe'],
370 detached: options.detached,
371 windowsHide: true,
372 shell: false,
373 });
374 execTrace.emitBackgroundSpawn(cmd, args);
375
376 let stdout = '';
377 let stderr = '';
378 const captureOutput = options.captureOutput ?? true;
379 const abort = watchCommandAbort(child, options);
380
381 if (captureOutput) {
382 child.stdout?.setEncoding('utf8');
383 child.stderr?.setEncoding('utf8');
384
385 child.stdout?.on('data', (chunk) => {
386 stdout += chunk;
387 });
388 child.stderr?.on('data', (chunk) => {
389 stderr += chunk;
390 });
391 }
392
393 const wait = new Promise<ExecResult>((resolve, reject) => {
394 child.on('error', (err) => {
395 abort.dispose();
396 execTrace.emitBackgroundCompletion(cmd, args, 'error');
397 reject(spawnRejectionError(abort, executable, cmd, args, err));
398 });
399 child.on('close', (code) => {
400 abort.dispose();
401 execTrace.emitBackgroundCompletion(cmd, args, 'exit');
402 const exitCode = code ?? 1;
403 const failure = commandCloseFailure(
404 abort,
405 executable,
406 cmd,
407 args,
408 exitCode,
409 options.allowFailure,
410 stdout,
411 stderr,
412 );
413 if (failure) {
414 reject(failure);
415 return;
416 }

Calls 6

createExecTraceContextFunction · 0.85
watchCommandAbortFunction · 0.85
spawnRejectionErrorFunction · 0.85
commandCloseFailureFunction · 0.85
resolveFunction · 0.70

Tested by

no test coverage detected