Docs: Update ChromeLauncher usage example (#2260)

* Update usage example. Add recipe.

* forget the recipe.
This commit is contained in:
Paul Irish 2017-05-15 23:11:49 -07:00 коммит произвёл Eric Bidelman
Родитель 72b06e4228
Коммит b843104217
1 изменённых файлов: 12 добавлений и 23 удалений

Просмотреть файл

@ -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