Skip to content

Commit 2e58adc

Browse files
committed
Add typescript types
Thanks to @lenovouser Ref #9
1 parent d9834cd commit 2e58adc

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

index.d.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
declare namespace PidTree {
2+
export interface Options {
3+
/**
4+
* Include the provided PID in the list. Ignored if -1 is passed as PID.
5+
* @default false
6+
*/
7+
root?: boolean;
8+
}
9+
10+
export interface AdvancedResult {
11+
/**
12+
* PID of the parent.
13+
*/
14+
ppid: number;
15+
/**
16+
* PID
17+
*/
18+
pid: number;
19+
}
20+
21+
export type Result = number;
22+
}
23+
24+
/**
25+
* Get the list of children pids of the given pid.
26+
* @param pid A PID. If -1 will return all the pids.
27+
* @param callback Called when the list is ready.
28+
*/
29+
declare function pidtree(
30+
pid: string | number,
31+
callback: (error: Error | undefined, result: PidTree.Result[]) => void
32+
): void;
33+
34+
/**
35+
* Get the list of children pids of the given pid.
36+
* @param pid A PID. If -1 will return all the pids.
37+
* @param options Options object.
38+
* @param callback Called when the list is ready.
39+
*/
40+
declare function pidtree(
41+
pid: string | number,
42+
options: PidTree.Options,
43+
callback: (error: Error | undefined, result: PidTree.Result[]) => void
44+
): void;
45+
46+
/**
47+
* Get the list of children pids of the given pid.
48+
* @param pid A PID. If -1 will return all the pids.
49+
* @param options Options object.
50+
* @param callback Called when the list is ready.
51+
*/
52+
declare function pidtree(
53+
pid: string | number,
54+
options: PidTree.Options & {advanced: true},
55+
callback: (error: Error | undefined, result: PidTree.AdvancedResult[]) => void
56+
): void;
57+
58+
/**
59+
* Get the list of children pids of the given pid.
60+
* @param pid A PID. If -1 will return all the pids.
61+
* @param [options] Optional options object.
62+
* @returns A promise containing the list.
63+
*/
64+
declare function pidtree(
65+
pid: string | number,
66+
options?: PidTree.Options
67+
): Promise<PidTree.Result[]>;
68+
69+
/**
70+
* Get the list of children pids of the given pid.
71+
* @param pid A PID. If -1 will return all the pids.
72+
* @param options Options object.
73+
* @returns A promise containing the list.
74+
*/
75+
declare function pidtree(
76+
pid: string | number,
77+
options: PidTree.Options & {advanced: true}
78+
): Promise<PidTree.AdvancedResult[]>;
79+
80+
export = pidtree;

index.test-d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {expectType} from 'tsd';
2+
3+
import pidtree = require('.');
4+
5+
(async () => {
6+
expectType<number[]>(await pidtree(1));
7+
expectType<number[]>(await pidtree('1'));
8+
expectType<Array<{ppid: number; pid: number}>>(
9+
await pidtree(1, {advanced: true})
10+
);
11+
expectType<Array<{ppid: number; pid: number}>>(
12+
await pidtree('1', {advanced: true})
13+
);
14+
})();

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
"processes"
2929
],
3030
"main": "index.js",
31+
"types": "index.d.ts",
3132
"bin": {
3233
"pidtree": "./bin/pidtree.js"
3334
},
3435
"files": [
3536
"bin",
3637
"lib",
37-
"index.js"
38+
"index.js",
39+
"index.d.ts"
3840
],
3941
"engines": {
4042
"node": ">=0.10"
@@ -45,6 +47,7 @@
4547
"release": "np",
4648
"lint": "xo",
4749
"test": "nyc ava -m \"!*benchmark*\"",
50+
"types": "tsd",
4851
"bench": "ava -m \"*benchmark*\"",
4952
"coverage": "codecov"
5053
},
@@ -60,6 +63,7 @@
6063
"through": "^2.3.8",
6164
"time-span": "^2.0.0",
6265
"tree-kill": "^1.1.0",
66+
"tsd": "^0.11.0",
6367
"xo": "~0.20.3"
6468
},
6569
"ava": {

0 commit comments

Comments
 (0)