2017-05-10 05:13:34 +03:00
/ * *
2017-06-06 03:43:00 +03:00
* @ license Copyright 2017 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 .
2017-05-10 05:13:34 +03:00
* /
'use strict' ;
2017-11-21 05:10:03 +03:00
/* eslint-disable max-len */
2017-05-10 05:13:34 +03:00
const yargs = require ( 'yargs' ) ;
2017-11-21 05:10:03 +03:00
// @ts-ignore
2017-05-10 05:13:34 +03:00
const pkg = require ( '../package.json' ) ;
2017-11-21 05:10:03 +03:00
const printer = require ( './printer' ) ;
2017-05-10 05:13:34 +03:00
2017-11-21 05:10:03 +03:00
/ * *
* @ param { string = } manualArgv
* @ return { ! LH . Flags }
* /
function getFlags ( manualArgv ) {
// @ts-ignore yargs() is incorrectly typed as not returning itself
2017-06-28 09:16:13 +03:00
const y = manualArgv ? yargs ( manualArgv ) : yargs ;
return y . help ( 'help' )
2017-05-10 05:13:34 +03:00
. version ( ( ) => pkg . version )
. showHelpOnFail ( false , 'Specify --help for available options' )
. usage ( 'lighthouse <url>' )
. example (
'lighthouse <url> --view' , 'Opens the HTML report in a browser after the run completes' )
. example (
'lighthouse <url> --config-path=./myconfig.js' ,
'Runs Lighthouse with your own configuration: custom audits, report generation, etc.' )
. example (
'lighthouse <url> --output=json --output-path=./report.json --save-assets' ,
'Save trace, screenshots, and named JSON report.' )
. example (
'lighthouse <url> --disable-device-emulation --disable-network-throttling' ,
'Disable device emulation' )
. example (
'lighthouse <url> --chrome-flags="--window-size=412,732"' ,
'Launch Chrome with a specific window size' )
. example (
'lighthouse <url> --quiet --chrome-flags="--headless"' ,
'Launch Headless Chrome, turn off logging' )
2018-01-19 22:50:32 +03:00
. example (
'lighthouse <url> --extra-headers "{\\"Cookie\\":\\"monster=blue\\", \\"x-men\\":\\"wolverine\\"}"' ,
'Stringify\'d JSON HTTP Header key/value pairs to send in requests' )
. example (
'lighthouse <url> --extra-headers=./path/to/file.json' ,
'Path to JSON file of HTTP Header key/value pairs to send in requests' )
2017-05-10 05:13:34 +03:00
// List of options
. group ( [ 'verbose' , 'quiet' ] , 'Logging:' )
. describe ( {
verbose : 'Displays verbose logging' ,
2017-11-21 05:10:03 +03:00
quiet : 'Displays no progress, debug logs or errors' ,
2017-05-10 05:13:34 +03:00
} )
. group (
2017-11-21 05:10:03 +03:00
[
2018-02-08 03:13:33 +03:00
'save-assets' , 'list-all-audits' , 'list-trace-categories' , 'additional-trace-categories' ,
'config-path' , 'chrome-flags' , 'perf' , 'mixed-content' , 'port' , 'hostname' ,
'max-wait-for-load' , 'enable-error-reporting' , 'gather-mode' , 'audit-mode' ,
2018-03-28 01:15:52 +03:00
'only-audits' , 'only-categories' , 'skip-audits' ,
2017-11-21 05:10:03 +03:00
] ,
'Configuration:' )
2017-05-10 05:13:34 +03:00
. describe ( {
2017-10-27 03:01:03 +03:00
'enable-error-reporting' :
2017-11-22 00:16:24 +03:00
'Enables error reporting, overriding any saved preference. --no-enable-error-reporting will do the opposite. More: https://git.io/vFFTO' ,
2017-08-29 03:05:56 +03:00
'blocked-url-patterns' : 'Block any network requests to the specified URL patterns' ,
2017-05-10 05:13:34 +03:00
'disable-storage-reset' :
'Disable clearing the browser cache and other storage APIs before a run' ,
'disable-device-emulation' : 'Disable Nexus 5X emulation' ,
'disable-cpu-throttling' : 'Disable CPU throttling' ,
'disable-network-throttling' : 'Disable network throttling' ,
2018-01-05 23:17:11 +03:00
'gather-mode' :
2018-03-20 03:13:09 +03:00
'Collect artifacts from a connected browser and save to disk. (Artifacts folder path may optionally be provided). If audit-mode is not also enabled, the run will quit early.' ,
'audit-mode' : 'Process saved artifacts from disk. (Artifacts folder path may be provided, otherwise defaults to ./latest-run/)' ,
2017-05-10 05:13:34 +03:00
'save-assets' : 'Save the trace contents & screenshots to disk' ,
'list-all-audits' : 'Prints a list of all available audits and exits' ,
'list-trace-categories' : 'Prints a list of all required trace categories and exits' ,
'additional-trace-categories' :
'Additional categories to capture with the trace (comma-delimited).' ,
'config-path' : 'The path to the config JSON.' ,
2018-02-08 03:13:33 +03:00
'mixed-content' : 'Use the mixed-content auditing configuration.' ,
2017-05-10 05:13:34 +03:00
'chrome-flags' :
2018-01-09 03:18:02 +03:00
` Custom flags to pass to Chrome (space-delimited). For a full list of flags, see http://bit.ly/chrome-flags
Additionally , use the CHROME _PATH environment variable to use a specific Chrome binary . Requires Chromium version 54.0 or later . If omitted , any detected Chrome Canary or Chrome stable will be used . ` ,
2017-05-10 05:13:34 +03:00
'perf' : 'Use a performance-test-only configuration' ,
2017-08-15 00:19:53 +03:00
'hostname' : 'The hostname to use for the debugging protocol.' ,
2017-05-10 05:13:34 +03:00
'port' : 'The port to use for the debugging protocol. Use 0 for a random port' ,
'max-wait-for-load' :
'The timeout (in milliseconds) to wait before the page is considered done loading and the run should continue. WARNING: Very high values can lead to large traces and instability' ,
2018-01-19 22:50:32 +03:00
'extra-headers' : 'Set extra HTTP Headers to pass with request' ,
2018-03-28 01:15:52 +03:00
'only-audits' : 'Only run the specified audits' ,
'only-categories' : 'Only run the specified categories' ,
'skip-audits' : 'Run everything except these audits' ,
2017-05-10 05:13:34 +03:00
} )
2018-01-05 23:17:11 +03:00
// set aliases
. alias ( { 'gather-mode' : 'G' , 'audit-mode' : 'A' } )
2017-05-10 05:13:34 +03:00
. group ( [ 'output' , 'output-path' , 'view' ] , 'Output:' )
. describe ( {
'output' : ` Reporter for the results, supports multiple values ` ,
'output-path' : ` The file path to output the results. Use 'stdout' to write to stdout.
2017-11-21 05:10:03 +03:00
If using JSON output , default is stdout .
If using HTML output , default is a file in the working directory with a name based on the test URL and date .
If using multiple outputs , -- output - path is ignored .
Example : -- output - path = . / lighthouse - results . html ` ,
'view' : 'Open HTML report in your browser' ,
2017-05-10 05:13:34 +03:00
} )
// boolean values
. boolean ( [
'disable-storage-reset' , 'disable-device-emulation' , 'disable-cpu-throttling' ,
2018-01-05 23:17:11 +03:00
'disable-network-throttling' , 'save-assets' , 'list-all-audits' ,
2017-11-21 05:10:03 +03:00
'list-trace-categories' , 'perf' , 'view' , 'verbose' , 'quiet' , 'help' ,
2018-03-20 03:13:09 +03:00
'mixed-content' ,
2017-05-10 05:13:34 +03:00
] )
2017-11-21 05:10:03 +03:00
. choices ( 'output' , printer . getValidOutputOptions ( ) )
2017-08-29 03:05:56 +03:00
// force as an array
2018-03-28 01:15:52 +03:00
// note MUST use camelcase versions or only the kebab-case version will be forced
. array ( 'blockedUrlPatterns' )
. array ( 'onlyAudits' )
. array ( 'onlyCategories' )
. array ( 'skipAudits' )
. string ( 'extraHeaders' )
2017-05-10 05:13:34 +03:00
// default values
. default ( 'chrome-flags' , '' )
2018-01-09 03:18:02 +03:00
. default ( 'output' , 'html' )
2017-05-15 00:22:00 +03:00
. default ( 'port' , 0 )
2017-08-15 00:19:53 +03:00
. default ( 'hostname' , 'localhost' )
2017-11-21 05:10:03 +03:00
. check ( /** @param {!LH.Flags} argv */ ( argv ) => {
2017-05-10 05:13:34 +03:00
// Make sure lighthouse has been passed a url, or at least one of --list-all-audits
// or --list-trace-categories. If not, stop the program and ask for a url
if ( ! argv . listAllAudits && ! argv . listTraceCategories && argv . _ . length === 0 ) {
throw new Error ( 'Please provide a url' ) ;
}
return true ;
} )
. epilogue (
'For more information on Lighthouse, see https://developers.google.com/web/tools/lighthouse/.' )
. wrap ( yargs . terminalWidth ( ) )
. argv ;
}
2017-11-21 05:10:03 +03:00
module . exports = {
getFlags ,
} ;