Skip to content

Commit

Permalink
Add --view flag. Don't default CLI report to stdout. (#1764)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish authored Feb 24, 2017
1 parent dd994d9 commit dbb32c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
25 changes: 18 additions & 7 deletions lighthouse-cli/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as Printer from './printer';
import * as randomPort from './random-port';
import {Results} from './types/types';
const yargs = require('yargs');
const opn = require('opn');

interface LighthouseError extends Error {
code?: string
Expand Down Expand Up @@ -87,12 +88,14 @@ const cliFlags = yargs

.group([
'output',
'output-path'
'output-path',
'view'
], 'Output:')
.describe({
'output': 'Reporter for the results',
'output-path': `The file path to output the results
Example: --output-path=./lighthouse-results.html`
Example: --output-path=./lighthouse-results.html`,
'view': 'Open HTML report in your browser'
})

// boolean values
Expand All @@ -106,6 +109,7 @@ Example: --output-path=./lighthouse-results.html`
'list-all-audits',
'list-trace-categories',
'perf',
'view',
'skip-autolaunch',
'select-chrome',
'verbose',
Expand All @@ -117,7 +121,7 @@ Example: --output-path=./lighthouse-results.html`

// default values
.default('disable-cpu-throttling', true)
.default('output', Printer.GetValidOutputOptions()[Printer.OutputMode.pretty])
.default('output', Printer.GetValidOutputOptions()[Printer.OutputMode.none])
.default('output-path', 'stdout')
.default('port', 9222)
.default('max-wait-for-load', Driver.MAX_WAIT_FOR_FULLY_LOADED)
Expand Down Expand Up @@ -252,7 +256,7 @@ function handleError(err: LighthouseError) {

function saveResults(results: Results,
artifacts: Object,
flags: {output: any, outputPath: string, saveArtifacts: boolean, saveAssets: boolean}) {
flags: {output: any, outputPath: string, saveArtifacts: boolean, saveAssets: boolean, view: boolean}) {
let promise = Promise.resolve(results);
const cwd = process.cwd();
// Use the output path as the prefix for all generated files.
Expand All @@ -269,8 +273,15 @@ function saveResults(results: Results,
promise = promise.then(_ => assetSaver.saveAssets(artifacts, results.audits, resolvedPath));
}

if (flags.output === Printer.OutputMode[Printer.OutputMode.pretty]) {
promise = promise.then(_ => Printer.write(results, 'html', `${resolvedPath}.report.html`));
if (flags.output === Printer.OutputMode[Printer.OutputMode.none]) {
promise = promise
.then(_ => Printer.write(results, 'html', `${resolvedPath}.report.html`))
.then(_ => {
if (flags.view) return opn(`${resolvedPath}.report.html`, {wait: false});

log.warn('CLI', 'Report output no longer defaults to stdout. Use `--output=pretty` to re-enable.');
log.log('CLI', 'Protip: Run lighthouse with `--view` to immediately open the HTML report in your browser');
});
}

return promise.then(_ => Printer.write(results, flags.output, flags.outputPath));
Expand All @@ -279,7 +290,7 @@ function saveResults(results: Results,
export async function runLighthouse(url: string,
flags: {port: number, skipAutolaunch: boolean, selectChrome: boolean, output: any,
outputPath: string, interactive: boolean, saveArtifacts: boolean, saveAssets: boolean
maxWaitForLoad: number},
maxWaitForLoad: number, view: boolean},
config: Object | null): Promise<{}|void> {

let chromeLauncher: ChromeLauncher | undefined = undefined;
Expand Down
12 changes: 8 additions & 4 deletions lighthouse-cli/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* 'json': JSON formatted results
* 'html': An HTML report
*/
enum OutputMode { pretty, json, html };
type Mode = 'pretty' | 'json' | 'html';
enum OutputMode { pretty, json, html, none };
type Mode = 'pretty' | 'json' | 'html' | 'none';

import {Results, AuditResult} from './types/types';

Expand Down Expand Up @@ -80,7 +80,10 @@ function createOutput(results: Results, outputMode: OutputMode): string {
return JSON.stringify(results, null, 2);
}

// Pretty printed.
// No report (the new default)
if (outputMode === OutputMode.none) return '';

// Pretty printed CLI report.
const version = results.lighthouseVersion;
let output = `\n\n${log.bold}Lighthouse (${version}) results:${log.reset} ${results.url}\n\n`;

Expand Down Expand Up @@ -184,7 +187,8 @@ function write(results: Results, mode: Mode, path: string): Promise<Results> {
function GetValidOutputOptions():Array<Mode> {
return [OutputMode[OutputMode.pretty] as Mode,
OutputMode[OutputMode.json] as Mode,
OutputMode[OutputMode.html] as Mode];
OutputMode[OutputMode.html] as Mode,
OutputMode[OutputMode.none] as Mode];
}

export {
Expand Down

0 comments on commit dbb32c9

Please sign in to comment.