Skip to content

Commit 5ae212e

Browse files
committed
Clean up lockfile and subgraph methods
1 parent af05589 commit 5ae212e

File tree

1 file changed

+60
-54
lines changed
  • crates/turborepo-lockfiles/src/bun

1 file changed

+60
-54
lines changed

crates/turborepo-lockfiles/src/bun/mod.rs

+60-54
Original file line numberDiff line numberDiff line change
@@ -215,55 +215,49 @@ impl BunLockfile {
215215
Some((key, entry))
216216
}
217217

218-
pub fn lockfile(&self) -> Result<BunLockfileData, Error> {
219-
Ok(BunLockfileData {
220-
lockfile_version: self.data.lockfile_version,
221-
workspaces: self.data.workspaces.clone(),
222-
packages: self.data.packages.clone(),
223-
patched_dependencies: self.data.patched_dependencies.clone(),
224-
})
218+
pub fn lockfile(self) -> Result<BunLockfileData, Error> {
219+
Ok(self.data)
225220
}
226221

227222
fn subgraph(
228223
&self,
229224
workspace_packages: &[String],
230225
packages: &[String],
231226
) -> Result<BunLockfile, Error> {
232-
let mut new_packages = Map::new();
233-
let mut new_workspaces = Map::new();
234-
let mut new_patched_dependencies = Map::new();
235-
236-
for package in packages {
237-
let package_key = self
238-
.key_to_entry
239-
.get(package)
240-
.expect("package key should exist");
241-
if let Some((key, entry)) = self.package_entry(&package_key) {
242-
new_packages.insert(key.to_string(), entry.clone());
243-
}
244-
}
245-
246-
// The root workspace is "" in the lockfile
247-
let root_workspace = self
248-
.data
249-
.workspaces
250-
.get("")
251-
.expect("root workspace should exist");
252-
new_workspaces.insert("".to_string(), root_workspace.clone());
253-
254-
for workspace in workspace_packages {
255-
let workspace_entry = self.data.workspaces.get(workspace);
256-
257-
if let Some(workspace_entry) = workspace_entry {
258-
new_workspaces.insert(workspace.to_string(), workspace_entry.clone());
259-
}
260-
}
261-
262-
for (key, patch) in self.data.patched_dependencies.iter() {
263-
if packages.contains(&key) {
264-
new_patched_dependencies.insert(key.to_string(), patch.to_string());
265-
}
266-
}
227+
let new_workspaces = workspace_packages
228+
.iter()
229+
// Add the root workspace, which is always indexed by ""
230+
.chain(std::iter::once(&"".to_string()))
231+
.map(|ws| {
232+
(
233+
ws.to_string(),
234+
self.data
235+
.workspaces
236+
.get(ws)
237+
.expect("workspace should exist")
238+
.clone(),
239+
)
240+
})
241+
.collect();
242+
243+
let new_packages: Map<_, _> = packages
244+
.iter()
245+
.filter_map(|package| {
246+
let key = self.key_to_entry.get(package)?;
247+
self.package_entry(key)
248+
.map(|(k, v)| (k.to_string(), v.clone()))
249+
})
250+
.collect();
251+
252+
let new_patched_dependencies = new_packages
253+
.values()
254+
.filter_map(|entry| {
255+
self.data
256+
.patched_dependencies
257+
.get(&entry.ident)
258+
.map(|patch| (entry.ident.clone(), patch.clone()))
259+
})
260+
.collect();
267261

268262
Ok(Self {
269263
data: BunLockfileData {
@@ -378,10 +372,14 @@ mod test {
378372
.unwrap();
379373
let subgraph_data = subgraph.lockfile().unwrap();
380374

381-
assert!(subgraph_data
382-
.packages
383-
.get("is-odd")
384-
.is_some_and(|pkg| pkg.ident == "[email protected]"));
375+
assert_eq!(
376+
subgraph_data
377+
.packages
378+
.iter()
379+
.map(|(key, pkg)| (key.as_str(), pkg.ident.as_str()))
380+
.collect::<Vec<_>>(),
381+
vec![("is-odd", "[email protected]")]
382+
);
385383
assert_eq!(
386384
subgraph_data.workspaces.keys().collect::<Vec<_>>(),
387385
vec!["", "apps/docs"]
@@ -396,10 +394,14 @@ mod test {
396394
.unwrap();
397395
let subgraph_a_data = subgraph_a.lockfile().unwrap();
398396

399-
assert!(subgraph_a_data
400-
.packages
401-
.get("is-odd")
402-
.is_some_and(|pkg| pkg.ident == "[email protected]"));
397+
assert_eq!(
398+
subgraph_a_data
399+
.packages
400+
.iter()
401+
.map(|(key, pkg)| (key.as_str(), pkg.ident.as_str()))
402+
.collect::<Vec<_>>(),
403+
vec![("is-odd", "[email protected]")]
404+
);
403405
assert_eq!(
404406
subgraph_a_data.workspaces.keys().collect::<Vec<_>>(),
405407
vec!["", "apps/a"]
@@ -410,10 +412,14 @@ mod test {
410412
.unwrap();
411413
let subgraph_b_data = subgraph_b.lockfile().unwrap();
412414

413-
assert!(subgraph_b_data
414-
.packages
415-
.get("b/is-odd")
416-
.is_some_and(|pkg| pkg.ident == "[email protected]"));
415+
assert_eq!(
416+
subgraph_b_data
417+
.packages
418+
.iter()
419+
.map(|(key, pkg)| (key.as_str(), pkg.ident.as_str()))
420+
.collect::<Vec<_>>(),
421+
vec![("b/is-odd", "[email protected]")]
422+
);
417423
assert_eq!(
418424
subgraph_b_data.workspaces.keys().collect::<Vec<_>>(),
419425
vec!["", "apps/b"]

0 commit comments

Comments
 (0)