# all files / cli/ printer.js

100% Statements 58/58
100% Branches 20/20
100% Functions 6/6
100% Lines 57/57
3 statements, 1 function, 1 branch 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
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184

    1×
    1×
    1×

    1×

    1×

    1×
    5×
    1×
    1×

    4×

    1×
    5×
    1×
    1×

    4×

    1×
    8×

    8×
    5×

    3×
    1×

    2×
    2×
    16×
    16×

    16×
    46×
    46×
    6×

    46×
    46×
    8×

    46×
    2×

    2×

    16×

    2×

    1×
    1×

    1×
    3×

    3×
    3×
    1×

    2×
    2×

    1×
    3×
    3×
    3×

    3×

    3×
    1×

    3×
    2×
    1×

    1×

     /**
     * @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 fs = require('fs');
    const ReportGenerator = require('../report/report-generator');
    const Formatter = require('../formatters/formatter');

    const log = require('../src/lib/log.js');

    /**
     * An enumeration of acceptable output modes:
     * <ul>
     *   <li>'pretty': Pretty print the results</li>
     *   <li>'json': JSON formatted results</li>
     *   <li>'html': An HTML report</li>
     * </ul>
     * @enum {string}
     */
    const OUTPUT_MODE = {
      pretty: 'pretty',
      json: 'json',
      html: 'html'
    };

    /**
     * Verify output mode.
     * @param {string} mode
     * @return {OUTPUT_MODE}
     */
    function checkOutputMode(mode) {
      if (!OUTPUT_MODE.hasOwnProperty(mode)) {
        log.log('warn', `Unknown output mode ${mode}; using pretty`);
        return OUTPUT_MODE.pretty;
      }

      return OUTPUT_MODE[mode];
    }

    /**
     * Verify output path to use, either stdout or a file path.
     * @param {string} path
     */
    function checkOutputPath(path) {
      if (!path) {
        log.log('warn', 'No output path set; using stdout');
        return 'stdout';
      }

      return path;
    }

    /**
     * Creates the results output in a format based on the `mode`.
     *
     * @param {{url: string, aggregations: !Array<*>}} results
     * @param {OUTPUT_MODE} outputMode
     * @return {string}
     */
    function createOutput(results, outputMode) {
      const reportGenerator = new ReportGenerator();

      // HTML report.
      if (outputMode === 'html') {
        return reportGenerator.generateHTML(results, {inline: true});
      }

      // JSON report.
      if (outputMode === 'json') {
        return JSON.stringify(results.aggregations, null, 2);
      }

      // Pretty printed.
      let output = `Lighthouse results: ${results.url}\n\n`;
      results.aggregations.forEach(item => {
        let score = (item.score.overall * 100).toFixed(0);
        output += `${item.name}: ${score}%\n`;

        item.score.subItems.forEach(subitem => {
          let lineItem = ` -- ${subitem.description}: ${subitem.value}`;
          if (typeof subitem.rawValue !== 'undefined') {
            lineItem += ` (${subitem.rawValue})`;
          }
          output += `${lineItem}\n`;
          if (subitem.debugString) {
            output += `    ${subitem.debugString}\n`;
          }

          if (subitem.extendedInfo && subitem.extendedInfo.value) {
            const formatter =
                Formatter.getByName(subitem.extendedInfo.formatter).getFormatter('pretty');
            output += `${formatter(subitem.extendedInfo.value)}`;
          }
        });

        output += '\n';
      });

      return output;
    }

    /* istanbul ignore next */
    /**
     * Writes the output to stdout.
     *
     * @param {string} output
     * @return {!Promise}
     */
    function writeToStdout(output) {
      return Promise.resolve(process.stdout.write(`${output}\n`));
    }

    /**
     * Writes the output to a file.
     *
     * @param {string} filePath The destination path
     * @param {string} output The output to write
     * @param {string} outputMode Output mode; either 'pretty', 'json', or 'html'.
     * @return {Promise}
     */
    function writeFile(filePath, output, outputMode) {
      return new Promise((resolve, reject) => {
        // TODO: make this mkdir to the filePath.
        fs.writeFile(filePath, output, 'utf8', err => {
          if (err) {
            return reject(err);
          }
          log.info('printer', `${outputMode} output written to ${filePath}`);
          resolve();
        });
      });
    }

    /**
     * Writes the results.
     *
     * @param {{url: string, aggregations: !Array<*>}} results
     * @param {string} mode Output mode; either 'pretty', 'json', or 'html'.
     * @param {string} path The output path to use, either stdout or a file path.
     * @return {!Promise}
     */
    function write(results, mode, path) {
      return new Promise((resolve, reject) => {
        const outputMode = checkOutputMode(mode);
        const outputPath = checkOutputPath(path);

        const output = createOutput(results, outputMode);

        // Testing stdout is out of scope, and doesn't really achieve much besides testing Node,
        // so we will skip this chunk of the code.
        /* istanbul ignore if */
        Iif (outputPath === 'stdout') {
          return writeToStdout(output).then(_ => resolve(results));
        }

        return writeFile(outputPath, output, outputMode).then(_ => {
          resolve(results);
        }).catch(err => reject(err));
      });
    }

    module.exports = {
      checkOutputMode,
      checkOutputPath,
      createOutput,
      write,
      OUTPUT_MODE
    };