{"version":3,"file":"spawnPosix.cjs","names":[],"sources":["../../../../src/utils/runParallel/spawnPosix.ts"],"sourcesContent":["//------------------------------------------------------------------------------\n// Requirements\n//------------------------------------------------------------------------------\n\nimport type { SpawnOptions } from 'node:child_process';\nimport { type ChildProcess, spawn as nodeSpawn } from 'node:child_process';\n\n//------------------------------------------------------------------------------\n// Helpers\n//------------------------------------------------------------------------------\n\n/**\n * Kills the new process and its sub processes synchronously using\n * process group kill (negative PID). This ensures all descendants\n * are terminated before the parent calls process.exit().\n */\nconst createKillHandler = (\n  child: ChildProcess\n): ((signal?: NodeJS.Signals | number) => boolean) => {\n  return (signal?: NodeJS.Signals | number): boolean => {\n    if (!child.pid) return false;\n\n    const killSignal = signal ?? 'SIGTERM';\n\n    // Use synchronous process group kill (negative PID) as primary strategy.\n    // This kills the entire process group (shell + all descendants) immediately.\n    try {\n      process.kill(-child.pid, killSignal);\n      return true;\n    } catch {\n      // Process group kill failed (e.g., process not a group leader).\n    }\n\n    // Fallback: kill the child process directly.\n    try {\n      process.kill(child.pid, killSignal);\n    } catch {\n      // ignore — process may have already exited.\n    }\n\n    return true;\n  };\n};\n\n//------------------------------------------------------------------------------\n// Public Interface\n//------------------------------------------------------------------------------\n\n/**\n * Launches a new process with the given command.\n * This is almost same as `child_process.spawn`.\n *\n * This returns a `ChildProcess` instance.\n * `kill` method of the instance kills the new process and its sub processes.\n *\n * @param command - The command to run.\n * @param args - List of string arguments.\n * @param options - Options.\n * @returns A ChildProcess instance of new process.\n * @private\n */\nexport const spawnPosix = (\n  command: string,\n  args: string[],\n  options: SpawnOptions\n): ChildProcess => {\n  // Spawn detached so the child becomes its own process group leader.\n  // This allows killing the entire tree via process.kill(-pid, signal).\n  const child = nodeSpawn(command, args, { ...options, detached: true });\n  child.kill = createKillHandler(child);\n\n  return child;\n};\n"],"mappings":";;;;;;;;;;AAgBA,MAAM,qBACJ,UACoD;CACpD,QAAQ,WAA8C;EACpD,IAAI,CAAC,MAAM,KAAK,OAAO;EAEvB,MAAM,aAAa,UAAU;EAI7B,IAAI;GACF,QAAQ,KAAK,CAAC,MAAM,KAAK,UAAU;GACnC,OAAO;EACT,QAAQ,CAER;EAGA,IAAI;GACF,QAAQ,KAAK,MAAM,KAAK,UAAU;EACpC,QAAQ,CAER;EAEA,OAAO;CACT;AACF;;;;;;;;;;;;;;AAmBA,MAAa,cACX,SACA,MACA,YACiB;CAGjB,MAAM,sCAAkB,SAAS,MAAM;EAAE,GAAG;EAAS,UAAU;CAAK,CAAC;CACrE,MAAM,OAAO,kBAAkB,KAAK;CAEpC,OAAO;AACT"}