11#!/usr/bin/env node
22
3- 'use strict' ;
4-
5- var os = require ( 'os' ) ;
6- var pidtree = require ( '..' ) ;
7-
8- // The method startsWith is not defined on string objects in node 0.10
9- // eslint-disable-next-line no-extend-native
10- String . prototype . startsWith = function ( suffix ) {
11- return this . substring ( 0 , suffix . length ) === suffix ;
12- } ;
3+ import os from 'node:os' ;
4+ import pidtree from '../index.js' ;
135
146function help ( ) {
15- var help =
7+ console . log (
168 ' Usage\n' +
17- ' $ pidtree <ppid>\n' +
18- '\n' +
19- 'Options\n' +
20- ' --list To print the pids as a list.\n' +
21- '\n' +
22- 'Examples\n' +
23- ' $ pidtree\n' +
24- ' $ pidtree --list\n' +
25- ' $ pidtree 1\n' +
26- ' $ pidtree 1 --list\n' ;
27- console . log ( help ) ;
9+ ' $ pidtree <ppid>\n' +
10+ '\n' +
11+ 'Options\n' +
12+ ' --list To print the pids as a list.\n' +
13+ '\n' +
14+ 'Examples\n' +
15+ ' $ pidtree\n' +
16+ ' $ pidtree --list\n' +
17+ ' $ pidtree 1\n' +
18+ ' $ pidtree 1 --list\n' ,
19+ ) ;
2820}
2921
3022function list ( ppid ) {
31- pidtree ( ppid === undefined ? - 1 : ppid , function ( err , list ) {
32- if ( err ) {
33- console . error ( err . message ) ;
23+ pidtree ( ppid === undefined ? - 1 : ppid , ( error , list ) => {
24+ if ( error ) {
25+ console . error ( error . message ) ;
3426 return ;
3527 }
3628
@@ -39,16 +31,16 @@ function list(ppid) {
3931}
4032
4133function tree ( ppid ) {
42- pidtree ( ppid , { advanced : true } , function ( err , list ) {
43- if ( err ) {
44- console . error ( err . message ) ;
34+ pidtree ( ppid , { advanced : true } , ( error , list ) => {
35+ if ( error ) {
36+ console . error ( error . message ) ;
4537 return ;
4638 }
4739
48- var parents = { } ; // Hash Map of parents
49- var tree = { } ; // Adiacency Hash Map
40+ const parents = { } ; // Hash Map of parents
41+ const tree = { } ; // Adjacency Hash Map
5042 while ( list . length > 0 ) {
51- var element = list . pop ( ) ;
43+ const element = list . pop ( ) ;
5244 if ( tree [ element . ppid ] ) {
5345 tree [ element . ppid ] . push ( element . pid ) ;
5446 } else {
@@ -60,55 +52,53 @@ function tree(ppid) {
6052 }
6153 }
6254
63- var roots = [ ppid ] ;
55+ let roots = [ ppid ] ;
6456 if ( ppid === - 1 ) {
65- // Get all the roots
66- roots = Object . keys ( tree ) . filter ( function ( node ) {
67- return parents [ node ] === undefined ;
68- } ) ;
57+ // Get all the roots.
58+ roots = Object . keys ( tree ) . filter ( ( node ) => parents [ node ] === undefined ) ;
6959 }
7060
71- roots . forEach ( function ( root ) {
61+ for ( const root of roots ) {
7262 print ( tree , root ) ;
73- } ) ;
63+ }
7464 } ) ;
7565
7666 function print ( tree , start ) {
7767 function printBranch ( node , branch ) {
78- var isGraphHead = branch . length === 0 ;
79- var children = tree [ node ] || [ ] ;
68+ const isGraphHead = branch . length === 0 ;
69+ const children = tree [ node ] || [ ] ;
8070
81- var branchHead = '' ;
71+ let branchHead = '' ;
8272 if ( ! isGraphHead ) {
8373 branchHead = children . length > 0 ? '┬ ' : '─ ' ;
8474 }
8575
8676 console . log ( branch + branchHead + node ) ;
8777
88- var baseBranch = branch ;
78+ let baseBranch = branch ;
8979 if ( ! isGraphHead ) {
90- var isChildOfLastBranch = branch . slice ( - 2 ) === '└─' ;
80+ const isChildOfLastBranch = branch . slice ( - 2 ) === '└─' ;
9181 baseBranch = branch . slice ( 0 , - 2 ) + ( isChildOfLastBranch ? ' ' : '| ' ) ;
9282 }
9383
94- var nextBranch = baseBranch + '├─' ;
95- var lastBranch = baseBranch + '└─' ;
96- children . forEach ( function ( child , index ) {
84+ const nextBranch = baseBranch + '├─' ;
85+ const lastBranch = baseBranch + '└─' ;
86+ for ( const [ index , child ] of children . entries ( ) ) {
9787 printBranch (
9888 child ,
99- children . length - 1 === index ? lastBranch : nextBranch
89+ children . length - 1 === index ? lastBranch : nextBranch ,
10090 ) ;
101- } ) ;
91+ }
10292 }
10393
10494 printBranch ( start , '' ) ;
10595 }
10696}
10797
10898function run ( ) {
109- var flag ;
110- var ppid ;
111- for ( var i = 2 ; i < process . argv . length ; i ++ ) {
99+ let flag ;
100+ let ppid ;
101+ for ( let i = 2 ; i < process . argv . length ; i ++ ) {
112102 if ( process . argv [ i ] . startsWith ( '--' ) ) {
113103 flag = process . argv [ i ] ;
114104 } else {
@@ -120,9 +110,13 @@ function run() {
120110 ppid = - 1 ;
121111 }
122112
123- if ( flag === '--list' ) list ( ppid ) ;
124- else if ( flag === undefined ) tree ( ppid ) ;
125- else help ( ) ;
113+ if ( flag === '--list' ) {
114+ list ( ppid ) ;
115+ } else if ( flag === undefined ) {
116+ tree ( ppid ) ;
117+ } else {
118+ help ( ) ;
119+ }
126120}
127121
128122run ( ) ;
0 commit comments