var Q = require('q');

var util = require('./util');
var pkg = require('../package.json');
var colors = require('colors/safe');
var isRunning = util.isRunning;
var showUsage = util.showUsage;
var readConfig = util.readConfig;
var readConfigList = util.readConfigList;
var warn = util.warn;
var info = util.info;

function showAll(byStop) {
  var list = readConfigList().map(function(config) {
    var deferred = Q.defer();
    isRunning(config.pid, function(running) {
      deferred.resolve(running && config);
    });
    return deferred.promise;
  });
  Q.all(list).then(function(confList) {
    confList = confList.filter(function(conf) {
      return conf;
    });
    var len = confList.length;
    if (!len) {
      warn('[!] No running whistle.');
    } else {
      var tips = [byStop ? '[i] Other running whistle:' : '[i] All running whistle:'];
      confList.forEach(function(conf, i) {
        ++i;
        var options = conf.options;
        tips.push('  ' + i + '. port: ' + (options.port || pkg.port)
          + (options.host ? ', host: ' + options.host : '')
          + (options.storage ? ', storage: ' + options.storage : '')
          + (conf.pid ? ', pid: ' + conf.pid : '')
          + (byStop ? colors.red(' (stop cmd: ' + (options.storage ? 'w2 stop -S ' + options.storage : 'w2 stop') + ')') : ''));
      });
      byStop && warn('[!] This whistle is not running.');
      info(tips.join('\n'));
    }
  });

  // All running whistle:
  // 1. port: 8899
  // 2. port: 888, storage: xxx
}

module.exports = function(all, storage) {
  if (!all) {
    var config = readConfig(storage) || '';
    var pid = config.pid;
    var options = pid && config.options;
    if (pid) {
      isRunning(pid, function(running) {
        if (running) {
          return showUsage(true, options);
        }
        showAll();
      });
      return;
    }
  }
  showAll();
};

module.exports.showAll = showAll;

@@ -784,48 +784,8 @@
  = %5B
-byStop ? '%5Bi%5D Other running whistle:' : 
 '%5Bi%5D
@@ -809,16 +809,16 @@
 tle:'%5D;%0A
+
       co
@@ -932,18 +932,79 @@
 + i + '.
- p
+' + (conf.pid ? ' PID: ' + conf.pid + ',' : '')%0A          + ' P
 ort: ' +
@@ -1062,17 +1062,17 @@
 st ? ', 
-h
+H
 ost: ' +
@@ -1125,17 +1125,17 @@
 ge ? ', 
-s
+S
 torage: 
@@ -1164,59 +1164,8 @@
 '')%0A
-          + (conf.pid ? ', pid: ' + conf.pid : '')%0A
     
@@ -1196,17 +1196,17 @@
 .red(' (
-s
+S
 top cmd:

refine code