Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# wintools - Some windows tools for node.js #
# wintools - Some windows tools for node.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double # also works BTW...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jus copied this from your own version in role repository, that looked to me more updated :)


From: Elad Ben-Israel [reply@reply.github.com]
Sent: Wednesday, March 14, 2012 10:36 PM
To: Yosef Dinerstein
Subject: Re: [node-wintools] Parse XML output of WMIC. (#1)

@@ -1,15 +1,23 @@
-# wintools - Some windows tools for node.js #

The double # also works BTW...


Reply to this email directly or view it on GitHub:
https://github.com/anodejs/node-wintools/pull/1/files#r559384

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Its a hack...

Sent from my iPhone

On Mar 14, 2012, at 23:55, "Yosef Dinerstein" reply@reply.github.com wrote:

@@ -1,15 +1,23 @@
-# wintools - Some windows tools for node.js #

Jus copied this from your own version in role repository, that looked to me more updated :)


From: Elad Ben-Israel [reply@reply.github.com]
Sent: Wednesday, March 14, 2012 10:36 PM
To: Yosef Dinerstein
Subject: Re: [node-wintools] Parse XML output of WMIC. (#1)

@@ -1,15 +1,23 @@
-# wintools - Some windows tools for node.js #

The double # also works BTW...


Reply to this email directly or view it on GitHub:
https://github.com/anodejs/node-wintools/pull/1/files#r559384


Reply to this email directly or view it on GitHub:
https://github.com/anodejs/node-wintools/pull/1/files#r560322


## Installation

```bash
npm install wintools
```

* __ps(callback)__ where callback is `function(err, list)` returns a list of running processes.
* __kill.pid(pid, callback)__ kills a process by PID.
* __kill.image(imageName, callback)__ kills a process by image name (e.g. `node.exe`).
* __shutdown.poweroff([callback])__ turns of off the machine immediately.
* __shutdown.restart([callback])__ rstarts the machine immediately.
```javascript
var wintools = require('wintools');
```

## API

* `wintools.ps(filter, callback)` where filter is a filter function `function(p)` executed on each process before pushing it to the list and callback is `function(err, list)` returns a list of running processes.
* `wintools.kill.pid(pid, callback)` kills a process by PID.
* `wintools.kill.image(imageName, callback)` kills a process by image name (e.g. `node.exe`).
* `wintools.shutdown.poweroff([callback])` turns of off the machine immediately.
* `wintools.shutdown.restart([callback])` rstarts the machine immediately.

## License ##
## License

MIT
82 changes: 45 additions & 37 deletions lib/kill.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
var exec = require('child_process').exec;

var exec = require('child_process').exec;
/**
* Kills a process by PID.
* @param pid Required. Process ID.
* @param callback Optional.
* @remarks Windows only
*/
exports.pid = function(pid, callback) {
if (!pid) throw new Error('pid is required');
if (!callback) callback = function() {};
exec('taskkill /t /f /pid ' + pid.toString(), function (err, stdout, stderr) {
if (err) {
callback({ msg: "unable to kill " + pid, err: err, stdout: stdout, stderr: stderr });
return;
}
callback();
});
};

* Kills a process by PID.
* @param pid Required. Process ID.
* @param callback Optional.
* @remarks Windows only
*/
exports.pid = function (pid, callback) {
if (!pid) throw new Error('pid is required');
if (!callback) callback = function () { };
exec('taskkill /t /f /pid ' + pid.toString(), function (err, stdout, stderr) {
if (err) {
callback({ msg: "unable to kill " + pid, err: err, stdout: stdout, stderr: stderr });
return;
}
callback();
});
};
/**
* Kills all the processes with the specified image name
* @param imageName Required. The name of the image (e.g. 'node.exe')
* @param callback Optional.
*/
exports.image = function(imageName, callback) {
if (!imageName) throw new Error('imageName is required');
if (!callback) callback = function() {};

exec('taskkill /t /f /im ' + imageName, function (err, stdout, stderr) {
if (err) {
callback({ msg: "unable to kill " + imageName, err: err, stdout: stdout, stderr: stderr });
return;
}

callback();
});
* Kills all the processes with the specified image name
* @param imageName Required. The name of the image (e.g. 'node.exe')
* @param callback Optional.
*/
exports.image = function (imageName, opts, callback) {
if (!imageName) throw new Error('imageName is required');

if (typeof opts === 'function') {
callback = opts;
opts = '';
}

if (!callback) callback = function () { };

var cmd = ('taskkill /t /f /im ' + imageName + ' ' + opts).trim();
console.log('cmd:', cmd);
exec(cmd, function (err, stdout, stderr) {
if (err) {
callback({ msg: "unable to kill " + imageName, err: err, stdout: stdout, stderr: stderr });
return;
}

callback();
});
}
104 changes: 58 additions & 46 deletions lib/ps.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,60 @@
var exec = require('child_process').exec;

var spawn = require('child_process').spawn;
var xml2js = require('xml2js');

/**
* Returns all the system processes
* @param callback {function(err, list)} Called with the list of all processes.
* @remarks Runs only on Windows (uses WMI)
*/
module.exports = function(callback) {
if (!callback) callback = function(err, list) { };

exec('wmic process list /format:csv', function (err, stdout, stderr) {
if (err) {
callback({ err: err, msg: "unable to enumerate processes" });
return;
}

stdout = stdout.replace(/\r/g, '').split('\n').slice(1);
fields = stdout.shift().split(',');

var output = {};
stdout.forEach(function (line) {

var parts = line.split(',');
var entry = {};
for (var i = 0; i < fields.length; ++i) {
entry[fields[i]] = parts[i];
}

var e = {
pid: entry.Handle,
desc: entry.Description,
cmd: entry.CommandLine,
prog: entry.ExecutablePath,
workingSet: entry.WorkingSetSize,
};

// remove some empty stuff
if (!e.cmd) delete e.cmd;
if (!e.prog) delete e.prog;

if (e.pid) {
output[e.pid] = e;
}
});

callback(null, output);
});
* Returns all the system processes
* @param filter {function(p)->Boolean} A filter function executed on each process before pushing it to the list.
* @param callback {function(err, list)} Called with the list of all processes.
* @remarks Runs only on Windows (uses WMI)
*/
module.exports = function (filter, callback) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest that instead of filter we will receive options.filter so we can have it optional (the options object is distinguishable from the callback function so it is easy to make it optional):

function(options, callback) {
  if (typeof options === 'function') {
    callback = options;
    options = {};
  }

  options = options || {};
  options.filter = options.filter || function() { return true; };

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, absolutely. This would be much better.


From: Elad Ben-Israel [reply@reply.github.com]
Sent: Wednesday, March 14, 2012 10:40 PM
To: Yosef Dinerstein
Subject: Re: [node-wintools] Parse XML output of WMIC. (#1)

  •        if (!e.cmd) delete e.cmd;
    

- if (!e.prog) delete e.prog;

  •        if (e.pid) {
    
  •            output[e.pid] = e;
    
  •        }
    

- });

  •    callback(null, output);
    
  • });
    +* Returns all the system processes
    +* @param filter {function(p)->Boolean} A filter function executed on each process before pushing it to the list.
    +* @param callback {function(err, list)} Called with the list of all processes.
    +* @remarks Runs only on Windows (uses WMI)
    +*/
    +module.exports = function (filter, callback) {

May I suggest that instead of filter we will receive options.filter so we can have it optional (the options object is distinguishable from the callback function so it is easy to make it optional):

function(options, callback) {
  if (typeof options === 'function') {
    callback = options;
    options = {};
  }

  options = options || {};
  options.filter = options.filter || function() { return true; };

Reply to this email directly or view it on GitHub:
https://github.com/anodejs/node-wintools/pull/1/files#r559415

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this change

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

-----Original Message-----
From: Yosef Dinerstein [mailto:reply@reply.github.com]
Sent: Thursday, March 15, 2012 4:04 AM
To: Elad Ben-Israel
Subject: Re: [node-wintools] Parse XML output of WMIC. (#1)

  •        if (!e.cmd) delete e.cmd;
    

- if (!e.prog) delete e.prog;

  •        if (e.pid) {
    
  •            output[e.pid] = e;
    
  •        }
    

- });

  •    callback(null, output);
    
  • });
    +* Returns all the system processes
    +* @param filter {function(p)->Boolean} A filter function executed on each process before pushing it to the list.
    +* @param callback {function(err, list)} Called with the list of all processes.
    +* @remarks Runs only on Windows (uses WMI) */ module.exports =
    +function (filter, callback) {

Made this change


Reply to this email directly or view it on GitHub:
https://github.com/anodejs/node-wintools/pull/1/files#r562293

if (!filter) filter = function () { return true; }
if (!callback) callback = function () { };

var options = [
'process',
'list',
'/format:rawxml'
];

// Spawn process, since output from exec can be too big for buffer size supported.
var p = spawn('wmic', options);

var xml = '';

p.stdout.on('data', function (data) {
xml = xml + data.toString();
});

p.stderr.on('data', function (data) {
console.error(data);
});

p.on('exit', function () {
parser = new xml2js.Parser();
parser.parseString(xml, function (err, result) {
var output = {};
result.RESULTS.CIM.INSTANCE.forEach(function (p) {
var entry = {};
p.PROPERTY.forEach(function (v) {
entry[v['@'].NAME] = v.VALUE;
});

var e = {
pid: entry.Handle,
desc: entry.Description,
cmd: entry.CommandLine,
prog: entry.ExecutablePath
};

if (!e.cmd) delete e.cmd;
if (!e.prog) delete e.prog;

if (e.pid && filter(e)) {
output[e.pid] = e;
}
});
callback(null, output);
});
});
}
51 changes: 33 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
{
"name": "wintools",
"description": "Some Windows tools for node.js",
"main": "./main",
"author": "Elad Ben-Israel",
"version": "0.1.0",
"dependencies": {
"async": ">=0.1.15"
},
"devDependencies": {
"nodeunit": "*"
},
"repository": {
"type": "git",
"url": "https://github.com/anodejs/node-wintools"
},
"scripts": {
"test": "node_modules/nodeunit/bin/nodeunit tests/"
}
"name": "wintools",
"description": "Some Windows tools for node.js",
"main": "./main",
"bin": {},
"author": "anode <anode@microsoft.com>",
"version": "0.2.0",
"license": "MIT",
"contributors": [
"Elad Ben-Israel <eladb@microsoft.com>",
"Yosef Dinerstein <yosefd@microsoft.com>"
],
"keywords": [
"nodejs",
"windows"
],
"engines": {
"node": "~0.6.x"
},
"dependencies": {
"async": "0.1.x",
"xml2js": "0.1.x"
},
"devDependencies": {
"nodeunit": "0.6.x"
},
"repository": {
"type": "git",
"url": "git://github.com/anodejs/node-wintools.git"
},
"scripts": {
"test": "nodeunit test/*.js"
}
}

60 changes: 30 additions & 30 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
var wintools = require('../main');

exports.ps = function(test) {
wintools.ps(function(err, ps) {
test.ok(!err, err);
test.ok(ps);
test.ok(ps && Object.keys(ps).length > 0);
test.done();
});
};

exports.killByPID = function(test) {
wintools.kill.pid(77777, function(err) {
test.ok(err);
test.done();
});
};

exports.killByImage = function(test) {
wintools.kill.image('bla.exe', function(err) {
test.ok(err);
test.done();
});
};

exports.shutdown = function(test) {
// only check that api exists
test.ok(wintools.shutdown.poweroff);
test.ok(wintools.shutdown.restart);
test.done();
var wintools = require('../main');
exports.ps = function (test) {
wintools.ps(null, function (err, ps) {
test.ok(!err, err);
test.ok(ps);
test.ok(ps && Object.keys(ps).length > 0);
test.done();
});
};
exports.killByPID = function (test) {
wintools.kill.pid(77777, function (err) {
test.ok(err);
test.done();
});
};
exports.killByImage = function (test) {
wintools.kill.image('bla.exe', function (err) {
test.ok(err);
test.done();
});
};
exports.shutdown = function (test) {
// only check that api exists
test.ok(wintools.shutdown.poweroff);
test.ok(wintools.shutdown.restart);
test.done();
};