Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --view flag. Don't default CLI report to stdout. #1764

Merged
merged 1 commit into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate that one cannot get both pretty output and HTML output now, but we're killing it entirely and #1714 captures multiple output desires anyway

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true.

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