all files / lighthouse-core/ runner.js

81.13% Statements 43/53
64.29% Branches 9/14
100% Functions 3/3
85.42% Lines 41/48
6 statements, 2 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                              56× 56×                         28× 28× 28× 28×                                                                                                         31×        
/**
 * @license
 * Copyright 2016 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';
 
const Core = require('./core');
const Driver = require('./driver');
const Aggregator = require('./aggregator');
const assetSaver = require('./lib/asset-saver');
const fs = require('fs');
const path = require('path');
 
class Runner {
 
  static getGatherersNeededByAudits(audits) {
    audits = Core.expandAudits(audits);
 
    return audits.reduce((list, audit) => {
      audit.meta.requiredArtifacts.forEach(artifact => list.add(artifact));
      return list;
    }, new Set());
  }
 
  static run(driver, opts) {
    const config = opts.config;
 
    // Make a run, which can be .then()'d with whatever needs to run (based on the config).
    let run = Promise.resolve();
 
    // If there are passes run the Driver and gather the artifacts. If not, we will need
    // to check that there are artifacts specified in the config, and throw if not.
    if (config.passes) {
      const requiredGatherers = this.getGatherersNeededByAudits(config.audits);
 
      // Make sure we only have the gatherers that are needed by the audits
      // that have been listed in the config.
      config.passes = config.passes.map(pass => {
        pass.gatherers.filter(gatherer => {
          try {
            const GathererClass = Driver.getGathererClass(gatherer);
            const gathererNecessary = requiredGatherers.has(GathererClass.name);
            return gathererNecessary;
          } catch (requireError) {
            throw new Error(`Unable to locate gatherer: ${gatherer}`);
          }
        });
 
        return pass;
      })
 
      // Now remove any passes which no longer have gatherers.
      .filter(p => p.gatherers.length > 0);
 
      // Finally set up the driver to gather.
      run = run.then(_ => Driver.run(config.passes, Object.assign({}, opts, {driver})));
    } else {
      Eif (!config.artifacts) {
        throw new Error('Neither passes nor artifacts has been included in the config.');
      }
 
      run = run.then(_ => config.artifacts);
    }
 
    // Ignoring these two flags since this functionality is not exposed by the module.
    /* istanbul ignore next */
    Iif (opts.flags.saveArtifacts) {
      run = run.then(artifacts => {
        assetSaver.saveArtifacts(artifacts);
        return artifacts;
      });
    }
 
    /* istanbul ignore next */
    Iif (opts.flags.saveAssets) {
      run = run.then(artifacts => {
        assetSaver.saveAssets(opts, artifacts);
        return artifacts;
      });
    }
 
    // Now either run the audits, or use the supplied auditResults.
    Eif (config.audits) {
      run = run.then(artifacts => Core.audit(artifacts, config.audits));
    } else {
      if (!config.auditResults) {
        throw new Error('Neither audits nor auditResults has been included in the config.');
      }
 
      run = run.then(_ => config.auditResults);
    }
 
    // Only run aggregations if needed.
    Eif (config.aggregations) {
      run = run
          .then(results => Aggregator.aggregate(config.aggregations, results))
          .then(aggregations => {
            return {
              url: opts.url,
              aggregations
            };
          });
    }
 
    return run;
  }
 
  /**
   * Returns list of audit names for external querying.
   * @return {!Array<string>}
   */
  static getAuditList() {
    return fs
        .readdirSync(path.join(__dirname, './audits'))
        .filter(f => /\.js$/.test(f));
  }
}
 
module.exports = Runner;