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.
Summary
Add new subcommand,
yarn replace
, which removes one package and adds one package in one go.Motivation
Often, developers find themselves in situations where they try out one package just to find that the package is not useful for them and they want to try another package instead.
Currently, the workflow for this is to execute
yarn remove <old-package>
andyarn add <new-package>
sequentially. By having to execute two commands, native packages recompilation and other redundant tasks have to be executed twice, resulting in a timewaste.I believe
yarn remove <old-package> && yarn add <new-package>
is something that developers do commonly. Having a shorthand for these types of operation would also save time for typing.Detailed design
I propose a new subcommand called
replace <old-package> <new-package>
.For example:
yarn replace leftpad left-pad
.The flow for this command is like this:
yarn remove <old-package>
. Don't rebuild native dependencies in this step.– Execute
preinstall
lifecycle if needed.yarn remove <old-package> && yarn add <new-package>
. Save the lockfile. Executepostinstall
lifecycle if needed.Flags
The supported flags for
yarn add
andyarn remove
are the same, execeptyarn add
has these flags:--ignore-workspace-root-check
,--dev
,--peer
,--optional
,--exact
,--tilde
.yarn replace
should honor all flags thatyarn add
honors. If the flag is supported byyarn remove
as well, it should also be applied to the package removal section of the process. Otherwise, the flag just gets applied to the package installation process instead.How We Teach This
yarn replace
is a convienience command for advanced users and is not a core feature of yarn.I see this feature in a similar category as
yarn upgrade-interactive
, a command that is not essential, but is nice to have.As this command is optional to use for developers, it is not necessary to teach novice users this command. However, advanced users who would like to get efficient when using Yarn, can read up on this command in the documentation
Drawbacks
yarn add
/yarn remove
API might not make sense in ayarn replace
context.Alternatives
N/A
Unresolved questions
Which parts of
yarn remove
andyarn add
are exactly redundant? I can think of:– Bootup of Yarn
– Writing of Lockfile
– Writing of package.json
– Rebuilding of dependencies.