diff --git a/lighthouse-cli/cli-flags.js b/lighthouse-cli/cli-flags.js index 9b9ceb989cf2..9e7188f042e1 100644 --- a/lighthouse-cli/cli-flags.js +++ b/lighthouse-cli/cli-flags.js @@ -62,6 +62,17 @@ function getFlags(manualArgv) { 'lighthouse --only-categories=performance,pwa', 'Only run specific categories.') + /** + * Also accept a file for all of these flags. Yargs will merge in and override the file-based + * flags with the command-line flags. + * + * i.e. when command-line `--throttling-method=provided` and file `throttlingMethod: "devtools"`, + * throttlingMethod will be `provided`. + * + * @see https://github.com/yargs/yargs/blob/a6e67f15a61558d0ba28bfe53385332f0ce5d431/docs/api.md#config + */ + .config('cli-flags-path') + // List of options .group(['verbose', 'quiet'], 'Logging:') .describe({ @@ -78,6 +89,7 @@ function getFlags(manualArgv) { ], 'Configuration:') .describe({ + 'cli-flags-path': 'The path to a JSON file that contains the desired CLI flags to apply. Flags specified at the command line will still override the file-based ones.', // We don't allowlist specific locales. Why? So we can support the user who requests 'es-MX' (unsupported) and we'll fall back to 'es' (supported) 'locale': 'The locale/language the report should be formatted in', 'enable-error-reporting': diff --git a/lighthouse-cli/test/cli/cli-flags-test.js b/lighthouse-cli/test/cli/cli-flags-test.js index 431386a0f976..ed7d073acd74 100644 --- a/lighthouse-cli/test/cli/cli-flags-test.js +++ b/lighthouse-cli/test/cli/cli-flags-test.js @@ -29,6 +29,25 @@ describe('CLI bin', function() { }); }); + it('settings are accepted from a file path', () => { + const flags = getFlags([ + 'http://www.example.com', + `--cli-flags-path="${__dirname}/../fixtures/cli-flags-path.json"`, + '--budgets-path=path/to/my/budget-from-command-line.json', // this should override the config value + ].join(' ')); + + expect(flags).toMatchObject({ + budgetsPath: 'path/to/my/budget-from-command-line.json', + onlyCategories: ['performance', 'seo'], + chromeFlags: '--window-size 800,600', + throttlingMethod: 'devtools', + throttling: { + requestLatencyMs: 700, + cpuSlowdownMultiplier: 6, + }, + }); + }); + it('array values support csv when appropriate', () => { const flags = getFlags([ 'http://www.example.com', diff --git a/lighthouse-cli/test/fixtures/cli-flags-path.json b/lighthouse-cli/test/fixtures/cli-flags-path.json new file mode 100644 index 000000000000..7d993c9df5c9 --- /dev/null +++ b/lighthouse-cli/test/fixtures/cli-flags-path.json @@ -0,0 +1,10 @@ +{ + "budgetPath": "path/to/my/budget-from-config.json", + "onlyCategories": ["performance", "seo"], + "chromeFlags": "--window-size 800,600", + "throttling-method": "devtools", + "throttling": { + "requestLatencyMs": 700, + "cpuSlowdownMultiplier": 6 + } +} diff --git a/readme.md b/readme.md index f911924eb001..2d5ba0afa38b 100644 --- a/readme.md +++ b/readme.md @@ -89,6 +89,8 @@ Output: Options: --help Show help [boolean] --version Show version number [boolean] + --cli-flags-path The path to a JSON file that contains the desired CLI flags to apply. + Flags specified at the command line will still override the file-based ones. --blocked-url-patterns Block any network requests to the specified URL patterns [array] --disable-storage-reset Disable clearing the browser cache and other storage APIs before a run [boolean] --throttling-method Controls throttling method [choices: "devtools", "provided", "simulate"]