1use gleam_core::{
2 Error, Result,
3 error::{FileIoAction, FileKind},
4 paths::ProjectPaths,
5};
6
7use crate::{cli, fs};
8
9pub fn command(paths: &ProjectPaths, packages: Vec<String>) -> Result<()> {
10 // Read gleam.toml so we can remove deps from it
11 let root_config = paths.root_config();
12 let mut toml = fs::read(&root_config)?
13 .parse::<toml_edit::DocumentMut>()
14 .map_err(|e| Error::FileIo {
15 kind: FileKind::File,
16 action: FileIoAction::Parse,
17 path: root_config.to_path_buf(),
18 err: Some(e.to_string()),
19 })?;
20
21 // Remove the specified dependencies
22 let mut packages_not_exist = vec![];
23 for package_to_remove in packages.iter() {
24 #[allow(clippy::indexing_slicing)]
25 let maybe_removed_item = toml["dependencies"]
26 .as_table_like_mut()
27 .and_then(|deps| deps.remove(package_to_remove));
28
29 #[allow(clippy::indexing_slicing)]
30 let maybe_removed_dev_item = toml["dev-dependencies"]
31 .as_table_like_mut()
32 .and_then(|deps| deps.remove(package_to_remove));
33
34 if maybe_removed_item.or(maybe_removed_dev_item).is_none() {
35 packages_not_exist.push(package_to_remove.into());
36 }
37 }
38
39 if !packages_not_exist.is_empty() {
40 return Err(Error::RemovedPackagesNotExist {
41 packages: packages_not_exist,
42 });
43 }
44
45 // Write the updated config
46 fs::write(root_config.as_path(), &toml.to_string())?;
47 _ = crate::dependencies::cleanup(paths, cli::Reporter::new())?;
48
49 for package_to_remove in packages {
50 cli::print_removed(&package_to_remove);
51 }
52
53 Ok(())
54}