|
| 1 | +use { |
| 2 | + crate::{config::Config, specifier::Specifier}, |
| 3 | + log::debug, |
| 4 | + serde::Deserialize, |
| 5 | + std::{collections::HashMap, fs, rc::Rc, time::Instant}, |
| 6 | +}; |
| 7 | + |
| 8 | +pub type Catalog = HashMap<String, Rc<Specifier>>; |
| 9 | +pub type CatalogsByName = HashMap<String, Catalog>; |
| 10 | + |
| 11 | +/// Extract catalogs from the project configuration. |
| 12 | +/// |
| 13 | +/// Attempts to read catalog definitions from: |
| 14 | +/// 1. pnpm-workspace.yaml (pnpm catalogs) |
| 15 | +/// 2. package.json at project root (bun catalogs) |
| 16 | +/// |
| 17 | +/// See: |
| 18 | +/// - https://pnpm.io/catalogs |
| 19 | +/// - https://bun.sh/docs/pm/catalogs |
| 20 | +pub fn from_config(config: &Config) -> Option<CatalogsByName> { |
| 21 | + let start = Instant::now(); |
| 22 | + let catalogs = try_from_pnpm(config).or_else(|| try_from_bun(config)); |
| 23 | + debug!("Catalog discovery completed in {:?}", start.elapsed()); |
| 24 | + catalogs |
| 25 | +} |
| 26 | + |
| 27 | +/// Try to read catalogs from pnpm-workspace.yaml |
| 28 | +/// |
| 29 | +/// pnpm supports both: |
| 30 | +/// - `catalog:` (singular) - default catalog |
| 31 | +/// - `catalogs:` (plural) - named catalogs |
| 32 | +/// |
| 33 | +/// Example pnpm-workspace.yaml: |
| 34 | +/// ```yaml |
| 35 | +/// packages: |
| 36 | +/// - 'packages/*' |
| 37 | +/// catalog: |
| 38 | +/// chalk: ^4.1.2 |
| 39 | +/// catalogs: |
| 40 | +/// react16: |
| 41 | +/// react: ^16.7.0 |
| 42 | +/// react-dom: ^16.7.0 |
| 43 | +/// ``` |
| 44 | +fn try_from_pnpm(config: &Config) -> Option<CatalogsByName> { |
| 45 | + let file_path = config.cli.cwd.join("pnpm-workspace.yaml"); |
| 46 | + |
| 47 | + if !file_path.exists() { |
| 48 | + return None; |
| 49 | + } |
| 50 | + |
| 51 | + debug!("Reading catalogs from pnpm-workspace.yaml"); |
| 52 | + |
| 53 | + let contents = fs::read_to_string(&file_path).ok()?; |
| 54 | + let workspace: PnpmWorkspace = serde_yaml::from_str(&contents).ok()?; |
| 55 | + |
| 56 | + let mut catalogs_by_name = CatalogsByName::new(); |
| 57 | + |
| 58 | + // Add default catalog if present |
| 59 | + if let Some(default_catalog) = workspace.catalog { |
| 60 | + let mut catalog = Catalog::new(); |
| 61 | + for (name, version) in default_catalog { |
| 62 | + catalog.insert(name, Specifier::new(&version)); |
| 63 | + } |
| 64 | + if !catalog.is_empty() { |
| 65 | + catalogs_by_name.insert("default".to_string(), catalog); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Add named catalogs if present |
| 70 | + if let Some(named_catalogs) = workspace.catalogs { |
| 71 | + for (catalog_name, dependencies) in named_catalogs { |
| 72 | + let mut catalog = Catalog::new(); |
| 73 | + for (name, version) in dependencies { |
| 74 | + catalog.insert(name, Specifier::new(&version)); |
| 75 | + } |
| 76 | + if !catalog.is_empty() { |
| 77 | + catalogs_by_name.insert(catalog_name, catalog); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if catalogs_by_name.is_empty() { |
| 83 | + debug!("No catalogs found in pnpm-workspace.yaml"); |
| 84 | + None |
| 85 | + } else { |
| 86 | + debug!("Found {} catalog(s) in pnpm-workspace.yaml", catalogs_by_name.len()); |
| 87 | + Some(catalogs_by_name) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// Try to read catalogs from package.json at project root |
| 92 | +/// |
| 93 | +/// Bun supports catalogs defined in the root package.json: |
| 94 | +/// - At top level: `catalog` and `catalogs` |
| 95 | +/// - Under workspaces: `workspaces.catalog` and `workspaces.catalogs` |
| 96 | +/// |
| 97 | +/// Example package.json: |
| 98 | +/// ```json |
| 99 | +/// { |
| 100 | +/// "workspaces": { |
| 101 | +/// "catalog": { |
| 102 | +/// "react": "^19.0.0" |
| 103 | +/// }, |
| 104 | +/// "catalogs": { |
| 105 | +/// "testing": { |
| 106 | +/// "jest": "30.0.0" |
| 107 | +/// } |
| 108 | +/// } |
| 109 | +/// } |
| 110 | +/// } |
| 111 | +/// ``` |
| 112 | +fn try_from_bun(config: &Config) -> Option<CatalogsByName> { |
| 113 | + let file_path = config.cli.cwd.join("package.json"); |
| 114 | + |
| 115 | + if !file_path.exists() { |
| 116 | + return None; |
| 117 | + } |
| 118 | + |
| 119 | + debug!("Reading catalogs from package.json"); |
| 120 | + |
| 121 | + let contents = fs::read_to_string(&file_path).ok()?; |
| 122 | + let package_json: BunPackageJson = serde_json::from_str(&contents).ok()?; |
| 123 | + |
| 124 | + let mut catalogs_by_name = CatalogsByName::new(); |
| 125 | + |
| 126 | + // Try workspaces.catalog and workspaces.catalogs first |
| 127 | + if let Some(workspaces) = package_json.workspaces { |
| 128 | + if let Some(default_catalog) = workspaces.catalog { |
| 129 | + let mut catalog = Catalog::new(); |
| 130 | + for (name, version) in default_catalog { |
| 131 | + catalog.insert(name, Specifier::new(&version)); |
| 132 | + } |
| 133 | + if !catalog.is_empty() { |
| 134 | + catalogs_by_name.insert("default".to_string(), catalog); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if let Some(named_catalogs) = workspaces.catalogs { |
| 139 | + for (catalog_name, dependencies) in named_catalogs { |
| 140 | + let mut catalog = Catalog::new(); |
| 141 | + for (name, version) in dependencies { |
| 142 | + catalog.insert(name, Specifier::new(&version)); |
| 143 | + } |
| 144 | + if !catalog.is_empty() { |
| 145 | + catalogs_by_name.insert(catalog_name, catalog); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Fall back to top-level catalog and catalogs |
| 152 | + if catalogs_by_name.is_empty() { |
| 153 | + if let Some(default_catalog) = package_json.catalog { |
| 154 | + let mut catalog = Catalog::new(); |
| 155 | + for (name, version) in default_catalog { |
| 156 | + catalog.insert(name, Specifier::new(&version)); |
| 157 | + } |
| 158 | + if !catalog.is_empty() { |
| 159 | + catalogs_by_name.insert("default".to_string(), catalog); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if let Some(named_catalogs) = package_json.catalogs { |
| 164 | + for (catalog_name, dependencies) in named_catalogs { |
| 165 | + let mut catalog = Catalog::new(); |
| 166 | + for (name, version) in dependencies { |
| 167 | + catalog.insert(name, Specifier::new(&version)); |
| 168 | + } |
| 169 | + if !catalog.is_empty() { |
| 170 | + catalogs_by_name.insert(catalog_name, catalog); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if catalogs_by_name.is_empty() { |
| 177 | + debug!("No catalogs found in package.json"); |
| 178 | + None |
| 179 | + } else { |
| 180 | + debug!("Found {} catalog(s) in package.json", catalogs_by_name.len()); |
| 181 | + Some(catalogs_by_name) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +#[derive(Debug, Deserialize)] |
| 186 | +struct PnpmWorkspace { |
| 187 | + catalog: Option<HashMap<String, String>>, |
| 188 | + catalogs: Option<HashMap<String, HashMap<String, String>>>, |
| 189 | +} |
| 190 | + |
| 191 | +#[derive(Debug, Deserialize)] |
| 192 | +struct BunPackageJson { |
| 193 | + catalog: Option<HashMap<String, String>>, |
| 194 | + catalogs: Option<HashMap<String, HashMap<String, String>>>, |
| 195 | + workspaces: Option<BunWorkspaces>, |
| 196 | +} |
| 197 | + |
| 198 | +#[derive(Debug, Deserialize)] |
| 199 | +struct BunWorkspaces { |
| 200 | + catalog: Option<HashMap<String, String>>, |
| 201 | + catalogs: Option<HashMap<String, HashMap<String, String>>>, |
| 202 | +} |
0 commit comments