From b843104217592162be9654f9e19700c4e11890d7 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Mon, 15 May 2017 23:11:49 -0700 Subject: [PATCH] Docs: Update ChromeLauncher usage example (#2260) * Update usage example. Add recipe. * forget the recipe. --- readme.md | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/readme.md b/readme.md index 4caf28dea4..65ae008052 100644 --- a/readme.md +++ b/readme.md @@ -160,32 +160,21 @@ assumes you've installed Lighthouse as a dependency (`yarn add --dev lighthouse` ```javascript const lighthouse = require('lighthouse'); -const ChromeLauncher = require('lighthouse/chrome-launcher/chrome-launcher.js'); -const Printer = require('lighthouse/lighthouse-cli/printer'); +const chromeLauncher = require('lighthouse/chrome-launcher/chrome-launcher'); -function launchChromeAndRunLighthouse(url, flags, config) { - const launchedChrome = ChromeLauncher.launch(); - - launchedChrome - .then(() => lighthouse(url, flags, config)) // Run Lighthouse. - .then(results => launchedChrome.kill().then(() => results)) // Kill Chrome and return results. - .catch(err => { - // Kill Chrome if there's an error. - return launchedChrome.kill().then(() => { - throw err; - }); - }); +function launchChromeAndRunLighthouse(url, flags, config = null) { + return chromeLauncher.launch().then(chrome => { + flags.port = chrome.port; + return lighthouse(url, flags, config).then(results => + chrome.kill().then(() => results) + ); + }); } -// Use an existing config or create a custom one. -const config = require('lighthouse/lighthouse-core/config/perf.json'); -const url = 'https://example.com'; -const flags = {output: 'html'}; - -launchChromeAndRunLighthouse(url, flags, config).then(lighthouseResults => { - lighthouseResults.artifacts = undefined; // You can save the artifacts separately if so desired - return Printer.write(lighthouseResults, flags.output); -}).catch(err => console.error(err)); +// In use: +const flags = {output: 'json'}; +launchChromeAndRunLighthouse('https://example.com', flags) + .then(results => console.log(results)); ``` ### Recipes