-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cli: add --print-config flag #6107
Conversation
const config = generateConfig(cliFlags, configJson); | ||
process.stdout.write(config.print()); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needed to be in the full flow of the cli since it turns out process.stdout.write
(and by extension console.log
) isn't flushed or waited on by process.exit()
, so the exit can come mid write and the JSON output will be truncated. Since index.js
just requires this module (running the top-level code) and calls run()
, this needed some way to prevent getting to run()
without calling process.exit()
, so...here we are.
(and might as well switch to async/await while in here)
if (typeof cliFlags.enableErrorReporting === 'undefined') { | ||
cliFlags.enableErrorReporting = await askPermission(); | ||
} | ||
if (cliFlags.enableErrorReporting) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this check isn't strictly necessary (Sentry.init
itself checks for enableErrorReporting
), but added this just to reassure anyone reading this file and not digging in farther
lighthouse-core/lib/i18n/i18n.js
Outdated
@@ -315,4 +319,5 @@ module.exports = { | |||
createMessageInstanceIdFn, | |||
getFormatted, | |||
replaceIcuMessageInstanceIds, | |||
MESSAGE_INSTANCE_ID_REGEX, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for testing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comment that it's exported only for testing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comment that it's exported only for testing?
I added an isIcuMessage()
instead, just because we do the same check a couple of times internally, so nice in there and nicely encapsulated externally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i like the idea.
lighthouse-cli/cli-flags.js
Outdated
if (!isListMode && !isOnlyAuditMode && argv._.length === 0) { | ||
throw new Error('Please provide a url'); | ||
const isPrintConfigMode = argv.printConfig; | ||
if (isListMode || isOnlyAuditMode || isPrintConfigMode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think printConfigMode is actually captured under the spirit of the isListMode
variable. should be renamed though. doPrintAndQuit
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
lighthouse-core/index.js
Outdated
* not present, the default config is used. | ||
* @return {Config} | ||
*/ | ||
function generateConfig(flags, configJson) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the order here is unexpected to me. i know lighthouse's signature matches this order, but ... can we switch to make it the same as the Config constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the order here is unexpected to me. i know lighthouse's signature matches this order, but ... can we switch to make it the same as the Config constructor
yeah, that's fine.
I think there's also a strong YAGNI argument for not making this a method at all and just have the CLI pull in Config
, but it's nice not having the Config
constructor in our public API, and in theory it's also nice to say "just give me a config file in the exact way core makes one"...it's just that that (currently?) just involves calling the constructor
lighthouse-core/lib/i18n/i18n.js
Outdated
@@ -315,4 +319,5 @@ module.exports = { | |||
createMessageInstanceIdFn, | |||
getFormatted, | |||
replaceIcuMessageInstanceIds, | |||
MESSAGE_INSTANCE_ID_REGEX, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comment that it's exported only for testing?
lighthouse-core/lib/i18n/i18n.js
Outdated
* Recursively walk the input object, looking for property values that are | ||
* string references and replace them with their localized values. Primarily | ||
* used with the full LHR as input. | ||
* @param {*} lhr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not {LH.Result}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not
{LH.Result}
?
I pass in the config now to get a translated one. I can rename the param to make that more obvious
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks print-y good to me! ;)
lighthouse-core/config/config.js
Outdated
* Audit `implementation` and `instance` do not survive this process. | ||
* @return {string} | ||
*/ | ||
print() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚲 🏠 : print
makes me think it will print it not just return the string :)
formatAsString
getPrintString
stringify
getDisplayString
any of those strike your fancy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
going with getPrintString
unless anyone has other votes :) I don't care a whole lot, though it would be nice if whatever we choose communicates that the string result was functional in nature, not just pretty printing or something
assert.ok(localizableCount > 0); | ||
}); | ||
|
||
it('prints a valid ConfigJson that can make an identical Config', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ this test
const ret = spawnSync('node', [indexPath, '--print-config'], {encoding: 'utf8'}); | ||
|
||
const config = JSON.parse(ret.stdout); | ||
assert.strictEqual(config.settings.output[0], 'html'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would actually be a good candidate for snapshot testing, but I know you're not a fan :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good candidate for snapshot testing, but I know you're not a fan
I'd make my opinion more nuanced in that if there's constant snapshot churn they lose efficacy as a unit test because we stop paying attention, but I agree this is a good candidate (default config shouldn't change too much...) and less awkward than this style of checking.
We do lose the immediacy of the 'html' here vs the 'json' in the test below (and auditMode
true vs false), but that might be ok(ish).
I'll add it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I'm not a fan of is expect(result).to.not.be.checked.in.a.ridiculous('manner')
:P:P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haha alright, very fair. jest isn't big on the dot chains though! at least not nearly as bad as chai
finally the last step fixing #1826 (more or less :)
after
Config
takes the input config JSON and extends it, merges passes, concats audit arrays, filters for--only-audits
, merges options, etc, it can be difficult to visualize exactly what they end result will be. This flag makes that simple by printing the full, normalized config to stdout.The only real issue with JSON.stringifying the config is the live
implementation
properties in audits and gatherers (as well asinstance
in gatherers), which can't be stringified. These are skipped.However, if audits/gatherers were specified with a
path
(like all the core configs do), the output of--print-config
is a validLH.Config.Json
and can be c/p into a new config file and used to create an identical LH run.Currently not implemented possibilities:
path
, some kind of string replacement for the liveimplementation
andinstance
properties to at least indicate that they were there (and throw an error if you pass it back intoConfig
). Basically a more informative[object Object]
. Liveimplementation
seems mostly used for testing Lighthouse itself, though, so this may be necessary.config.js
to help in these cases alreadyI've also eliminated any empty audit/gatherer
options
objects, as they're noisy in the output, they're all empty, and they're optional in aLH.Config.Json
anyways (they're immediately replaced with the same{}
when normalized).Happy to bikeshed on anything!