var fs = require('fs'),
    Q = require('q'),
    path = require('path');

function readModuleMeanJSONFileDone (_modules, fileDefer, file, source, fileErr, data){
  if (data) {
    try {
      var json = JSON.parse(data.toString());
      if (json.name && json.version) {
        var dependable = _modules.createModule(json.name,json.version,path.join(source,file));
      } else {
        return fileDefer.resolve();
      }
      if (json.dependencies) {
        dependable.cloneDependencies(json.dependencies);
      }
      _modules.add(dependable);
    } catch (err) {
      console.log(file,'mean json error',err);
    }
  }
  fileDefer.resolve();
}

function readModulePackageJSONFileDone (_modules, fileDefer, file, source, fileErr, data) {
  if (data) {
    try {
      var json = JSON.parse(data.toString());
      if (json.mean) {
        fs.readFile(path.join(process.cwd(), source, file, 'mean.json'), readModuleMeanJSONFileDone.bind(null, _modules, fileDefer, file, source));
        return;
      }
    } catch (err) {
      fileDefer.reject(err);
      return;
    }
    fileDefer.resolve();
  }else{
    fileDefer.resolve();
    //fileDefer.reject(fileErr);
  }
}

function fileForEachProcess (_modules, source, promises, file) {
  var fileDefer = Q.defer();
  fs.readFile(path.join(process.cwd(), source, file, 'package.json'), readModulePackageJSONFileDone.bind(null, _modules, fileDefer, file, source));
  promises.push(fileDefer.promise);
}

function processDirFilesFromSearchSource(_modules, disabled, source, deferred, err, files) {
  if (err || !files || !files.length) {
    if (err && err.code !== 'ENOENT') {
      console.log(err);
    } else {
      return deferred.resolve();
    }
    return deferred.reject(err);
  }

  var promises = [];
  for (var i in disabled) {
    var index = files.indexOf(i);
    if (index < 0) continue;
    files.splice(index, 1);
  }

  files.forEach(fileForEachProcess.bind(null, _modules, source, promises));
  return deferred.resolve(Q.all(promises));
}

function searchSourceForFindModules(_modules, disabled, source) {
  var deferred = Q.defer();
  fs.readdir(path.join(process.cwd(), source), processDirFilesFromSearchSource.bind (null, _modules, disabled, source, deferred));
  return deferred.promise;
}

module.exports = searchSourceForFindModules;

var fs = require('fs'),
    Q = require('q'),
    path = require('path');

function readModuleMeanJSONFileDone (_modules, fileDefer, file, source, fileErr, data){
  if (data) {
    try {
      var json = JSON.parse(data.toString());
      if (json.name && json.version) {
        var dependable = _modules.createModule(json.name,json.version,path.join(source,file));
      } else {
        return fileDefer.resolve();
      }
      if (json.dependencies) {
        dependable.cloneDependencies(json.dependencies);
      }
      _modules.add(dependable);
    } catch (err) {
      console.log(file,'mean json error',err);
    }
  }
  fileDefer.resolve();
}

function readModulePackageJSONFileDone (_modules, fileDefer, file, source, fileErr, data) {
  if (data) {
    try {
      var json = JSON.parse(data.toString());
      if (json.mean) {
        fs.readFile(path.join(process.cwd(), source, file, 'mean.json'), readModuleMeanJSONFileDone.bind(null, _modules, fileDefer, file, source));
        return;
      }
    } catch (err) {
      console.error(file, 'package.json error in readModulePackageJSONFileDone', err);
      fileDefer.reject(err);
      return;
    }
    fileDefer.resolve();
  }else{
    fileDefer.resolve();
    //fileDefer.reject(fileErr);
  }
}

function fileForEachProcess (_modules, source, promises, file) {
  var fileDefer = Q.defer();
  fs.readFile(path.join(process.cwd(), source, file, 'package.json'), readModulePackageJSONFileDone.bind(null, _modules, fileDefer, file, source));
  promises.push(fileDefer.promise);
}

function processDirFilesFromSearchSource(_modules, disabled, source, deferred, err, files) {
  if (err || !files || !files.length) {
    if (err && err.code !== 'ENOENT') {
      console.log(err);
    } else {
      return deferred.resolve();
    }
    return deferred.reject(err);
  }

  var promises = [];
  for (var i in disabled) {
    var index = files.indexOf(i);
    if (index < 0) continue;
    files.splice(index, 1);
  }

  files.forEach(fileForEachProcess.bind(null, _modules, source, promises));
  return deferred.resolve(Q.all(promises));
}

function searchSourceForFindModules(_modules, disabled, source) {
  var deferred = Q.defer();
  fs.readdir(path.join(process.cwd(), source), processDirFilesFromSearchSource.bind (null, _modules, disabled, source, deferred));
  return deferred.promise;
}

module.exports = searchSourceForFindModules;

fix #116 - log package.json loading errors (#120)