♻️ Refactor @percy/cli to use @percy/cli-command #676
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What is this?
This PR refactors
@percy/cli
to use the new@percy/cli-command
foundation.Historically, this package was used as the entry point to Percy CLI and its various commands through oclif plugins, which are defined by the
package.json
file'soclif
entry. To support autoloading of plugins found within a project's dependencies, this package was adapted to modify its own package.json at load time before oclif reads it.This alleviated requiring oclif's plugins plugin to manually install plugins and allowed us to rely on Node's normal module resolution. However, in part due to oclif's plugin loading, and in part due to our own plugin discovery, the CLI only worked with Node's native module loading (and not plug-n-play environments).
With the new
@percy/cli-command
as a foundation, oclif can be peeled away to give us complete control over plugin loading. This is done by using a function as thecommands
definition argument which when called will search for plugins within the projects dependencies as well as this packages own and sibling dependencies.For environments with a native module loading, it will walk through top-level directories within
node_modules
(and recurse into the@percy
scope). For Yarn 2+, it will use the PnP API to find declared package dependencies. Matching CLI plugins will be named either@percy/cli-
orpercy-cli-
and contain apackage.json
entry for@percy/cli
. And for legacy Percy oclif plugins, command classes are found and transformed into functional commands using thelegacyCommand
function from@percy/cli-command
.