index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. function pify(fn, arg1, arg2) {
  3. return new Promise(function(resolve, reject) {
  4. fn(arg1, arg2, function(err, data) {
  5. if (err) return reject(err);
  6. resolve(data);
  7. });
  8. });
  9. }
  10. // Node versions prior to 4.0.0 do not define have `startsWith`.
  11. /* istanbul ignore if */
  12. if (!String.prototype.startsWith) {
  13. // eslint-disable-next-line no-extend-native
  14. String.prototype.startsWith = function(suffix) {
  15. return this.substring(0, suffix.length) === suffix;
  16. };
  17. }
  18. var pidtree = require('./lib/pidtree');
  19. /**
  20. * Get the list of children pids of the given pid.
  21. * @public
  22. * @param {Number|String} pid A PID. If -1 will return all the pids.
  23. * @param {Object} [options] Optional options object.
  24. * @param {Boolean} [options.root=false] Include the provided PID in the list.
  25. * @param {Boolean} [options.advanced=false] Returns a list of objects in the
  26. * format {pid: X, ppid: Y}.
  27. * @param {Function} [callback=undefined] Called when the list is ready. If not
  28. * provided a promise is returned instead.
  29. * @returns {Promise.<Object[]>} Only when the callback is not provided.
  30. */
  31. function list(pid, options, callback) {
  32. if (typeof options === 'function') {
  33. callback = options;
  34. options = undefined;
  35. }
  36. if (typeof callback === 'function') {
  37. pidtree(pid, options, callback);
  38. return;
  39. }
  40. return pify(pidtree, pid, options);
  41. }
  42. module.exports = list;