forked from
gleam.run/gleam
⭐️ A friendly language for building type-safe, scalable systems!
2.0 kB
61 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Gleam contributors
3
4use gleam_core::{
5 Error, Result,
6 error::{FileIoAction, FileKind},
7 paths::ProjectPaths,
8};
9
10use crate::{cli, fs};
11
12pub fn command(paths: &ProjectPaths, packages: Vec<String>) -> Result<()> {
13 // Read gleam.toml so we can remove deps from it
14 let root_config = paths.root_config();
15 let mut toml = fs::read(&root_config)?
16 .parse::<toml_edit::DocumentMut>()
17 .map_err(|e| Error::FileIo {
18 kind: FileKind::File,
19 action: FileIoAction::Parse,
20 path: root_config.to_path_buf(),
21 err: Some(e.to_string()),
22 })?;
23
24 // Remove the specified dependencies
25 let mut packages_not_exist = vec![];
26 for package_to_remove in packages.iter() {
27 let remove = |toml: &mut toml_edit::DocumentMut, name| {
28 #[allow(clippy::indexing_slicing)]
29 toml[name]
30 .as_table_like_mut()
31 .and_then(|deps| deps.remove(package_to_remove))
32 };
33
34 // dev-dependencies is the old deprecated name for dev_dependencies
35 let removed = remove(&mut toml, "dependencies")
36 .or_else(|| remove(&mut toml, "dev_dependencies"))
37 .or_else(|| remove(&mut toml, "dev-dependencies"));
38
39 if removed.is_none() {
40 packages_not_exist.push(package_to_remove.into());
41 }
42 }
43
44 if !packages_not_exist.is_empty() {
45 return Err(Error::RemovedPackagesNotExist {
46 packages: packages_not_exist,
47 });
48 }
49
50 // Write the updated config
51 fs::write(root_config.as_path(), &toml.to_string())?;
52
53 // If there's no manifest yet (e.g. the project has never been built) we
54 // don't need to clean it up. `cleanup` reads manifest.toml unconditionally
55 // and that failed with a confusing "File IO failure" before this guard.
56 if paths.manifest().exists() {
57 _ = crate::dependencies::cleanup(paths, cli::Reporter::new())?;
58 }
59
60 Ok(())
61}