Skip to main content

wapm_cli/commands/
uninstall.rs

1use crate::config::Config;
2use crate::dataflow;
3use structopt::StructOpt;
4use thiserror::Error;
5
6#[derive(Clone, Debug, Error)]
7pub enum Error {
8    #[error("Packages may only be uninstalled by the package name.")]
9    NoAtSignAllowed,
10}
11
12#[derive(StructOpt, Debug)]
13pub struct UninstallOpt {
14    pub package: String,
15    /// Uninstall the package(s) globally
16    #[structopt(short = "g", long = "global")]
17    pub global: bool,
18}
19
20pub fn uninstall(options: UninstallOpt) -> anyhow::Result<()> {
21    let dir = match options.global {
22        true => Config::get_globals_directory()?,
23        false => Config::get_current_dir()?,
24    };
25    let uninstalled_package_names = vec![options.package.as_str()];
26
27    // do not allow the "@" symbol to prevent mis-use of this command
28    if options.package.contains('@') {
29        return Err(Error::NoAtSignAllowed.into());
30    }
31
32    // returned bool indicates if there was any to the lockfile. If this pacakge is uninstalled,
33    // there will be a diff created, which causes update to return true. Because no other change
34    // is made, we can assume any change resulted in successfully uninstalled package.
35    let result = dataflow::update(vec![], uninstalled_package_names, dir)?;
36
37    if !result {
38        info!("Package \"{}\" is not installed.", options.package);
39    } else {
40        info!("Package \"{}\" uninstalled.", options.package);
41    }
42
43    Ok(())
44}