{"version":3,"file":"index.mjs","names":[],"sources":["../src/utils.ts","../src/treekill.ts","../src/kill.ts","../src/index.ts"],"sourcesContent":["/**\n * Check if the error contains the 'code' property from usage of\n * process.kill. This code can contain values such as ESRCH.\n *\n * @param err the error object that was caught\n * @returns true if the error contains the 'code' property\n */\nexport const isKillError = (err: unknown) => {\n  return typeof err === 'object' && err !== null && 'code' in err;\n};\n","/**\n * Code below is inspired by tree-kill-sync and tree-kill:\n * - https://github.com/dvpnt/tree-kill-sync/blob/master/index.js\n * - https://github.com/pkrumins/node-tree-kill/blob/master/index.js\n */\n\nimport { execSync } from 'child_process';\nimport { isKillError } from './utils';\n\ntype PpidMap = Record<number, number[]>;\n\ninterface PidItem {\n  pid: number;\n  ppid: number;\n}\n\n/**\n * Retrieves a list of all running process IDs (PIDs) along with their parent\n * process IDs (PPIDs).\n *\n * @returns {PidItem[]} An array of PidItem objects containing PID and PPID.\n */\nconst getAllPids = (): PidItem[] =>\n  execSync('ps -A -o pid=,ppid=')\n    .toString()\n    .trim()\n    .split('\\n')\n    .map((row: string) => {\n      /* v8 ignore next */\n      const [, pid, ppid] = /\\s*(\\d+)\\s*(\\d+)/.exec(row) ?? [];\n      return {\n        pid: Number(pid),\n        ppid: Number(ppid),\n      };\n    });\n\n/**\n * Retrieves all child PIDs of a given parent PID.\n *\n * @param {number} parentPid - The parent PID for which to find child PIDs.\n * @returns {number[]} An array of child PIDs.\n */\nconst getAllChildPIDs = (parentPid: number): number[] => {\n  const all = getAllPids();\n  const ppidHash: PpidMap = all.reduce((hash: PpidMap, item) => {\n    hash[item.ppid] = (hash[item.ppid] || []).concat(item.pid);\n    return hash;\n  }, {});\n\n  const result: number[] = [];\n\n  /**\n   * Adds all children PIDs to the result array\n   *\n   * @param pid parent process ID of which to add children\n   */\n  const recursivelAddChild = (pid: number) => {\n    ppidHash[pid] = ppidHash[pid] || [];\n    ppidHash[pid].forEach((childPid) => {\n      result.push(childPid);\n      recursivelAddChild(childPid);\n    });\n  };\n  recursivelAddChild(parentPid);\n  return result;\n};\n\n/**\n * Kills a process with the specified PID using the given signal.\n * The error ESRCH will be ignored.\n *\n * @param {number} pid - The PID of the process to be killed.\n * @param {number | string} signal - The signal to send for termination.\n */\nexport const killPid = (pid: number, signal: number | string) => {\n  try {\n    process.kill(pid, signal);\n  } catch (err: unknown) {\n    /* v8 ignore next 3 */\n    if (isKillError(err) && err.code !== 'ESRCH') {\n      throw err;\n    }\n  }\n};\n\n/**\n * Recursively kills a process and all its child processes using the specified\n * signal - i.e. terminates the whole process tree.\n *\n * @param {number} pid - process pid to be killed alongside children.\n * @param {number | string} signal - the signal to send for termination.\n */\nexport const treeKill = (pid: number, signal: string | number) => {\n  getAllChildPIDs(pid).forEach((pid: number) => {\n    killPid(pid, signal);\n  });\n  killPid(pid, signal);\n};\n","import { execFileSync } from 'child_process';\nimport { killPid, treeKill } from './treekill';\n\n/**\n * Kills a process with the given PID.\n *\n * @param {number} pid - The process ID to kill.\n * @param {string | number} [signal='SIGTERM'] - signal to send to the process\n * @param {boolean} [recursive=false] - pass true for tree kill\n *\n * @returns {void}\n */\nconst killSync = (pid: number, signal?: string | number, recursive = false): void => {\n  signal = signal ?? 'SIGTERM';\n  if (!recursive) {\n    return killPid(pid, signal);\n  }\n  /* v8 ignore next 6 */\n  switch (process.platform) {\n    case 'win32':\n      execFileSync('taskkill', ['/pid', pid.toString(), '/T', '/F']);\n      break;\n    case 'darwin':\n    default:\n      treeKill(pid, signal);\n      break;\n  }\n};\n\nexport default killSync;\n","import killSync from './kill';\n\nexport default killSync;\n"],"mappings":"2DAOA,MAAa,EAAe,GACnB,OAAO,GAAQ,YAAY,GAAgB,SAAU,ECcxD,MACJ,EAAS,sBAAsB,CAC5B,UAAU,CACV,MAAM,CACN,MAAM;EAAK,CACX,IAAK,GAAgB,CAEpB,GAAM,EAAG,EAAK,GAAQ,mBAAmB,KAAK,EAAI,EAAI,EAAE,CACxD,MAAO,CACL,IAAK,OAAO,EAAI,CAChB,KAAM,OAAO,EAAK,CACnB,EACD,CAQA,EAAmB,GAAgC,CAEvD,IAAM,EADM,GACiB,CAAC,QAAQ,EAAe,KACnD,EAAK,EAAK,OAAS,EAAK,EAAK,OAAS,EAAE,EAAE,OAAO,EAAK,IAAI,CACnD,GACN,EAAE,CAAC,CAEA,EAAmB,EAAE,CAOrB,EAAsB,GAAgB,CAC1C,EAAS,GAAO,EAAS,IAAQ,EAAE,CACnC,EAAS,GAAK,QAAS,GAAa,CAClC,EAAO,KAAK,EAAS,CACrB,EAAmB,EAAS,EAC5B,EAGJ,OADA,EAAmB,EAAU,CACtB,GAUI,GAAW,EAAa,IAA4B,CAC/D,GAAI,CACF,QAAQ,KAAK,EAAK,EAAO,OAClB,EAAc,CAErB,GAAI,EAAY,EAAI,EAAI,EAAI,OAAS,QACnC,MAAM,IAYC,GAAY,EAAa,IAA4B,CAChE,EAAgB,EAAI,CAAC,QAAS,GAAgB,CAC5C,EAAQ,EAAK,EAAO,EACpB,CACF,EAAQ,EAAK,EAAO,EE9FtB,IAAA,GDUkB,EAAa,EAA0B,EAAY,KAAgB,CAEnF,GADA,IAAmB,UACf,CAAC,EACH,OAAO,EAAQ,EAAK,EAAO,CAG7B,OAAQ,QAAQ,SAAhB,CACE,IAAK,QACH,EAAa,WAAY,CAAC,OAAQ,EAAI,UAAU,CAAE,KAAM,KAAK,CAAC,CAC9D,MAEF,QACE,EAAS,EAAK,EAAO,CACrB"}