🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

pidtree

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pidtree - npm Package Compare versions

Comparing version
0.3.0
to
0.3.1
+189
readme.md
<h1 align="center">
<b>pidtree</b>
</h1>
<p align="center">
<!-- CI - TravisCI -->
<a href="https://travis-ci.org/simonepri/pidtree">
<img src="https://img.shields.io/travis/simonepri/pidtree/master.svg?label=MacOS%20%26%20Linux" alt="Mac/Linux Build Status" />
</a>
<!-- CI - AppVeyor -->
<a href="https://ci.appveyor.com/project/simonepri/pidtree">
<img src="https://img.shields.io/appveyor/ci/simonepri/pidtree/master.svg?label=Windows" alt="Windows Build status" />
</a>
<!-- Coverage - Codecov -->
<a href="https://codecov.io/gh/simonepri/pidtree">
<img src="https://img.shields.io/codecov/c/github/simonepri/pidtree/master.svg" alt="Codecov Coverage report" />
</a>
<!-- DM - Snyk -->
<a href="https://snyk.io/test/github/simonepri/pidtree?targetFile=package.json">
<img src="https://snyk.io/test/github/simonepri/pidtree/badge.svg?targetFile=package.json" alt="Known Vulnerabilities" />
</a>
<!-- DM - David -->
<a href="https://david-dm.org/simonepri/pidtree">
<img src="https://david-dm.org/simonepri/pidtree/status.svg" alt="Dependency Status" />
</a>
<br/>
<!-- Code Style - XO-Prettier -->
<a href="https://github.com/xojs/xo">
<img src="https://img.shields.io/badge/code_style-XO+Prettier-5ed9c7.svg" alt="XO Code Style used" />
</a>
<!-- Test Runner - AVA -->
<a href="https://github.com/avajs/ava">
<img src="https://img.shields.io/badge/test_runner-AVA-fb3170.svg" alt="AVA Test Runner used" />
</a>
<!-- Test Coverage - Istanbul -->
<a href="https://github.com/istanbuljs/nyc">
<img src="https://img.shields.io/badge/test_coverage-NYC-fec606.svg" alt="Istanbul Test Coverage used" />
</a>
<!-- Init - ni -->
<a href="https://github.com/simonepri/ni">
<img src="https://img.shields.io/badge/initialized_with-ni-e74c3c.svg" alt="NI Scaffolding System used" />
</a>
<!-- Release - np -->
<a href="https://github.com/sindresorhus/np">
<img src="https://img.shields.io/badge/released_with-np-6c8784.svg" alt="NP Release System used" />
</a>
<br/>
<!-- Version - npm -->
<a href="https://www.npmjs.com/package/pidtree">
<img src="https://img.shields.io/npm/v/pidtree.svg" alt="Latest version on npm" />
</a>
<!-- License - MIT -->
<a href="https://github.com/simonepri/pidtree/tree/master/license">
<img src="https://img.shields.io/github/license/simonepri/pidtree.svg" alt="Project license" />
</a>
</p>
<p align="center">
🚸 Cross platform children list of a PID.
<br/>
<sub>
Coded with ❤️ by <a href="#authors">Simone Primarosa</a>.
</sub>
</p>
## Synopsis
This package is really similar to [ps-tree][gh:ps-tree] but is faster, safer and
provides sub-children results.
Furthermore ps-tree is [unmaintained][gh:ps-tree-um].
Uuh, and a fancy [CLI](#cli) is also available!
## Usage
```js
var pidtree = require('pidtree')
// Get childs of current process
pidtree(process.pid, function (err, pids) {
console.log(pids)
// => []
})
// Include the given pid in the result array
pidtree(process.pid, {root: true}, function (err, pids) {
console.log(pids)
// => [727]
})
// Get all the processes of the System (-1 is a special value of this package)
pidtree(-1, function (err, pids) {
console.log(pids)
// => [530, 42, ..., 41241]
})
// Include PPID in the results
pidtree(1, {advanced: true}, function (err, pids) {
console.log(pids)
// => [{ppid: 1, pid: 530}, {ppid: 1, pid: 42}, ..., {ppid: 1, pid: 41241}]
})
// If no callback is given it returns a promise instead
const pids = await pidtree(1)
console.log(pids)
// => [141, 42, ..., 15242]
```
## Compatibility
| Linux | FreeBSD | NetBSD | SunOS | macOS | Win | AIX |
| --- | --- | --- | --- | --- | --- | --- |
| ✅ | ❓ | ❓ | ❓ | ✅ | ✅ | ❓ |
✅ = Working
❓ = Not tested but should work
Please if your platform is not supported [file an issue][new issue].
## CLI
<img src="https://github.com/simonepri/pidtree/raw/master/media/cli.gif" alt="pidtree cli" width="300" align="right"/>
Show a tree of the processes inside your system inside your terminal.
```bash
npx pidtree $PPID
```
Just replace `$PPID` with one of the pids inside your system.
Or don't pass anything if you want all the pids inside your system.
```bash
npx pidtree
```
To display the output as a list, similar to the one produced from `pgrep -P $PID`,
pass the `--list` flag.
```bash
npx pidtree --list
```
## API
<a name="pidtree"></a>
## pidtree(pid, [options], [callback]) ⇒ <code>[Promise.&lt;Array.&lt;Object&gt;&gt;]</code>
Get the list of children pids of the given pid.
**Kind**: global function
**Returns**: <code>Promise.&lt;Array.&lt;Object&gt;&gt;</code> - Only when the callback is not provided.
**Access**: public
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| pid | <code>Number</code> \| <code>String</code> | | A pid. If -1 will return all the pids. |
| [options] | <code>Object</code> | | Optional options object. |
| [options.root] | <code>Boolean</code> | <code>false</code> | Include the provided pid in the list. Ignored if -1 is passed as pid. |
| [callback] | <code>function</code> | | Called when the list is ready. If not provided a promise is returned instead. |
## Related
- [pidusage][gh:pidusage] -
Cross-platform process cpu % and memory usage of a PID
## Authors
- **Simone Primarosa** - [simonepri][github:simonepri]
See also the list of [contributors][contributors] who participated in this project.
## License
This project is licensed under the MIT License - see the [license][license] file for details.
<!-- Links -->
[new issue]: https://github.com/simonepri/pidtree/issues/new
[license]: https://github.com/simonepri/pidtree/tree/master/license
[contributors]: https://github.com/simonepri/pidtree/contributors
[github:simonepri]: https://github.com/simonepri
[gh:pidusage]: https://github.com/soyuka/pidusage
[gh:ps-tree]: https://github.com/indexzero/ps-tree
[gh:ps-tree-um]: https://github.com/indexzero/ps-tree/issues/30
+41
-27

@@ -8,15 +8,21 @@ #!/usr/bin/env node

// The method startsWith is not defined on string objects in node 0.10
// eslint-disable-next-line no-extend-native
String.prototype.startsWith = function(suffix) {
return this.substring(0, suffix.length) === suffix;
};
function help() {
var help = ` Usage
$ pidtree <ppid>
Options
--list To print the pids as a list.
Examples
$ pidtree
$ pidtree --list
$ pidtree 1
$ pidtree 1 --list
`;
var help =
' Usage\n' +
' $ pidtree <ppid>\n' +
'\n' +
'Options\n' +
' --list To print the pids as a list.\n' +
'\n' +
'Examples\n' +
' $ pidtree\n' +
' $ pidtree --list\n' +
' $ pidtree 1\n' +
' $ pidtree 1 --list\n';
console.log(help);

@@ -31,2 +37,3 @@ }

}
console.log(list.join(os.EOL));

@@ -46,19 +53,25 @@ });

while (list.length > 0) {
var e = list.pop();
if (tree[e.ppid]) {
tree[e.ppid].push(e.pid);
var element = list.pop();
if (tree[element.ppid]) {
tree[element.ppid].push(element.pid);
} else {
tree[e.ppid] = [e.pid];
tree[element.ppid] = [element.pid];
}
if (ppid === -1) {
parents[e.pid] = e.ppid;
parents[element.pid] = element.ppid;
}
}
var roots = [ppid];
if (ppid === -1) {
// Get all the roots
roots = Object.keys(tree).filter(node => parents[node] === undefined);
roots = Object.keys(tree).filter(function(node) {
return parents[node] === undefined;
});
}
roots.forEach(root => print(tree, root));
roots.forEach(function(root) {
print(tree, root);
});
});

@@ -68,6 +81,6 @@

function printBranch(node, branch) {
const isGraphHead = branch.length === 0;
const children = tree[node] || [];
var isGraphHead = branch.length === 0;
var children = tree[node] || [];
let branchHead = '';
var branchHead = '';
if (!isGraphHead) {

@@ -77,13 +90,13 @@ branchHead = children.length > 0 ? '┬ ' : '─ ';

console.log(`${branch}${branchHead}${node}`);
console.log(branch + branchHead + node);
var baseBranch = branch;
if (!isGraphHead) {
const isChildOfLastBranch = branch.slice(-2) === '└─';
var isChildOfLastBranch = branch.slice(-2) === '└─';
baseBranch = branch.slice(0, -2) + (isChildOfLastBranch ? ' ' : '| ');
}
const nextBranch = baseBranch + '├─';
const lastBranch = baseBranch + '└─';
children.forEach((child, index) => {
var nextBranch = baseBranch + '├─';
var lastBranch = baseBranch + '└─';
children.forEach(function(child, index) {
printBranch(

@@ -110,2 +123,3 @@ child,

}
if (ppid === undefined) {

@@ -112,0 +126,0 @@ ppid = -1;

@@ -12,2 +12,8 @@ 'use strict';

// The method startsWith is not defined on string objects in node 0.10
// eslint-disable-next-line no-extend-native
String.prototype.startsWith = function(suffix) {
return this.substring(0, suffix.length) === suffix;
};
var pidtree = require('./lib/pidtree');

@@ -18,3 +24,3 @@

* @public
* @param {Number|String} PID A PID. If -1 will return all the pids.
* @param {Number|String} pid A PID. If -1 will return all the pids.
* @param {Object} [options] Optional options object.

@@ -33,2 +39,3 @@ * @param {Boolean} [options.root=false] Include the provided PID in the list.

}
if (typeof callback === 'function') {

@@ -38,2 +45,3 @@ pidtree(pid, options, callback);

}
return pify(pidtree, pid, options);

@@ -40,0 +48,0 @@ }

@@ -7,4 +7,5 @@ 'use strict';

* Spawn a binary and read its stdout.
* @param {String} cmd
* @param {String[]} args
* @param {String} cmd The name of the binary to spawn.
* @param {String[]} args The arguments for the binary.
* @param {Object} [options] Optional option for the spawn function.
* @param {Function} done(err, stdout)

@@ -11,0 +12,0 @@ */

@@ -12,3 +12,3 @@ 'use strict';

linux: 'ps',
aix: 'ps',
aix: 'ps'
};

@@ -20,2 +20,3 @@

}
var file = platformToMethod[platform];

@@ -29,3 +30,3 @@

if (file === undefined) {
return callback(
callback(
new Error(

@@ -32,0 +33,0 @@ os.platform() +

@@ -1,2 +0,1 @@

/* eslint-disable complexity */
'use strict';

@@ -13,3 +12,3 @@

* format {pid: X, ppid: Y}.
* @param {pidCallback} callback Called when the list is ready.
* @param {Function} callback(err, list) Called when the list is ready.
*/

@@ -21,2 +20,3 @@ function list(PID, options, callback) {

}
if (typeof options !== 'object') {

@@ -45,2 +45,3 @@ options = {};

}
callback(null, list);

@@ -56,2 +57,3 @@ return;

}
if (list[l][0] === PID) {

@@ -61,2 +63,3 @@ root = options.advanced ? {pid: PID} : PID; // Special pids like 0 on *nix

}
if (!root) {

@@ -70,7 +73,7 @@ callback(new Error('No maching pid found'));

while (list.length > 0) {
var e = list.pop();
if (tree[e[0]]) {
tree[e[0]].push(e[1]);
var element = list.pop();
if (tree[element[0]]) {
tree[element[0]].push(element[1]);
} else {
tree[e[0]] = [e[1]];
tree[element[0]] = [element[1]];
}

@@ -87,4 +90,4 @@ }

if (!tree[curpid]) continue;
var len = tree[curpid].length;
for (var j = 0; j < len; j++) {
var length = tree[curpid].length;
for (var j = 0; j < length; j++) {
pids.push(

@@ -96,2 +99,3 @@ options.advanced

}
delete tree[curpid];

@@ -103,2 +107,3 @@ }

}
callback(null, pids);

@@ -105,0 +110,0 @@ });

@@ -39,5 +39,6 @@ 'use strict';

}
callback(null, list);
} catch (err) {
callback(err);
} catch (error) {
callback(error);
}

@@ -44,0 +45,0 @@ });

@@ -18,2 +18,3 @@ 'use strict';

}
if (code !== 0) {

@@ -41,5 +42,6 @@ callback(new Error('pidtree wmic command exited with code ' + code));

}
callback(null, list);
} catch (err) {
callback(err);
} catch (error) {
callback(error);
}

@@ -46,0 +48,0 @@ });

{
"name": "pidtree",
"version": "0.3.0",
"version": "0.3.1",
"description": "Cross platform children list of a PID",

@@ -47,3 +47,3 @@ "license": "MIT",

"lint": "xo",
"test": "npm run lint&& nyc ava -m \"!*benchmark*\"",
"test": "nyc ava -m \"!*benchmark*\"",
"bench": "ava -m \"*benchmark*\"",

@@ -53,8 +53,8 @@ "coverage": "codecov"

"devDependencies": {
"ava": "*",
"codecov": "*",
"ava": "~0.25.0",
"codecov": "^3.6.5",
"mockery": "^2.1.0",
"np": "*",
"npm-check": "*",
"nyc": "*",
"np": "^2.20.1",
"npm-check": "^5.9.2",
"nyc": "^11.6.0",
"pify": "^3.0.0",

@@ -65,3 +65,3 @@ "string-to-stream": "^1.1.0",

"tree-kill": "^1.1.0",
"xo": "*"
"xo": "~0.20.3"
},

@@ -81,7 +81,11 @@ "ava": {

"rules": {
"prefer-destructuring": 0,
"prefer-arrow-callback": 0,
"no-var": 0,
"object-shorthand": 0
"object-shorthand": 0,
"unicorn/no-for-loop": 0,
"unicorn/prefer-string-slice": 0,
"unicorn/string-content": 0
}
}
}
<h1 align="center">
<b>pidtree</b>
</h1>
<p align="center">
<!-- CI - TravisCI -->
<a href="https://travis-ci.org/simonepri/pidtree">
<img src="https://img.shields.io/travis/simonepri/pidtree/master.svg?label=MacOS%20%26%20Linux" alt="Mac/Linux Build Status" />
</a>
<!-- CI - AppVeyor -->
<a href="https://ci.appveyor.com/project/simonepri/pidtree">
<img src="https://img.shields.io/appveyor/ci/simonepri/pidtree/master.svg?label=Windows" alt="Windows Build status" />
</a>
<!-- Coverage - Codecov -->
<a href="https://codecov.io/gh/simonepri/pidtree">
<img src="https://img.shields.io/codecov/c/github/simonepri/pidtree/master.svg" alt="Codecov Coverage report" />
</a>
<!-- DM - Snyk -->
<a href="https://snyk.io/test/github/simonepri/pidtree?targetFile=package.json">
<img src="https://snyk.io/test/github/simonepri/pidtree/badge.svg?targetFile=package.json" alt="Known Vulnerabilities" />
</a>
<!-- DM - David -->
<a href="https://david-dm.org/simonepri/pidtree">
<img src="https://david-dm.org/simonepri/pidtree/status.svg" alt="Dependency Status" />
</a>
<br/>
<!-- Code Style - XO-Prettier -->
<a href="https://github.com/xojs/xo">
<img src="https://img.shields.io/badge/code_style-XO+Prettier-5ed9c7.svg" alt="XO Code Style used" />
</a>
<!-- Test Runner - AVA -->
<a href="https://github.com/avajs/ava">
<img src="https://img.shields.io/badge/test_runner-AVA-fb3170.svg" alt="AVA Test Runner used" />
</a>
<!-- Test Coverage - Istanbul -->
<a href="https://github.com/istanbuljs/nyc">
<img src="https://img.shields.io/badge/test_coverage-NYC-fec606.svg" alt="Istanbul Test Coverage used" />
</a>
<!-- Init - ni -->
<a href="https://github.com/simonepri/ni">
<img src="https://img.shields.io/badge/initialized_with-ni-e74c3c.svg" alt="NI Scaffolding System used" />
</a>
<!-- Release - np -->
<a href="https://github.com/sindresorhus/np">
<img src="https://img.shields.io/badge/released_with-np-6c8784.svg" alt="NP Release System used" />
</a>
<br/>
<!-- Version - npm -->
<a href="https://www.npmjs.com/package/pidtree">
<img src="https://img.shields.io/npm/v/pidtree.svg" alt="Latest version on npm" />
</a>
<!-- License - MIT -->
<a href="https://github.com/simonepri/pidtree/tree/master/license">
<img src="https://img.shields.io/github/license/simonepri/pidtree.svg" alt="Project license" />
</a>
</p>
<p align="center">
🚸 Cross platform children list of a PID.
<br/>
<sub>
Coded with ❤️ by <a href="#authors">Simone Primarosa</a>.
</sub>
</p>
## Synopsis
This package is really similar to [ps-tree][gh:ps-tree] but is faster, safer and
provides sub-children results.
Furthermore ps-tree is [unmaintained][gh:ps-tree-um].
Uuh, and a fancy [CLI](#cli) is also available!
## Usage
```js
var pidtree = require('pidtree')
// Get childs of current process
pidtree(process.pid, function (err, pids) {
console.log(pids)
// => []
})
// Include the given pid in the result array
pidtree(process.pid, {root: true}, function (err, pids) {
console.log(pids)
// => [727]
})
// Get all the processes of the System (-1 is a special value of this package)
pidtree(-1, function (err, pids) {
console.log(pids)
// => [530, 42, ..., 41241]
})
// Include PPID in the results
pidtree(1, {advanced: true}, function (err, pids) {
console.log(pids)
// => [{ppid: 1, pid: 530}, {ppid: 1, pid: 42}, ..., {ppid: 1, pid: 41241}]
})
// If no callback is given it returns a promise instead
const pids = await pidtree(1)
console.log(pids)
// => [141, 42, ..., 15242]
```
## Compatibility
| Linux | FreeBSD | NetBSD | SunOS | macOS | Win | AIX |
| --- | --- | --- | --- | --- | --- | --- |
| ✅ | ❓ | ❓ | ❓ | ✅ | ✅ | ❓ |
✅ = Working
❓ = Not tested but should work
Please if your platform is not supported [file an issue][new issue].
## CLI
<img src="https://github.com/simonepri/pidtree/raw/refactor/media/cli.gif" alt="pidtree cli" width="300" align="right"/>
Show a tree of the processes inside your system inside your terminal.
```bash
npx pidtree $PPID
```
Just replace `$PPID` with one of the pids inside your system.
Or don't pass anything if you want all the pids inside your system.
```bash
npx pidtree
```
To display the output as a list, similar to the one produced from `pgrep -P $PID`,
pass the `--list` flag.
```bash
npx pidtree --list
```
## API
<a name="pidtree"></a>
## pidtree(pid, [options], [callback]) ⇒ <code>[Promise.&lt;Array.&lt;Object&gt;&gt;]</code>
Get the list of children pids of the given pid.
**Kind**: global function
**Returns**: <code>Promise.&lt;Array.&lt;Object&gt;&gt;</code> - Only when the callback is not provided.
**Access**: public
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| pid | <code>Number</code> \| <code>String</code> | | A pid. If -1 will return all the pids. |
| [options] | <code>Object</code> | | Optional options object. |
| [options.root] | <code>Boolean</code> | <code>false</code> | Include the provided pid in the list. Ignored if -1 is passed as pid. |
| [callback] | <code>function</code> | | Called when the list is ready. If not provided a promise is returned instead. |
## Related
- [pidusage][gh:pidusage] -
Cross-platform process cpu % and memory usage of a PID
## Authors
- **Simone Primarosa** - [simonepri][github:simonepri]
See also the list of [contributors][contributors] who participated in this project.
## License
This project is licensed under the MIT License - see the [license][license] file for details.
<!-- Links -->
[new issue]: https://github.com/simonepri/pidtree/issues/new
[license]: https://github.com/simonepri/pidtree/tree/master/license
[contributors]: https://github.com/simonepri/pidtree/contributors
[github:simonepri]: https://github.com/simonepri
[gh:pidusage]: https://github.com/soyuka/pidusage
[gh:ps-tree]: https://github.com/indexzero/ps-tree
[gh:ps-tree-um]: https://github.com/indexzero/ps-tree/issues/30