index.js 800 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const exec = require('child_process').exec;
  2. const tree = require('./tree');
  3. const utils = require('./utils');
  4. var hasPS = true;
  5. // discover if the OS has `ps`, and therefore can use psTree
  6. exec('ps', (error) => {
  7. module.exports.hasPS = hasPS = !error;
  8. });
  9. module.exports = function main(pid, callback) {
  10. if (typeof pid === 'number') {
  11. pid = pid.toString();
  12. }
  13. if (hasPS && !process.env.NO_PS) {
  14. return tree(pid, callback);
  15. }
  16. utils
  17. .getStat()
  18. .then(utils.tree)
  19. .then((tree) => utils.pidsForTree(tree, pid))
  20. .then((res) =>
  21. callback(
  22. null,
  23. res.map((p) => p.PID)
  24. )
  25. )
  26. .catch((error) => callback(error));
  27. };
  28. if (!module.parent) {
  29. module.exports(process.argv[2], (e, pids) => console.log(pids));
  30. }
  31. module.exports.hasPS = hasPS;