Skip to content

Update to use ESM throughout #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 2 additions & 4 deletions bin/pidtree.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/usr/bin/env node

'use strict';

var os = require('os');
var pidtree = require('..');
import os from 'os';
import { pidtree } from '../index.js';

// The method startsWith is not defined on string objects in node 0.10
// eslint-disable-next-line no-extend-native
Expand Down
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
import { pidtreeCallback } from './lib/pidtree.js';

function pify(fn, arg1, arg2) {
return new Promise(function(resolve, reject) {
Expand All @@ -18,8 +18,6 @@ if (!String.prototype.startsWith) {
};
}

var pidtree = require('./lib/pidtree');

/**
* Get the list of children pids of the given pid.
* @public
Expand All @@ -32,18 +30,16 @@ var pidtree = require('./lib/pidtree');
* provided a promise is returned instead.
* @returns {Promise.<Object[]>} Only when the callback is not provided.
*/
function list(pid, options, callback) {
export function pidtree(pid, options, callback) {
if (typeof options === 'function') {
callback = options;
options = undefined;
}

if (typeof callback === 'function') {
pidtree(pid, options, callback);
pidtreeCallback(pid, options, callback);
return;
}

return pify(pidtree, pid, options);
return pify(pidtreeCallback, pid, options);
}

module.exports = list;
10 changes: 3 additions & 7 deletions lib/bin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

var spawn = require('child_process').spawn;
import child_process from 'child_process';

function stripStderr(stderr) {
if (!stderr) return;
Expand All @@ -20,14 +18,14 @@ function stripStderr(stderr) {
* @param {Object} [options] Optional option for the spawn function.
* @param {Function} done(err, stdout)
*/
function run(cmd, args, options, done) {
export function run(cmd, args, options, done) {
if (typeof options === 'function') {
done = options;
options = undefined;
}

var executed = false;
var ch = spawn(cmd, args, options);
var ch = child_process.spawn(cmd, args, options);
var stdout = '';
var stderr = '';

Expand Down Expand Up @@ -57,5 +55,3 @@ function run(cmd, args, options, done) {
done(null, stdout, code);
});
}

module.exports = run;
14 changes: 5 additions & 9 deletions lib/get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

var os = require('os');
import os from 'os';

var platformToMethod = {
darwin: 'ps',
Expand All @@ -13,8 +11,8 @@ var platformToMethod = {
};

var methodToRequireFn = {
ps: () => require('./ps'),
wmic: () => require('./wmic'),
ps: async () => (await import('./ps.js')).ps,
wmic: async () => (await import('./wmic.js')).wmic,
};

var platform = os.platform();
Expand All @@ -28,7 +26,7 @@ var method = platformToMethod[platform];
* Gets the list of all the pids of the system.
* @param {Function} callback Called when the list is ready.
*/
function get(callback) {
export async function get(callback) {
if (method === undefined) {
callback(
new Error(
Expand All @@ -38,8 +36,6 @@ function get(callback) {
);
}

var list = methodToRequireFn[method]();
var list = await methodToRequireFn[method]();
list(callback);
}

module.exports = get;
10 changes: 3 additions & 7 deletions lib/pidtree.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

var getAll = require('./get');
import { get } from './get.js';

/**
* Get the list of children and grandchildren pids of the given PID.
Expand All @@ -11,7 +9,7 @@ var getAll = require('./get');
* format {pid: X, ppid: Y}.
* @param {Function} callback(err, list) Called when the list is ready.
*/
function list(PID, options, callback) {
export function pidtreeCallback(PID, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
Expand All @@ -27,7 +25,7 @@ function list(PID, options, callback) {
return;
}

getAll(function(err, list) {
get(function(err, list) {
if (err) {
callback(err);
return;
Expand Down Expand Up @@ -100,5 +98,3 @@ function list(PID, options, callback) {
callback(null, pids);
});
}

module.exports = list;
11 changes: 4 additions & 7 deletions lib/ps.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';
import os from 'os';

var os = require('os');
var bin = require('./bin');
import { run } from './bin.js';

/**
* Gets the list of all the pids of the system through the ps command.
* @param {Function} callback(err, list)
*/
function ps(callback) {
export function ps(callback) {
var args = ['-A', '-o', 'ppid,pid'];

bin('ps', args, function(err, stdout, code) {
run('ps', args, function(err, stdout, code) {
if (err) return callback(err);
if (code !== 0) {
return callback(new Error('pidtree ps command exited with code ' + code));
Expand Down Expand Up @@ -43,5 +42,3 @@ function ps(callback) {
}
});
}

module.exports = ps;
11 changes: 4 additions & 7 deletions lib/wmic.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use strict';
import os from 'os';

var os = require('os');
var bin = require('./bin');
import { run } from './bin.js';

/**
* Gets the list of all the pids of the system through the wmic command.
* @param {Function} callback(err, list)
*/
function wmic(callback) {
export function wmic(callback) {
var args = ['PROCESS', 'get', 'ParentProcessId,ProcessId'];
var options = {windowsHide: true, windowsVerbatimArguments: true};
bin('wmic', args, options, function(err, stdout, code) {
run('wmic', args, options, function(err, stdout, code) {
if (err) {
callback(err);
return;
Expand Down Expand Up @@ -45,5 +44,3 @@ function wmic(callback) {
}
});
}

module.exports = wmic;
38 changes: 17 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pidtree",
"version": "0.6.0",
"version": "1.0.0",
"description": "Cross platform children list of a PID",
"license": "MIT",
"homepage": "http://github.com/simonepri/pidtree#readme",
Expand All @@ -11,7 +11,8 @@
},
"author": "Simone Primarosa <[email protected]> (https://github.com/simonepri)",
"contributors": [
"Simone Primarosa <[email protected]> (https://github.com/simonepri)"
"Simone Primarosa <[email protected]> (https://github.com/simonepri)",
"Gavin Aiken <[email protected]> (https://github.com/gavinaiken)"
],
"keywords": [
"ps-tree",
Expand All @@ -28,6 +29,7 @@
"processes"
],
"main": "index.js",
"type": "module",
"types": "index.d.ts",
"bin": {
"pidtree": "./bin/pidtree.js"
Expand All @@ -39,41 +41,35 @@
"index.d.ts"
],
"engines": {
"node": ">=0.10"
"node": ">=16.0.0"
},
"scripts": {
"start": "node ./bin/pidtree.js",
"update": "npm-check -u",
"release": "np",
"lint": "xo",
"test": "nyc ava -m \"!*benchmark*\"",
"test": "c8 ava -m \"!*benchmark*\"",
"test:windows": "ava -m \"!*benchmark*\"",
"types": "tsd",
"bench": "ava -m \"*benchmark*\""
},
"devDependencies": {
"ava": "~0.25.0",
"mockery": "^2.1.0",
"np": "^2.20.1",
"npm-check": "^5.9.2",
"nyc": "^11.6.0",
"pify": "^3.0.0",
"string-to-stream": "^1.1.0",
"ava": "~3.15.0",
"c8": "^10.1.3",
"np": "^10.2.0",
"npm-check": "^6.0.1",
"pify": "^6.1.0",
"sinon": "^19.0.2",
"string-to-stream": "^3.0.1",
"through": "^2.3.8",
"time-span": "^2.0.0",
"tree-kill": "^1.1.0",
"tsd": "^0.11.0",
"xo": "~0.20.3"
"time-span": "^5.1.0",
"tree-kill": "^1.2.2",
"tsd": "^0.31.2",
"xo": "~0.60.0"
},
"ava": {
"verbose": true
},
"nyc": {
"reporter": [
"lcovonly",
"text"
]
},
"xo": {
"prettier": true,
"space": true,
Expand Down
2 changes: 1 addition & 1 deletion test/bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';

import tspan from 'time-span';

import pidtree from '..';
import { pidtree } from '../index.js';

async function execute(pid, times) {
const end = tspan();
Expand Down
2 changes: 0 additions & 2 deletions test/helpers/exec/child.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

var started = false;
setInterval(function() {
if (started) return;
Expand Down
6 changes: 2 additions & 4 deletions test/helpers/exec/parent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

var path = require('path');
var cp = require('child_process');
import cp from 'child_process';
import path from 'path';

var started = false;
var spawned = {};
Expand Down
22 changes: 13 additions & 9 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import test from 'ava';
import pify from 'pify';
import treek from 'tree-kill';

import pidtree from '..';
import { pidtree } from '../index.js';

const dirname = path.dirname(new URL(import.meta.url).pathname);

const scripts = {
parent: path.join(__dirname, 'helpers', 'exec', 'parent.js'),
child: path.join(__dirname, 'helpers', 'exec', 'child.js'),
parent: path.join(dirname, 'helpers', 'exec', 'parent.js'),
child: path.join(dirname, 'helpers', 'exec', 'child.js'),
};

test('should work with a single pid', async t => {
let result = await pidtree(-1, {advanced: true});
t.log(result);
// t.log(result);

t.true(Array.isArray(result));
result.forEach((p, i) => {
i = i.toString();
t.is(typeof p, 'object', i);
t.is(typeof p.ppid, 'number', i);
t.false(isNaN(p.ppid), i);
Expand All @@ -30,6 +33,7 @@ test('should work with a single pid', async t => {

t.true(Array.isArray(result));
result.forEach((p, i) => {
i = i.toString();
t.is(typeof p, 'number', i);
t.false(isNaN(p), i);
});
Expand Down Expand Up @@ -109,18 +113,18 @@ test('show include the root if the root option is passsed', async t => {
});

test('should throw an error if an invalid pid is provided', async t => {
let err = await t.throws(pidtree(null));
let err = await t.throwsAsync(pidtree(null));
t.is(err.message, 'The pid provided is invalid');
err = await t.throws(pidtree([]));
err = await t.throwsAsync(pidtree([]));
t.is(err.message, 'The pid provided is invalid');
err = await t.throws(pidtree('invalid'));
err = await t.throwsAsync(pidtree('invalid'));
t.is(err.message, 'The pid provided is invalid');
err = await t.throws(pidtree(-2));
err = await t.throwsAsync(pidtree(-2));
t.is(err.message, 'The pid provided is invalid');
});

test('should throw an error if the pid does not exists', async t => {
const err = await t.throws(pidtree(65535));
const err = await t.throwsAsync(pidtree(65535));
t.is(err.message, 'No matching pid found');
});

Expand Down
Loading