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

Fix issue #11 #56

Open
wants to merge 3 commits into
base: master
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
32 changes: 29 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,18 @@ function parseGrid(output) {

function formatOutput(data) {
var formatedData = [];

data.forEach(function (d) {
var pid = ( d.PID && d.PID[0] ) || ( d.ProcessId && d.ProcessId[0] ) || undefined;
var cmd = d.CMD || d.CommandLine || d.COMMAND || undefined;
var ppid = ( d.PPID && d.PPID[0] ) || ( d.ParentProcessId && d.ParentProcessId[0] ) || undefined;

if (pid && cmd) {
var command = cmd[0];
var command = ensureCommand(cmd);
var args = '';

if (cmd.length > 1) {
args = cmd.slice(1);
if (cmd.length > 0) {
args = cmd;
}
Copy link
Owner

Choose a reason for hiding this comment

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

it's not a good practice to change the parameter cmd and even use it later, too implicit. I would suggest you write a command parser like:

function commandParser(cmd) { ... }

var ret = commandParser(cmd);
var command = ret.command;
var args = ret.args; 


formatedData.push({
Expand All @@ -291,3 +292,28 @@ function formatOutput(data) {

return formatedData;
}

/**
* ensure command exists
*
* @param cmd {array} parse command + arguments array, e.g,
* [ '/Users/foo/dev/git/ps/test/with',
* 'space/sleep',
* 'with',
* 'space',
* '1'
* ];
* @returns {string} the command. cmd is modified to only contain actual arguments.
*/
function ensureCommand(cmd) {
var command = cmd.shift();
var fs = require('fs');

Copy link
Owner

Choose a reason for hiding this comment

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

Still, I would not change the cmd directly, I would either get a copy of cmd or just use an index to do the shifting.

while (cmd.length // there's more
&& cmd[0][0] !== '-' // doesn't start with -
&& !fs.existsSync(command) // command doesn't exist
) {
command += ' ' + cmd.shift(); // get next
}
return command;
}
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Path = require('path');
var Sinon = require('sinon');

var serverPath = Path.resolve(__dirname, './node_process_for_test.js');
var serverPathSpace = Path.resolve(__dirname, './with space/sleep with space');
var UpperCaseArg = '--UPPER_CASE';
var child = null;
var pid = null;
Expand Down Expand Up @@ -197,3 +198,26 @@ describe('test', function () {
});
});
});

// don't run on Windows
(process.platform === 'win32' ? describe.skip : describe)('test command with space', function () {
var sleep = 1;

before(function () {
child = CP.spawn(serverPathSpace, [sleep]);
pid = child.pid;
});

afterEach(killProcess);

it('by command with space in path', function (done) {
PS.lookup({command: 'with space'}, function (err, list) {
assert.equal(list.length, 1);
assert.equal(list[0].pid, pid);
assert.equal(list[0].command, serverPathSpace);
assert.equal(list[0].arguments[0], sleep);
done();
});
});

});
1 change: 1 addition & 0 deletions test/with space/sleep with space