Skip to content

Commit

Permalink
core(cli): accept flags from path (#9109)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored Jun 4, 2019
1 parent 273b4a4 commit 7deb5ab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lighthouse-cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ function getFlags(manualArgv) {
'lighthouse <url> --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({
Expand All @@ -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':
Expand Down
19 changes: 19 additions & 0 deletions lighthouse-cli/test/cli/cli-flags-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 10 additions & 0 deletions lighthouse-cli/test/fixtures/cli-flags-path.json
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 7deb5ab

Please sign in to comment.