Skip to main content

wapm_cli/commands/
remove.rs

1//! Code pertaining to the `remove` subcommand: it removes dependencies
2//! from the manifest.
3
4use crate::data::manifest::Manifest;
5use structopt::StructOpt;
6use thiserror::Error;
7
8/// Options for the `remove` subcommand
9#[derive(StructOpt, Debug)]
10pub struct RemoveOpt {
11    packages: Vec<String>,
12}
13
14#[derive(Debug, Error)]
15enum RemoveError {
16    #[error("There were problems removing packages")]
17    GenericError,
18    #[error("No packages to remove; could not find a manifest in the current directory")]
19    NoManifest,
20    #[error("No packages listed to remove")]
21    ArgumentsRequired,
22}
23
24/// Run the remove command
25pub fn remove(options: RemoveOpt) -> anyhow::Result<()> {
26    let mut error = false;
27    let mut manifest: Manifest = {
28        let cur_dir = crate::config::Config::get_current_dir()?;
29        Manifest::find_in_directory(cur_dir).map_err(|_| RemoveError::NoManifest)?
30    };
31
32    if options.packages.is_empty() {
33        return Err(RemoveError::ArgumentsRequired.into());
34    }
35
36    for package_name in options.packages {
37        if package_name.contains('@') {
38            error = true;
39            error!(
40                "`wapm remove` can not remove specific versions. Try to remove \"{}\" again without the version",
41                package_name
42            );
43            continue;
44        }
45
46        if manifest.remove_dependency(&package_name).is_some() {
47            info!("Removing \"{}\"", &package_name);
48        }
49    }
50
51    manifest.save()?;
52
53    if error {
54        Err(RemoveError::GenericError.into())
55    } else {
56        println!("Packages successfully removed!");
57        Ok(())
58    }
59}
60
61#[cfg(feature = "integration_tests")]
62impl RemoveOpt {
63    pub fn new(packages: Vec<String>) -> Self {
64        RemoveOpt { packages }
65    }
66}