Skip to content

Commit

Permalink
[api] Update forever list to use cliff
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jun 22, 2011
1 parent d2aa52b commit c5c9172
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var fs = require('fs'),
exec = require('child_process').exec,
async = require('async'),
colors = require('colors'),
cliff = require('cliff'),
daemon = require('daemon'),
nconf = require('nconf'),
timespan = require('timespan'),
Expand Down Expand Up @@ -375,32 +376,48 @@ forever.stopAll = function (format) {
// Returns the list of all process data managed by forever.
//
forever.list = function (format, procs) {
var formatted = [];
var formatted;

procs = procs || getAllProcesses();
if (!procs) {
return null;
}

if (format) {
var index = 0, maxLen = 0;
// Iterate over the procs to see which has the longest options string
procs.forEach(function (proc) {
proc.length = [proc.command || 'node', proc.file].concat(proc.options).join(' ').length;
if (proc.length > maxLen) {
maxLen = proc.length;
}
});
var index = 0, rows = [
[' ', 'command ', 'script', 'forever ', 'pid', 'logfile', 'uptime']
];

//
// Iterate over the procs to see which has the
// longest options string
//
procs.forEach(function (proc) {
// Create padding string to keep output aligned
var padding = new Array(maxLen - proc.length + 1).join(' ');
formatted.push(formatProcess(proc, index, padding));
rows.push([
'[' + index + ']',
(proc.command || 'node').grey,
[proc.file].concat(proc.options).join(' ').grey,
proc.foreverPid,
proc.pid,
proc.logFile ? proc.logFile.magenta : '',
timespan.fromDates(new Date(proc.ctime), new Date()).toString().yellow
]);

index++;
});

formatted = cliff.stringifyRows(rows, [
'white',
'grey',
'grey',
'white',
'white',
'magenta',
'yellow'
]);
}

return format ? formatted.join('\n') : procs;
return format ? formatted : procs;
};

//
Expand Down Expand Up @@ -582,26 +599,6 @@ forever.checkProcess = function (pid, callback) {
});
};

//
// ### function formatProcess (proc index, padding)
// #### @proc {Object} Process to format
// #### @index {Number} Index of the process in the set of all processes
// #### @padding {string} Padding to add to the formatted output
// Returns a formatted string for the process @proc at
// the specified index.
//
function formatProcess (proc, index, padding) {
var command = proc.command || 'node';

// Create an array of the output we can later join
return ['[' + index + ']', command.grey, proc.file.grey]
.concat(proc.options.map(function (opt) { return opt.grey }))
.concat([padding + '[' + proc.pid + ',', proc.foreverPid + ']'])
.concat(proc.logFile ? proc.logFile.magenta : '')
.concat(timespan.fromDates(new Date(proc.ctime), new Date()).toString().yellow)
.join(' ');
};

//
// ### function getAllProcess ([findDead])
// #### @findDead {boolean} Optional parameter that indicates to return dead procs
Expand Down

0 comments on commit c5c9172

Please sign in to comment.