Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ on:
jobs:
lint:
runs-on: ubuntu-latest
name: XO & Prettier
steps:
- name: Setup repo
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
# Pinned: the legacy xo@0.20 toolchain crashes on modern Node
# (util.isDate was removed). Revisit when the toolchain is updated.
node-version: 14
- name: Install dev dependencies
run: |
npm install --only=dev
npm list --dev --depth=0
node-version: 22
- name: Install dependencies
run: npm install
- name: Run lint
run: npm run lint
28 changes: 8 additions & 20 deletions .github/workflows/test-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,20 @@ on:
jobs:
test:
runs-on: macos-latest
name: AVA & TSD & Benchmark & Codecov
strategy:
fail-fast: false
# Node 26+ is not yet supported by the coverage tool (c8); revisit
# "current" once the toolchain catches up.
matrix:
node: [current, 22, 20, 18]
node: [24, 22, 20, 18]
steps:
- name: Setup repo
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup node ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- name: Install lib dependencies
run: |
npm install --only=prod
npm list --prod --depth=0
- name: Install dev dependencies
run: |
npm install --only=dev
npm list --dev --depth=0
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test
#- name: Run type checking
# run: npm run types
- name: Run benchmark
run: |
npm run bench
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
run: npm test
30 changes: 12 additions & 18 deletions .github/workflows/test-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,26 @@ on:
jobs:
test:
runs-on: ubuntu-latest
name: AVA & TSD & Benchmark & Codecov
strategy:
fail-fast: false
# Node 26+ is not yet supported by the coverage tool (c8); revisit
# "current" once the toolchain catches up.
matrix:
node: [current, 22, 20, 18]
node: [24, 22, 20, 18]
steps:
- name: Setup repo
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup node ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- name: Install lib dependencies
run: |
npm install --only=prod
npm list --prod --depth=0
- name: Install dev dependencies
run: |
npm install --only=dev
npm list --dev --depth=0
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test
#- name: Run type checking
# run: npm run types
run: npm test
- name: Run type checking
run: npm run types
- name: Run benchmark
run: |
npm run bench
run: npm run bench
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v5
33 changes: 8 additions & 25 deletions .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,20 @@ jobs:
# windows-latest is Windows Server 2025, which ships without wmic, so the
# integration tests here run against the PowerShell fallback in lib/get.js.
runs-on: windows-latest
name: AVA & TSD & Benchmark & Codecov
strategy:
fail-fast: false
# Node 26+ is not yet supported by the coverage tool (c8); revisit
# "current" once the toolchain catches up.
matrix:
node: [current, 22, 20, 18]
node: [24, 22, 20, 18]
steps:
- name: Setup repo
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup node ${{ matrix.node }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- name: Install lib dependencies
run: |
npm install --only=prod
npm list --prod --depth=0
- name: Install dev dependencies
run: |
npm install --only=dev
npm list --dev --depth=0
- name: Install dependencies
run: npm install
- name: Run tests
if: ${{ matrix.node <= 6 }}
run: npm run test
- name: Run tests
if: ${{ !(matrix.node <= 6) }}
run: npm run test:windows
#- name: Run type checking
# run: npm run types
- name: Run benchmark
run: |
npm run bench
- name: Upload coverage to Codecov
if: ${{ matrix.node <= 6 }}
uses: codecov/codecov-action@v2
run: npm test
102 changes: 48 additions & 54 deletions bin/pidtree.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
#!/usr/bin/env node

'use strict';

var os = require('os');
var pidtree = require('..');

// 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;
};
import os from 'node:os';
import pidtree from '../index.js';

function help() {
var help =
console.log(
' 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);
' $ 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',
);
}

function list(ppid) {
pidtree(ppid === undefined ? -1 : ppid, function(err, list) {
if (err) {
console.error(err.message);
pidtree(ppid === undefined ? -1 : ppid, (error, list) => {
if (error) {
console.error(error.message);
return;
}

Expand All @@ -39,16 +31,16 @@ function list(ppid) {
}

function tree(ppid) {
pidtree(ppid, {advanced: true}, function(err, list) {
if (err) {
console.error(err.message);
pidtree(ppid, {advanced: true}, (error, list) => {
if (error) {
console.error(error.message);
return;
}

var parents = {}; // Hash Map of parents
var tree = {}; // Adiacency Hash Map
const parents = {}; // Hash Map of parents
const tree = {}; // Adjacency Hash Map
while (list.length > 0) {
var element = list.pop();
const element = list.pop();
if (tree[element.ppid]) {
tree[element.ppid].push(element.pid);
} else {
Expand All @@ -60,55 +52,53 @@ function tree(ppid) {
}
}

var roots = [ppid];
let roots = [ppid];
if (ppid === -1) {
// Get all the roots
roots = Object.keys(tree).filter(function(node) {
return parents[node] === undefined;
});
// Get all the roots.
roots = Object.keys(tree).filter((node) => parents[node] === undefined);
}

roots.forEach(function(root) {
for (const root of roots) {
print(tree, root);
});
}
});

function print(tree, start) {
function printBranch(node, branch) {
var isGraphHead = branch.length === 0;
var children = tree[node] || [];
const isGraphHead = branch.length === 0;
const children = tree[node] || [];

var branchHead = '';
let branchHead = '';
if (!isGraphHead) {
branchHead = children.length > 0 ? '┬ ' : '─ ';
}

console.log(branch + branchHead + node);

var baseBranch = branch;
let baseBranch = branch;
if (!isGraphHead) {
var isChildOfLastBranch = branch.slice(-2) === '└─';
const isChildOfLastBranch = branch.slice(-2) === '└─';
baseBranch = branch.slice(0, -2) + (isChildOfLastBranch ? ' ' : '| ');
}

var nextBranch = baseBranch + '├─';
var lastBranch = baseBranch + '└─';
children.forEach(function(child, index) {
const nextBranch = baseBranch + '├─';
const lastBranch = baseBranch + '└─';
for (const [index, child] of children.entries()) {
printBranch(
child,
children.length - 1 === index ? lastBranch : nextBranch
children.length - 1 === index ? lastBranch : nextBranch,
);
});
}
}

printBranch(start, '');
}
}

function run() {
var flag;
var ppid;
for (var i = 2; i < process.argv.length; i++) {
let flag;
let ppid;
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i].startsWith('--')) {
flag = process.argv[i];
} else {
Expand All @@ -120,9 +110,13 @@ function run() {
ppid = -1;
}

if (flag === '--list') list(ppid);
else if (flag === undefined) tree(ppid);
else help();
if (flag === '--list') {
list(ppid);
} else if (flag === undefined) {
tree(ppid);
} else {
help();
}
}

run();
Loading
Loading