-
Notifications
You must be signed in to change notification settings - Fork 243
feat(pm): add patch and patch-commit commands
#2308
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aa0de4c
feat(pm): add `patch` and `patch-commit` commands
jong-kyung e74e46a
test(snapshots): cover pm patch commands
jong-kyung d3b7862
test(snapshots): make pm patch cases deterministic
jong-kyung 782b700
fix(pm): save yarn berry patches during patch-commit
jong-kyung 228e7fb
test(snapshots): drop the error-path yarn berry patch case
jong-kyung ffd3bc9
test(snapshots): drop the platform-dependent bun patch case
jong-kyung 1b59a64
Merge branch 'main' into feat/pm-patch
jong-kyung cdff7bc
Merge branch 'main' into feat/pm-patch
jong-kyung 719aed6
Merge branch 'main' into feat/pm-patch
fengmk2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crates/vite_cli_snapshots/tests/cli_snapshots/fixtures/pm_patch/npm/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "pm-patch-npm", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "license": "MIT", | ||
| "packageManager": "npm@11.11.1" | ||
| } |
7 changes: 7 additions & 0 deletions
7
crates/vite_cli_snapshots/tests/cli_snapshots/fixtures/pm_patch/pnpm/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "pm-patch-pnpm", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "license": "MIT", | ||
| "packageManager": "pnpm@10.34.4" | ||
| } |
26 changes: 26 additions & 0 deletions
26
crates/vite_cli_snapshots/tests/cli_snapshots/fixtures/pm_patch/snapshots.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| [[case]] | ||
| name = "pm_patch_npm" | ||
| vp = "local" | ||
| cwd = "npm" | ||
| steps = [ | ||
| { argv = ["vp", "pm", "patch", "placeholder"], comment = "npm warns and exits successfully because patch is unsupported" }, | ||
| { argv = ["vp", "pm", "patch-commit", "placeholder"], comment = "npm warns and exits successfully because patch-commit is unsupported" }, | ||
| ] | ||
|
|
||
| [[case]] | ||
| name = "pm_patch_pnpm" | ||
| vp = "local" | ||
| cwd = "pnpm" | ||
| steps = [ | ||
| { argv = ["vp", "pm", "patch", "placeholder", "--", "--version"], comment = "pnpm receives the patch command" }, | ||
| { argv = ["vp", "pm", "patch-commit", "placeholder", "--", "--version"], comment = "pnpm receives the patch-commit command" }, | ||
| ] | ||
|
|
||
| [[case]] | ||
| name = "pm_patch_yarn" | ||
| vp = "local" | ||
| cwd = "yarn" | ||
| steps = [ | ||
| { argv = ["vp", "pm", "patch", "placeholder"], comment = "Yarn Classic warns and exits successfully because patch is unsupported" }, | ||
| { argv = ["vp", "pm", "patch-commit", "placeholder"], comment = "Yarn Classic warns and exits successfully because patch-commit is unsupported" }, | ||
| ] | ||
17 changes: 17 additions & 0 deletions
17
...e_cli_snapshots/tests/cli_snapshots/fixtures/pm_patch/snapshots/pm_patch_npm.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # pm_patch_npm | ||
|
|
||
| ## `vp pm patch placeholder` | ||
|
|
||
| npm warns and exits successfully because patch is unsupported | ||
|
|
||
| ``` | ||
| warn: npm does not have a 'patch' command. | ||
| ``` | ||
|
|
||
| ## `vp pm patch-commit placeholder` | ||
|
|
||
| npm warns and exits successfully because patch-commit is unsupported | ||
|
|
||
| ``` | ||
| warn: npm does not have a 'patch-commit' command. | ||
| ``` |
17 changes: 17 additions & 0 deletions
17
..._cli_snapshots/tests/cli_snapshots/fixtures/pm_patch/snapshots/pm_patch_pnpm.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # pm_patch_pnpm | ||
|
|
||
| ## `vp pm patch placeholder -- --version` | ||
|
|
||
| pnpm receives the patch command | ||
|
|
||
| ``` | ||
| 10.34.4 | ||
| ``` | ||
|
|
||
| ## `vp pm patch-commit placeholder -- --version` | ||
|
|
||
| pnpm receives the patch-commit command | ||
|
|
||
| ``` | ||
| 10.34.4 | ||
| ``` |
17 changes: 17 additions & 0 deletions
17
..._cli_snapshots/tests/cli_snapshots/fixtures/pm_patch/snapshots/pm_patch_yarn.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # pm_patch_yarn | ||
|
|
||
| ## `vp pm patch placeholder` | ||
|
|
||
| Yarn Classic warns and exits successfully because patch is unsupported | ||
|
|
||
| ``` | ||
| warn: yarn classic does not have a 'patch' command. | ||
| ``` | ||
|
|
||
| ## `vp pm patch-commit placeholder` | ||
|
|
||
| Yarn Classic warns and exits successfully because patch-commit is unsupported | ||
|
|
||
| ``` | ||
| warn: yarn classic does not have a 'patch-commit' command. | ||
| ``` |
7 changes: 7 additions & 0 deletions
7
crates/vite_cli_snapshots/tests/cli_snapshots/fixtures/pm_patch/yarn/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "pm-patch-yarn", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "license": "MIT", | ||
| "packageManager": "yarn@1.22.22" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| use vite_pm_cli_macros::pm_args; | ||
|
|
||
| use crate::resolution::{ | ||
| Bun, CommandBuilder, CommandResolution, DiagnosticKind, Diagnostics, Npm, Pnpm, Resolve, Yarn, | ||
| }; | ||
|
|
||
| #[pm_args] | ||
| #[derive(clap::Args, Clone, Debug, Default, PartialEq, Eq)] | ||
| pub struct PatchArgs { | ||
| /// Package to patch | ||
| pub(crate) package: String, | ||
|
|
||
| /// Additional arguments | ||
| #[arg(last = true, allow_hyphen_values = true)] | ||
| pub(crate) pass_through_args: Vec<String>, | ||
| } | ||
|
|
||
| impl Resolve<PatchArgs> for Pnpm { | ||
| fn resolve(&self, args: &PatchArgs, _diag: &mut Diagnostics) -> CommandResolution { | ||
| let mut cmd = CommandBuilder::new("pnpm"); | ||
| cmd.arg("patch").arg(&args.package).extend(args.pass_through_args.iter()); | ||
| cmd.into() | ||
| } | ||
| } | ||
|
|
||
| impl Resolve<PatchArgs> for Npm { | ||
| fn resolve(&self, _args: &PatchArgs, diag: &mut Diagnostics) -> CommandResolution { | ||
| diag.warn(DiagnosticKind::UnsupportedCommandNoop, "npm does not have a 'patch' command."); | ||
| CommandResolution::Noop | ||
| } | ||
| } | ||
|
|
||
| impl Resolve<PatchArgs> for Yarn { | ||
| fn resolve(&self, args: &PatchArgs, diag: &mut Diagnostics) -> CommandResolution { | ||
| if !self.is_berry() { | ||
| diag.warn( | ||
| DiagnosticKind::UnsupportedCommandNoop, | ||
| "yarn classic does not have a 'patch' command.", | ||
| ); | ||
| return CommandResolution::Noop; | ||
| } | ||
| let mut cmd = CommandBuilder::new("yarn"); | ||
| cmd.arg("patch").arg(&args.package).extend(args.pass_through_args.iter()); | ||
| cmd.into() | ||
| } | ||
| } | ||
|
|
||
| impl Resolve<PatchArgs> for Bun { | ||
| fn resolve(&self, args: &PatchArgs, _diag: &mut Diagnostics) -> CommandResolution { | ||
| let mut cmd = CommandBuilder::new("bun"); | ||
| cmd.arg("patch").arg(&args.package).extend(args.pass_through_args.iter()); | ||
| cmd.into() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::resolution::{ | ||
| resolve, | ||
| test_utils::{bun, npm, parse_args, pnpm, yarn}, | ||
| }; | ||
|
|
||
| fn patch_args(package: &str) -> PatchArgs { | ||
| PatchArgs { package: package.to_string(), ..Default::default() } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_pnpm_patch() { | ||
| let CommandResolution::Run(command) = | ||
| resolve(&pnpm("10.0.0"), patch_args("left-pad")).outcome | ||
| else { | ||
| panic!("expected command resolution"); | ||
| }; | ||
|
|
||
| assert_eq!(command.program, "pnpm"); | ||
| assert_eq!(command.args, vec!["patch", "left-pad"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_yarn_berry_patch() { | ||
| let CommandResolution::Run(command) = | ||
| resolve(&yarn("4.0.0"), patch_args("left-pad")).outcome | ||
| else { | ||
| panic!("expected command resolution"); | ||
| }; | ||
|
|
||
| assert_eq!(command.program, "yarn"); | ||
| assert_eq!(command.args, vec!["patch", "left-pad"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_bun_patch() { | ||
| let CommandResolution::Run(command) = | ||
| resolve(&bun("1.3.11"), patch_args("left-pad")).outcome | ||
| else { | ||
| panic!("expected command resolution"); | ||
| }; | ||
|
|
||
| assert_eq!(command.program, "bun"); | ||
| assert_eq!(command.args, vec!["patch", "left-pad"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_npm_patch_not_supported() { | ||
| let result = resolve(&npm("11.0.0"), patch_args("left-pad")); | ||
|
|
||
| assert_eq!(result.outcome, CommandResolution::Noop); | ||
| assert_eq!(result.diagnostics.len(), 1); | ||
| assert_eq!(result.diagnostics[0].kind, DiagnosticKind::UnsupportedCommandNoop); | ||
| assert_eq!(result.diagnostics[0].message, "npm does not have a 'patch' command."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_yarn_classic_patch_not_supported() { | ||
| let result = resolve(&yarn("1.22.22"), patch_args("left-pad")); | ||
|
|
||
| assert_eq!(result.outcome, CommandResolution::Noop); | ||
| assert_eq!(result.diagnostics.len(), 1); | ||
| assert_eq!(result.diagnostics[0].kind, DiagnosticKind::UnsupportedCommandNoop); | ||
| assert_eq!(result.diagnostics[0].message, "yarn classic does not have a 'patch' command."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_patch_with_pass_through_args() { | ||
| let CommandResolution::Run(command) = resolve( | ||
| &pnpm("10.0.0"), | ||
| PatchArgs { | ||
| package: "left-pad".to_string(), | ||
| pass_through_args: vec!["--edit-dir".to_string(), ".patches".to_string()], | ||
| }, | ||
| ) | ||
| .outcome | ||
| else { | ||
| panic!("expected command resolution"); | ||
| }; | ||
|
|
||
| assert_eq!(command.args, vec!["patch", "left-pad", "--edit-dir", ".patches"]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parser_captures_package_and_pass_through_args() { | ||
| let args = parse_args::<PatchArgs>(["left-pad", "--", "--edit-dir", ".patches"]).unwrap(); | ||
|
|
||
| assert_eq!(args.package, "left-pad"); | ||
| assert_eq!(args.pass_through_args, vec!["--edit-dir".to_string(), ".patches".to_string()]); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.