Skip to content
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

Add verbose option #7

Merged
merged 6 commits into from
May 1, 2017
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
7 changes: 4 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '0.12'
- nodejs_version: '0.10'
- nodejs_version: '4.8.2'
- nodejs_version: '5.12.0'
- nodejs_version: '6.10.2'
- nodejs_version: '7.9.0'
install:
- ps: Install-Product node $env:nodejs_version
- set CI=true
Expand Down
82 changes: 40 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,54 @@
'use strict';
var childProcess = require('child_process');
var neatCsv = require('neat-csv');
var sec = require('sec');
var pify = require('pify');
var Promise = require('pinkie-promise');

module.exports = function (opts) {
const childProcess = require('child_process');
const pify = require('pify');
const neatCsv = require('neat-csv');
const sec = require('sec');

module.exports = opts => {
if (process.platform !== 'win32') {
return Promise.reject(new Error('Windows only'));
}

opts = opts || {};

var args = ['/v', '/nh', '/fo', 'CSV'];
const args = ['/v', '/nh', '/fo', 'csv'];

if (opts.system && opts.username && opts.password) {
args.push('/s', opts.system, '/u', opts.username, '/p', opts.password);
}

if (Array.isArray(opts.filter) && opts.filter.length) {
opts.filter.forEach(function (el) {
args.push('/fi', el);
});
if (Array.isArray(opts.filter)) {
for (const filter of opts.filter) {
args.push('/fi', filter);
}
}

return pify(childProcess.execFile, Promise)('tasklist', args).then(function (stdout) {
return pify(neatCsv, Promise)(stdout, {
headers: [
'imageName',
'pid',
'sessionName',
'sessionNumber',
'memUsage',
'status',
'username',
'cpuTime',
'windowTitle'
]
}).then(function (data) {
return data.map(function (el) {
Object.keys(el).forEach(function (key) {
if (el[key] === 'N/A') {
el[key] = null;
}
});

el.pid = Number(el.pid);
el.sessionNumber = Number(el.sessionNumber);
el.memUsage = Number(el.memUsage.replace(/[^\d]/g, '')) * 1024;
el.cpuTime = sec(el.cpuTime);
return el;
});
});
});
const defaultHeaders = [
'imageName',
'pid',
'sessionName',
'sessionNumber',
'memUsage'
];

const verboseHeaders = defaultHeaders.concat([
'status',
'username',
'cpuTime',
'windowTitle'
]);

const headers = opts.verbose ? verboseHeaders : defaultHeaders;

return pify(childProcess.execFile)('tasklist', args)
.then(stdout => neatCsv(stdout, {headers}))
.then(data => data.map(task => {
// Normalize task props
task.pid = Number(task.pid);
task.sessionNumber = Number(task.sessionNumber);
task.memUsage = Number(task.memUsage.replace(/[^\d]/g, '')) * 1024;
if (opts.verbose) {
task.cpuTime = sec(task.cpuTime);
}
return task;
}));
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -31,7 +31,7 @@
"services"
],
"dependencies": {
"neat-csv": "^1.0.0",
"neat-csv": "^2.1.0",
"pify": "^2.2.0",
"pinkie-promise": "^2.0.0",
"sec": "^1.0.0"
Expand Down
53 changes: 49 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ tasklist().then(data => {
sessionName: 'Console',
sessionNumber: 1,
memUsage: 4415488, // bytes
status: 'Running',
username: 'SINDRESORHU3930\\sindre'
cpuTime: 0, // seconds
windowTitle: 'Task Host Window'
}, ...]
*/
});
Expand All @@ -51,6 +47,55 @@ Type: `object`

The `system`, `username`, `password` options must be specified together.

##### verbose

Type: `boolean`
Default: `false`

Return verbose results.

Without the `verbose` option, `taskkill` returns tasks with the following properties:

- `imageName` (Type: `string`)
- `pid` (Type: `number`)
- `sessionName` (Type: `string`)
- `sessionNumber` (Type: `number`)
- `memUsage` in bytes (Type: `number`)

With the `verbose` option set to `true`, it additionally returns the following properties:

- `status` (Type: `string`): one of `Running`, `Suspended`, `Not Responding`, or `Unknown`
- `username` (Type: `string`)
- `cpuTime` in seconds (Type: `number`)
- `windowTitle` (Type: `string`)

**Note:** It is not guaranteed that the `username` and `windowTitle` properties are returned with proper values. If they are *not available*, `'N/A'` may be returned on English Windows systems. (In contrast, `'Nicht zutreffend'` may be returned on German Windows systems, for example.)

**Verbose Example:**

```js
const tasklist = require('tasklist');

tasklist({verbose: true}).then(data => {
console.log(data);
/*
[{
imageName: 'taskhostex.exe',
pid: 1820,
sessionName: 'Console',
sessionNumber: 1,
memUsage: 4415488, // bytes
status: 'Running',
username: 'SINDRESORHU3930\\sindre'
cpuTime: 0, // seconds
windowTitle: 'Task Host Window'
}, ...]
*/
});
```

**Warning:** Using the `verbose` option may have a considerable performance impact (see: [/issues/6](https://github.com/sindresorhus/tasklist/issues/6)).

##### system

Type: `string`
Expand Down
53 changes: 36 additions & 17 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
import test from 'ava';
import fn from './';
import tasklist from './';

test('main', async t => {
const data = await fn();
const hasDefaultTaskProps = (t, task) => {
t.is(typeof task.imageName, 'string');
t.is(typeof task.pid, 'number');
t.is(typeof task.sessionName, 'string');
t.is(typeof task.sessionNumber, 'number');
t.is(typeof task.memUsage, 'number');
};

t.true(data.length > 0);
const d = data[0];
t.true(d.imageName.length > 0);
t.is(typeof d.pid, 'number');
t.is(typeof d.memUsage, 'number');
});
const hasNonVerboseTaskProps = (t, task) => {
t.is(task.status, undefined);
t.is(task.username, undefined);
t.is(task.cpuTime, undefined);
t.is(task.windowTitle, undefined);
};

test('filter option', async t => {
const data = await fn({filter: ['status ne running']});
const hasVerboseTaskProps = (t, task) => {
t.is(typeof task.status, 'string');
t.is(typeof task.username, 'string');
t.is(typeof task.cpuTime, 'number');
t.is(typeof task.windowTitle, 'string');
};

t.true(data.length > 0);
const d = data[0];
t.true(d.imageName.length > 0);
t.is(typeof d.pid, 'number');
t.is(typeof d.memUsage, 'number');
});
const macro = async (t, options) => {
const tasks = await tasklist(options);
t.true(tasks.length > 0);
for (const task of tasks) {
hasDefaultTaskProps(t, task);
if (options.verbose) {
hasVerboseTaskProps(t, task);
} else {
hasNonVerboseTaskProps(t, task);
}
}
};

test('default', macro, {});
test('verbose option', macro, {verbose: true});
test('filter option', macro, {filter: ['status eq running', 'username ne F4k3U53RN4M3']});