diff --git a/lib/index.js b/lib/index.js index 893c57b..827c8d8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; } formatedData.push({ @@ -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'); + + 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; +} \ No newline at end of file diff --git a/test/test.js b/test/test.js index a682004..42c03c6 100644 --- a/test/test.js +++ b/test/test.js @@ -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; @@ -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(); + }); + }); + +}); \ No newline at end of file diff --git a/test/with space/sleep with space b/test/with space/sleep with space new file mode 120000 index 0000000..48f35bb --- /dev/null +++ b/test/with space/sleep with space @@ -0,0 +1 @@ +/bin/sleep \ No newline at end of file