|
| 1 | +use crate::cmd::install::install; |
| 2 | +use crate::helper::global_bin::get_global_package_dir; |
| 3 | +use crate::helper::workspace::update_cwd_to_project; |
| 4 | +use crate::model::package::PackageInfo; |
| 5 | +use crate::util::linker::link; |
| 6 | +use crate::util::logger::log_verbose; |
| 7 | +use anyhow::{Context, Result}; |
| 8 | + |
| 9 | +/// Link current package to global (equivalent to npm link without args) |
| 10 | +pub async fn link_current_to_global(prefix: Option<&str>) -> Result<()> { |
| 11 | + let cwd = std::env::current_dir().context("Failed to get current directory")?; |
| 12 | + let project_path = update_cwd_to_project(&cwd).await?; |
| 13 | + |
| 14 | + // Create package info from path |
| 15 | + let package_info = PackageInfo::from_path(&project_path).with_context(|| { |
| 16 | + format!( |
| 17 | + "Failed to parse package info from path: {}", |
| 18 | + project_path.display() |
| 19 | + ) |
| 20 | + })?; |
| 21 | + |
| 22 | + // Install dependencies |
| 23 | + install(false, &project_path).await.map_err(|e| { |
| 24 | + anyhow::anyhow!("Failed to prepare dependencies for package to link: {}", e) |
| 25 | + })?; |
| 26 | + |
| 27 | + let global_package_path = get_global_package_dir(&prefix)?.join(package_info.name.clone()); |
| 28 | + // link local project to global package |
| 29 | + link(&project_path, &global_package_path).with_context(|| { |
| 30 | + format!( |
| 31 | + "Failed to link local project to global package: {} -> {}", |
| 32 | + project_path.display(), |
| 33 | + global_package_path.display() |
| 34 | + ) |
| 35 | + })?; |
| 36 | + // If the package has binary files, also link them to global bin directory |
| 37 | + if package_info.has_bin_files() { |
| 38 | + let global_bin_dir = global_package_path.join("bin"); |
| 39 | + package_info |
| 40 | + .link_to_target(&global_bin_dir) |
| 41 | + .await |
| 42 | + .with_context(|| { |
| 43 | + format!( |
| 44 | + "Failed to link binary files to global bin directory: {}", |
| 45 | + global_bin_dir.display() |
| 46 | + ) |
| 47 | + })?; |
| 48 | + } |
| 49 | + |
| 50 | + Ok(()) |
| 51 | +} |
| 52 | + |
| 53 | +/// Link a global package to local node_modules (equivalent to npm link pkg_name) |
| 54 | +pub async fn link_global_to_local(package_name: &str, prefix: Option<&str>) -> Result<()> { |
| 55 | + let cwd = std::env::current_dir().context("Failed to get current directory")?; |
| 56 | + let project_path = update_cwd_to_project(&cwd).await?; |
| 57 | + |
| 58 | + let global_package_path = get_global_package_dir(&prefix)?.join(package_name); |
| 59 | + |
| 60 | + // Create package info from path |
| 61 | + let package_info = PackageInfo::from_path(&global_package_path).with_context(|| { |
| 62 | + format!( |
| 63 | + "Failed to parse package info from path: {}", |
| 64 | + global_package_path.display() |
| 65 | + ) |
| 66 | + })?; |
| 67 | + |
| 68 | + // Create symlink from global package to local project |
| 69 | + let local_link_path = project_path.join("node_modules/").join(package_name); |
| 70 | + link(&global_package_path, &local_link_path).with_context(|| { |
| 71 | + format!( |
| 72 | + "Failed to link global package to local: {} -> {}", |
| 73 | + global_package_path.display(), |
| 74 | + local_link_path.display() |
| 75 | + ) |
| 76 | + })?; |
| 77 | + |
| 78 | + log_verbose(&format!( |
| 79 | + "link global package to local: {} -> {}", |
| 80 | + global_package_path.display(), |
| 81 | + project_path.display() |
| 82 | + )); |
| 83 | + |
| 84 | + // If the package has binary files, also link them to target project bin directory |
| 85 | + if package_info.has_bin_files() { |
| 86 | + log_verbose(&format!( |
| 87 | + "Linking binary files to project bin directory: {}", |
| 88 | + package_info.name |
| 89 | + )); |
| 90 | + let bin_dir = project_path.join("node_modules/.bin"); |
| 91 | + package_info |
| 92 | + .link_to_target(&bin_dir) |
| 93 | + .await |
| 94 | + .with_context(|| { |
| 95 | + format!( |
| 96 | + "Failed to link binary files to project bin directory: {}", |
| 97 | + bin_dir.display() |
| 98 | + ) |
| 99 | + })?; |
| 100 | + } |
| 101 | + |
| 102 | + Ok(()) |
| 103 | +} |
| 104 | + |
| 105 | +#[cfg(test)] |
| 106 | +mod tests { |
| 107 | + use super::*; |
| 108 | + use std::fs; |
| 109 | + use tempfile::TempDir; |
| 110 | + |
| 111 | + // helper to write minimal package.json and lock to bypass install flow side effects |
| 112 | + async fn write_minimal_project_files(path: &std::path::Path, name: &str) { |
| 113 | + let pkg = format!( |
| 114 | + r#"{{ |
| 115 | + "name": "{name}", |
| 116 | + "version": "1.0.0" |
| 117 | +}}"# |
| 118 | + ); |
| 119 | + fs::write(path.join("package.json"), pkg).unwrap(); |
| 120 | + let lock = r#"{ |
| 121 | + "name": "test", |
| 122 | + "version": "1.0.0", |
| 123 | + "lockfileVersion": 3, |
| 124 | + "requires": true, |
| 125 | + "packages": { |
| 126 | + "": {"name": "test", "version": "1.0.0"} |
| 127 | + } |
| 128 | +}"#; |
| 129 | + fs::write(path.join("package-lock.json"), lock).unwrap(); |
| 130 | + fs::create_dir_all(path.join("node_modules")).unwrap(); |
| 131 | + } |
| 132 | + |
| 133 | + #[tokio::test] |
| 134 | + async fn test_link_global_to_local_basic() { |
| 135 | + let temp_dir = TempDir::new().unwrap(); |
| 136 | + let project = temp_dir.path().join("proj"); |
| 137 | + let global = temp_dir.path().join("global"); |
| 138 | + fs::create_dir_all(&project).unwrap(); |
| 139 | + fs::create_dir_all(&global).unwrap(); |
| 140 | + |
| 141 | + // minimal project files to bypass install |
| 142 | + write_minimal_project_files(&project, "consumer").await; |
| 143 | + |
| 144 | + // create fake global package dir with package.json |
| 145 | + let pkg_name = "lib-a"; |
| 146 | + let global_pkg_dir = global.join("lib/node_modules").join(pkg_name); |
| 147 | + fs::create_dir_all(&global_pkg_dir).unwrap(); |
| 148 | + let pkg_json = format!( |
| 149 | + r#"{{ |
| 150 | + "name": "{pkg_name}", |
| 151 | + "version": "1.0.0" |
| 152 | +}}"# |
| 153 | + ); |
| 154 | + fs::write(global_pkg_dir.join("package.json"), pkg_json).unwrap(); |
| 155 | + |
| 156 | + // set cwd to project so local node_modules path is resolved |
| 157 | + std::env::set_current_dir(&project).unwrap(); |
| 158 | + |
| 159 | + let prefix_str = global.to_string_lossy().to_string(); |
| 160 | + let result = link_global_to_local(pkg_name, Some(&prefix_str)).await; |
| 161 | + assert!(result.is_ok(), "link_global_to_local should succeed"); |
| 162 | + |
| 163 | + // verify node_modules symlink |
| 164 | + let local_link = project.join("node_modules").join(pkg_name); |
| 165 | + assert!(local_link.exists()); |
| 166 | + assert!(local_link.is_symlink() || local_link.is_dir()); |
| 167 | + } |
| 168 | +} |
0 commit comments