diff --git a/index.js b/index.js index 6225d04..aeed808 100755 --- a/index.js +++ b/index.js @@ -48,7 +48,7 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) { var processLister; if (process.platform === 'win32') { // See also: https://github.com/nodejs/node-v0.x-archive/issues/2318 - processLister = spawn('wmic.exe', ['PROCESS', 'GET', 'Name,ProcessId,ParentProcessId,Status,WorkingSetSize']); + processLister = spawn('wmic.exe', ['PROCESS', 'GET', 'Name,ProcessId,ParentProcessId,WorkingSetSize']); } else { processLister = spawn('ps', ['-A', '-o', 'ppid,pid,stat,comm,rss']); } @@ -72,8 +72,11 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) { } // Convert RSS to number of bytes - columns[4] = parseInt(columns[4], 10); - if (process.platform !== 'win32') { + if (process.platform == 'win32') { + columns[3] = parseInt(columns[3], 10); + } + else { + columns[4] = parseInt(columns[4], 10); columns[4] *= 1024; } @@ -84,6 +87,13 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) { row[h.shift()] = h.length ? columns.shift() : columns.join(' '); } + // For Windows, WMIC.exe never returns any value for "Status" which used to get value corresponding to "STAT" + // See: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process?redirectedfrom=MSDN + // So just set "null" for the backward compatibility. + if (process.platform == 'win32') { + row['STAT'] = null; + } + return cb(null, row); }), es.writeArray(function (err, ps) { @@ -122,19 +132,14 @@ function normalizeHeader(str) { switch (str) { case 'Name': return 'COMMAND'; - break; case 'ParentProcessId': return 'PPID'; - break; case 'ProcessId': return 'PID'; - break; case 'Status': return 'STAT'; - break; case 'WorkingSetSize': return 'RSS'; - break; default: throw new Error('Unknown process listing header: ' + str); } diff --git a/test/test.js b/test/test.js index 0ebb5f4..30b5e4c 100644 --- a/test/test.js +++ b/test/test.js @@ -9,6 +9,9 @@ var red = chalk.red, green = chalk.green, cyan = chalk.cyan; +var isWin = process.platform === 'win32'; +var isNumGreaterThanZero = (n) => !isNaN(parseInt(n, 10)) && n > 0; + var scripts = { parent: path.join(__dirname, 'exec', 'parent.js'), child: path.join(__dirname, 'exec', 'child.js') @@ -65,9 +68,11 @@ test(cyan('Includes itself it includeRoot is true'), function (t) { }; t.equal(current.PID, '' + process.pid, green('✓ Current PID is valid')); - t.equal(current.COMMAND, 'node', green('✓ Current COMM is node')); + t.equal(current.COMMAND, isWin ? 'node.exe' : 'node', green('✓ Current COMM is node')); + t.equal(isNumGreaterThanZero(current.RSS), true, green('✓ Current RSS is valid')); t.notEqual(other.PID, '' + process.pid, green('✓ Current PID is valid')); - t.equal(other.COMMAND, 'ps', green('✓ Current COMM is ps')); + t.equal(other.COMMAND, isWin ? 'WMIC.exe' : 'ps', green('✓ Current COMM is ps')); + t.equal(isNumGreaterThanZero(other.RSS), true, green('✓ Other RSS is valid')); t.end(); }); });