index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. // The method startsWith is not defined on string objects in node 0.10
  11. // eslint-disable-next-line no-extend-native
  12. String.prototype.startsWith = function(suffix) {
  13. return this.substring(0, suffix.length) === suffix;
  14. };
  15. var pidtree = require('./lib/pidtree');
  16. /**
  17. * Get the list of children pids of the given pid.
  18. * @public
  19. * @param {Number|String} pid A PID. If -1 will return all the pids.
  20. * @param {Object} [options] Optional options object.
  21. * @param {Boolean} [options.root=false] Include the provided PID in the list.
  22. * @param {Boolean} [options.advanced=false] Returns a list of objects in the
  23. * format {pid: X, ppid: Y}.
  24. * @param {Function} [callback=undefined] Called when the list is ready. If not
  25. * provided a promise is returned instead.
  26. * @returns {Promise.<Object[]>} Only when the callback is not provided.
  27. */
  28. function list(pid, options, callback) {
  29. if (typeof options === 'function') {
  30. callback = options;
  31. options = undefined;
  32. }
  33. if (typeof callback === 'function') {
  34. pidtree(pid, options, callback);
  35. return;
  36. }
  37. return pify(pidtree, pid, options);
  38. }
  39. module.exports = list;