|
| 1 | +use std::{collections::HashMap, process::ExitStatus}; |
| 2 | + |
| 3 | +use vite_error::Error; |
| 4 | +use vite_path::AbsolutePath; |
| 5 | + |
| 6 | +use crate::package_manager::{ |
| 7 | + PackageManager, PackageManagerType, ResolveCommandResult, format_path_env, run_command, |
| 8 | +}; |
| 9 | + |
| 10 | +/// Options for the dedupe command. |
| 11 | +#[derive(Debug, Default)] |
| 12 | +pub struct DedupeCommandOptions<'a> { |
| 13 | + pub check: bool, |
| 14 | + pub pass_through_args: Option<&'a [String]>, |
| 15 | +} |
| 16 | + |
| 17 | +impl PackageManager { |
| 18 | + /// Run the dedupe command with the package manager. |
| 19 | + /// Return the exit status of the command. |
| 20 | + #[must_use] |
| 21 | + pub async fn run_dedupe_command( |
| 22 | + &self, |
| 23 | + options: &DedupeCommandOptions<'_>, |
| 24 | + cwd: impl AsRef<AbsolutePath>, |
| 25 | + ) -> Result<ExitStatus, Error> { |
| 26 | + let resolve_command = self.resolve_dedupe_command(options); |
| 27 | + run_command(&resolve_command.bin_path, &resolve_command.args, &resolve_command.envs, cwd) |
| 28 | + .await |
| 29 | + } |
| 30 | + |
| 31 | + /// Resolve the dedupe command. |
| 32 | + #[must_use] |
| 33 | + pub fn resolve_dedupe_command(&self, options: &DedupeCommandOptions) -> ResolveCommandResult { |
| 34 | + let bin_name: String; |
| 35 | + let envs = HashMap::from([("PATH".to_string(), format_path_env(self.get_bin_prefix()))]); |
| 36 | + let mut args: Vec<String> = Vec::new(); |
| 37 | + |
| 38 | + match self.client { |
| 39 | + PackageManagerType::Pnpm => { |
| 40 | + bin_name = "pnpm".into(); |
| 41 | + args.push("dedupe".into()); |
| 42 | + |
| 43 | + // pnpm uses --check for dry-run |
| 44 | + if options.check { |
| 45 | + args.push("--check".into()); |
| 46 | + } |
| 47 | + } |
| 48 | + PackageManagerType::Yarn => { |
| 49 | + bin_name = "yarn".into(); |
| 50 | + args.push("dedupe".into()); |
| 51 | + |
| 52 | + // yarn@2+ supports --check |
| 53 | + if options.check { |
| 54 | + args.push("--check".into()); |
| 55 | + } |
| 56 | + } |
| 57 | + PackageManagerType::Npm => { |
| 58 | + bin_name = "npm".into(); |
| 59 | + args.push("dedupe".into()); |
| 60 | + |
| 61 | + if options.check { |
| 62 | + args.push("--dry-run".into()); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Add pass-through args |
| 68 | + if let Some(pass_through_args) = options.pass_through_args { |
| 69 | + args.extend_from_slice(pass_through_args); |
| 70 | + } |
| 71 | + |
| 72 | + ResolveCommandResult { bin_path: bin_name, args, envs } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod tests { |
| 78 | + use tempfile::{TempDir, tempdir}; |
| 79 | + use vite_path::AbsolutePathBuf; |
| 80 | + use vite_str::Str; |
| 81 | + |
| 82 | + use super::*; |
| 83 | + |
| 84 | + fn create_temp_dir() -> TempDir { |
| 85 | + tempdir().expect("Failed to create temp directory") |
| 86 | + } |
| 87 | + |
| 88 | + fn create_mock_package_manager(pm_type: PackageManagerType, version: &str) -> PackageManager { |
| 89 | + let temp_dir = create_temp_dir(); |
| 90 | + let temp_dir_path = AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap(); |
| 91 | + let install_dir = temp_dir_path.join("install"); |
| 92 | + |
| 93 | + PackageManager { |
| 94 | + client: pm_type, |
| 95 | + package_name: pm_type.to_string().into(), |
| 96 | + version: Str::from(version), |
| 97 | + hash: None, |
| 98 | + bin_name: pm_type.to_string().into(), |
| 99 | + workspace_root: temp_dir_path.clone(), |
| 100 | + install_dir, |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_pnpm_dedupe_basic() { |
| 106 | + let pm = create_mock_package_manager(PackageManagerType::Pnpm, "10.0.0"); |
| 107 | + let result = pm.resolve_dedupe_command(&DedupeCommandOptions { ..Default::default() }); |
| 108 | + assert_eq!(result.bin_path, "pnpm"); |
| 109 | + assert_eq!(result.args, vec!["dedupe"]); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn test_pnpm_dedupe_check() { |
| 114 | + let pm = create_mock_package_manager(PackageManagerType::Pnpm, "10.0.0"); |
| 115 | + let result = |
| 116 | + pm.resolve_dedupe_command(&DedupeCommandOptions { check: true, ..Default::default() }); |
| 117 | + assert_eq!(result.bin_path, "pnpm"); |
| 118 | + assert_eq!(result.args, vec!["dedupe", "--check"]); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn test_npm_dedupe_basic() { |
| 123 | + let pm = create_mock_package_manager(PackageManagerType::Npm, "11.0.0"); |
| 124 | + let result = pm.resolve_dedupe_command(&DedupeCommandOptions { ..Default::default() }); |
| 125 | + assert_eq!(result.args, vec!["dedupe"]); |
| 126 | + assert_eq!(result.bin_path, "npm"); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_npm_dedupe_check() { |
| 131 | + let pm = create_mock_package_manager(PackageManagerType::Npm, "11.0.0"); |
| 132 | + let result = |
| 133 | + pm.resolve_dedupe_command(&DedupeCommandOptions { check: true, ..Default::default() }); |
| 134 | + assert_eq!(result.args, vec!["dedupe", "--dry-run"]); |
| 135 | + assert_eq!(result.bin_path, "npm"); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_yarn_dedupe_basic() { |
| 140 | + let pm = create_mock_package_manager(PackageManagerType::Yarn, "4.0.0"); |
| 141 | + let result = pm.resolve_dedupe_command(&DedupeCommandOptions { ..Default::default() }); |
| 142 | + assert_eq!(result.args, vec!["dedupe"]); |
| 143 | + assert_eq!(result.bin_path, "yarn"); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn test_yarn_dedupe_check() { |
| 148 | + let pm = create_mock_package_manager(PackageManagerType::Yarn, "4.0.0"); |
| 149 | + let result = |
| 150 | + pm.resolve_dedupe_command(&DedupeCommandOptions { check: true, ..Default::default() }); |
| 151 | + assert_eq!(result.args, vec!["dedupe", "--check"]); |
| 152 | + assert_eq!(result.bin_path, "yarn"); |
| 153 | + } |
| 154 | +} |
0 commit comments