text: // Generated by CoffeeScript 1.9.1
  (function() {
    'use strict';
    var Asset, EventEmitter, FileList, SourceFile, debug, fcache, helpers, startsWith, sysPath,
      bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
      extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
      hasProp = {}.hasOwnProperty,
      indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  
    debug = require('debug')('brunch:file-list');
  
    EventEmitter = require('events').EventEmitter;
  
    Asset = require('./asset');
  
    SourceFile = require('./source_file');
  
    helpers = require('../helpers');
  
    fcache = require('fcache');
  
    sysPath = require('path');
  
    startsWith = function(string, substring) {
      return string.lastIndexOf(substring, 0) === 0;
    };
  
    module.exports = FileList = (function(superClass) {
      extend(FileList, superClass);
  
      FileList.prototype.resetTime = 65;
  
      function FileList(config) {
        var interval;
        this.config = config;
        this._unlink = bind(this._unlink, this);
        this._change = bind(this._change, this);
        this.copy = bind(this.copy, this);
        this.compile = bind(this.compile, this);
        this.resetTimer = bind(this.resetTimer, this);
        this.files = [];
        this.assets = [];
        this.on('change', this._change);
        this.on('unlink', this._unlink);
        this.compiling = {};
        this.compiled = {};
        this.copying = {};
        this.initial = true;
        interval = this.config.fileListInterval;
        if (typeof interval === 'number') {
          this.resetTime = interval;
        }
      }
  
      FileList.prototype.getAssetErrors = function() {
        var invalidAssets;
        invalidAssets = this.assets.filter(function(asset) {
          return asset.error != null;
        });
        if (invalidAssets.length > 0) {
          return invalidAssets.map(function(invalidAsset) {
            return helpers.formatError(invalidAsset.error, invalidAsset.path);
          });
        } else {
          return null;
        }
      };
  
      FileList.prototype.isIgnored = function(path, test) {
        if (test == null) {
          test = this.config.conventions.ignored;
        }
        if (indexOf.call(this.config._normalized.paths.allConfigFiles, path) >= 0) {
          return true;
        }
        switch (toString.call(test)) {
          case '[object RegExp]':
            return path.match(test);
          case '[object Function]':
            return test(path);
          case '[object String]':
            return startsWith(sysPath.normalize(path), sysPath.normalize(test));
          case '[object Array]':
            return test.some((function(_this) {
              return function(subTest) {
                return _this.isIgnored(path, subTest);
              };
            })(this));
          default:
            return false;
        }
      };
  
      FileList.prototype.is = function(name, path) {
        var convention;
        convention = this.config._normalized.conventions[name];
        if (!convention) {
          return false;
        }
        if (typeof convention !== 'function') {
          throw new TypeError("Invalid convention " + convention);
        }
        return convention(path);
      };
  
      FileList.prototype.resetTimer = function() {
        if (this.timer != null) {
          clearTimeout(this.timer);
        }
        return this.timer = setTimeout((function(_this) {
          return function() {
            _this.files = _this.files.filter(function(file) {
              return !file.disposed;
            });
            if (Object.keys(_this.compiling).length === 0 && Object.keys(_this.copying).length === 0) {
              _this.emit('ready');
              return _this.compiled = {};
            } else {
              return _this.resetTimer();
            }
          };
        })(this), this.resetTime);
      };
  
      FileList.prototype.find = function(path) {
        return this.files.filter(function(file) {
          return file.path === path;
        })[0];
      };
  
      FileList.prototype.findAsset = function(path) {
        return this.assets.filter(function(file) {
          return file.path === path;
        })[0];
      };
  
      FileList.prototype.compileDependencyParents = function(path) {
        var parents, parentsList;
        parents = this.files.filter((function(_this) {
          return function(dependent) {
            return dependent.dependencies.length > 0 && indexOf.call(dependent.dependencies, path) >= 0 && !_this.compiled[dependent.path];
          };
        })(this));
        if (parents.length) {
          parentsList = parents.map(function(_) {
            return _.path;
          }).join(', ');
          debug("Compiling dependency '" + path + "' parent(s): " + parentsList);
          return parents.forEach(this.compile);
        }
      };
  
      FileList.prototype.compile = function(file) {
        var path;
        file.removed = false;
        path = file.path;
        if (this.compiling[path]) {
          return this.resetTimer();
        } else {
          this.compiling[path] = true;
          return file.compile((function(_this) {
            return function(error) {
              delete _this.compiling[path];
              _this.resetTimer();
              if (error != null) {
                return;
              }
              debug("Compiled file '" + path + "'...");
              _this.compiled[path] = true;
              return _this.emit('compiled', path);
            };
          })(this));
        }
      };
  
      FileList.prototype.copy = function(asset) {
        var path;
        path = asset.path;
        this.copying[path] = true;
        return asset.copy((function(_this) {
          return function(error) {
            delete _this.copying[path];
            _this.resetTimer();
            if (error != null) {
              return;
            }
            return debug("Copied asset '" + path + "'");
          };
        })(this));
      };
  
      FileList.prototype._add = function(path, compiler, linters, isHelper) {
        var file, isVendor, wrapper;
        isVendor = this.is('vendor', path);
        wrapper = this.config._normalized.modules.wrapper;
        file = new SourceFile(path, compiler, linters, wrapper, isHelper, isVendor);
        this.files.push(file);
        return file;
      };
  
      FileList.prototype._addAsset = function(path) {
        var file;
        file = new Asset(path, this.config);
        this.assets.push(file);
        return file;
      };
  
      FileList.prototype._change = function(path, compiler, linters, isHelper) {
        var ignored, ref;
        ignored = this.isIgnored(path);
        if (this.is('assets', path)) {
          if (!ignored) {
            return this.copy((ref = this.findAsset(path)) != null ? ref : this._addAsset(path));
          }
        } else {
          debug("Reading '" + path + "'");
          return fcache.updateCache(path, (function(_this) {
            return function(error, source) {
              var ref1, sourceFile;
              if (error) {
                if (error != null) {
                  return console.log('Reading', error);
                }
              }
              if (!ignored && (compiler != null ? compiler.length : void 0)) {
                sourceFile = (ref1 = _this.find(path)) != null ? ref1 : _this._add(path, compiler, linters, isHelper);
                _this.compile(sourceFile);
              }
              if (!_this.initial) {
                return _this.compileDependencyParents(path);
              }
            };
          })(this));
        }
      };
  
      FileList.prototype._unlink = function(path) {
        var file, ignored;
        ignored = this.isIgnored(path);
        if (this.is('assets', path)) {
          if (!ignored) {
            this.assets.splice(this.assets.indexOf(path), 1);
          }
        } else {
          if (ignored) {
            this.compileDependencyParents(path);
          } else {
            file = this.find(path);
            if (file && !file.disposed) {
              file.removed = true;
            }
          }
        }
        return this.resetTimer();
      };
  
      return FileList;
  
    })(EventEmitter);
  
  }).call(this);
  