var phantom = require('phantom');
var explorer = require('./explorer');

exports = module.exports = Clairvoyance;

/**
 * Set up clairvoyance with `options`.
 * @param {Object} options -
 */
function Clairvoyance(options) {
  this.css = options.css;
  this.html = options.html;
  this.reporters = options.reporters || [];
  this.result = {};
}

Clairvoyance.prototype.run = function(cb) {
  if (cb) {
    this.reporters.push(cb);
  }

  var _this = this;
  Promise
    .all([_createPhantom(this), _findCssFiles(this), _findHtmlFiles(this)])
    .then(function() {
      var tasks = [];

      for (var html of _this.htmlFiles) {
        for (var css of _this.cssFiles) {
          tasks.push(html.findCss(_this.ph, css));
        }
      }

      Promise.all(tasks).then(function() {
        _this.ph.exit();

        for (var css of _this.cssFiles) {
          _this.result[css.path] = css.result;
        }

        for (var reporter of _this.reporters) {
          reporter(_this.result);
        }
      });
    });
};

/**
 * @private
 * @param {Clairvoyance} _this -
 * @return {Promise} -
 */
function _createPhantom(_this) {
  return new Promise(function(resolve) {
    phantom.create().then(function(ph) {
      _this.ph = ph;
      resolve();
    });
  });
}

/**
 * @private
 * @param {Clairvoyance} _this -
 * @return {Promise} -
 */
function _findCssFiles(_this) {
  return new Promise(function(resolve) {
    _this.cssFiles = explorer.findCssFiles(_this.css);
    Promise.all(_this.cssFiles.map(function(css) {
      return css.fetch();
    })).then(resolve);
  });
}

/**
 * @private
 * @param {Clairvoyance} _this -
 * @return {Promise} -
 */
function _findHtmlFiles(_this) {
  return new Promise(function(resolve) {
    _this.htmlFiles = explorer.findHtmlFiles(_this.html);
    resolve();
  });
}

@@ -1,1025 +1,68 @@
-var phantom = require('phantom');%0Avar explorer = require('./explorer');%0A%0Aexports = module.exports = Clairvoyance;%0A%0A/**%0A * Set up clairvoyance with %60options%60.%0A * @param %7BObject%7D options -%0A */%0Afunction Clairvoyance(options) %7B%0A  this.css = options.css;%0A  this.html = options.html;%0A  this.reporters = options.reporters %7C%7C %5B%5D;%0A  this.result = %7B%7D;%0A%7D%0A%0AClairvoyance.prototype.run = function(cb) %7B%0A  if (cb) %7B%0A    this.reporters.push(cb);%0A  %7D%0A%0A  var _this = this;%0A  Promise%0A    .all(%5B_createPhantom(this), _findCssFiles(this), _findHtmlFiles(this)%5D)%0A    .then(function() %7B%0A      var tasks = %5B%5D;%0A%0A      for (var html of _this.htmlFiles) %7B%0A        for (var css of _this.cssFiles) %7B%0A          tasks.push(html.findCss(_this.ph, css));%0A        %7D%0A      %7D%0A%0A      Promise.all(tasks).then(function() %7B%0A        _this.ph.exit();%0A%0A        for (var css of _this.cssFiles) %7B%0A          _this.result%5Bcss.path%5D = css.result;%0A        %7D%0A%0A        for (var reporter of _this.reporters) %7B%0A          reporter(_this.result);%0A        %7D%0A      %7D);%0A    %7D);%0A%7D
+import phantom from 'phantom';%0Aimport explorer from './explorer'
 ;%0A%0A/
@@ -190,33 +190,26 @@
 Promise(
-function(
 resolve
-)
+ =%3E
  %7B%0A    p
@@ -233,20 +233,13 @@
 hen(
-function(ph)
+ph =%3E
  %7B%0A 
@@ -421,33 +421,26 @@
 Promise(
-function(
 resolve
-)
+ =%3E
  %7B%0A    _
@@ -528,36 +528,14 @@
 map(
-function(css) %7B%0A      return
+css =%3E
  css
@@ -546,17 +546,17 @@
 ch()
-;
+))
 %0A    
-%7D))
+  
 .the
@@ -710,25 +710,18 @@
 ise(
-function(
 resolve
-)
+ =%3E
  %7B%0A 
@@ -790,18 +790,979 @@
 esolve();%0A  %7D);%0A
-
 %7D%0A
+%0A/**%0A * Set up clairvoyance with %60options%60.%0A * @param %7BObject%7D options -%0A */%0Aclass Clairvoyance %7B%0A  constructor(options) %7B%0A    this.css = options.css;%0A    this.html = options.html;%0A    this.reporters = options.reporters %7C%7C %5B%5D;%0A    this.result = %7B%7D;%0A  %7D%0A%0A  run(cb) %7B%0A    if (cb) %7B%0A      this.reporters.push(cb);%0A    %7D%0A%0A    Promise%0A      .all(%5B_createPhantom(this), _findCssFiles(this), _findHtmlFiles(this)%5D)%0A      .then(() =%3E %7B%0A        const tasks = %5B%5D;%0A%0A        for (const html of this.htmlFiles) %7B%0A          for (const css of this.cssFiles) %7B%0A            tasks.push(html.findCss(this.ph, css));%0A          %7D%0A        %7D%0A%0A        Promise.all(tasks).then(() =%3E %7B%0A          this.ph.exit();%0A%0A          for (const css of this.cssFiles) %7B%0A            this.result%5Bcss.path%5D = css.result;%0A          %7D%0A%0A          for (const reporter of this.reporters) %7B%0A            reporter(this.result);%0A          %7D%0A        %7D);%0A      %7D);%0A  %7D%0A%7D%0A%0Aexports = module.exports = Clairvoyance;%0A

Translate 'lib/clairvoyance.js' to ES2015