var path = require('path');
var fs = require('fs');
var fse = require('fs-extra2');
var protocols = require('../rules/protocols');
var comUtil = require('../util');
var util = require('./util');
var mp = require('./module-paths');
var paths = mp.getPaths();

function readPluginMoudlesSync(dir, plugins) {
  plugins = plugins || {};
  try {
    var list = fs.readdirSync(dir).filter(function(name) {
      if (util.isWhistleModule(name)) {
        return true;
      }

      if (util.isOrgModule(name)) {
        try {
          var _dir = path.join(dir, name);
          fs.readdirSync(_dir).forEach(function(name) {
            if (!plugins[name] && util.isWhistleModule(name)) {
              plugins[name] = path.join(_dir, name);
            }
          });
        } catch(e) {}
      }
      return false;
    });

    list.forEach(function(name) {
      if (!plugins[name]) {
        plugins[name] = path.join(dir, name);
      }
    });
  } catch(e) {}

  return plugins;
}

module.exports = function() {
  var plugins = {};
  paths.forEach(function(dir) {
    readPluginMoudlesSync(dir, plugins);
  });

  var _plugins = {};
  Object.keys(plugins).forEach(function(name) {
    var simpleName = name.split('.')[1];
    if (protocols.contains(simpleName)) {
      return;
    }
    var dir = plugins[name];
    try {
      var pkgPath = path.join(dir, 'package.json');
      var pkg = fse.readJsonSync(pkgPath);
      if (pkg && pkg.version) {
        var stats = fs.statSync(pkgPath);
        var plugin = {
          moduleName: pkg.name,
          priority: parseInt(pkg.pluginPriority, 10) || 0,
          registry: comUtil.getRegistry(pkg),
          path: dir,
          pkgPath: pkgPath,
          mtime: stats.mtime.getTime(),
          version: pkg.version,
          description: pkg.description,
          homepage: util.getHomePageFromPackage(pkg),
          rules: comUtil.trim(comUtil.readFileSync(path.join(dir, 'rules.txt'))),
          _rules: comUtil.trim(comUtil.readFileSync(path.join(dir, '_rules.txt'))),
          _values: util.parseValues(comUtil.readFileSync(path.join(dir, '_values.txt')))
        };

        _plugins[simpleName + ':'] = plugin;
      }
    } catch(e) {}
  });

  return _plugins;
};



@@ -128,20 +128,17 @@
 ');%0Avar 
-comU
+u
 til = re
@@ -151,33 +151,39 @@
 '../util');%0Avar 
-u
+pluginU
 til = require('.
@@ -402,25 +402,31 @@
 %7B%0A      if (
-u
+pluginU
 til.isWhistl
@@ -482,17 +482,23 @@
     if (
-u
+pluginU
 til.isOr
@@ -661,17 +661,23 @@
 ame%5D && 
-u
+pluginU
 til.isWh
@@ -1643,20 +1643,17 @@
 gistry: 
-comU
+u
 til.getR
@@ -1847,17 +1847,23 @@
 mepage: 
-u
+pluginU
 til.getH
@@ -1900,28 +1900,25 @@
      rules: 
-comU
+u
 til.trim(com
@@ -1906,36 +1906,33 @@
 ules: util.trim(
-comU
+u
 til.readFileSync
@@ -1981,20 +1981,17 @@
 _rules: 
-comU
+u
 til.trim
@@ -1991,20 +1991,17 @@
 il.trim(
-comU
+u
 til.read
@@ -2056,25 +2056,31 @@
    _values: 
-u
+pluginU
 til.parseVal
@@ -2087,12 +2087,9 @@
 ues(
-comU
+u
 til.

refine code