Skip to main content

wapm_cli/commands/
bin.rs

1use crate::config::Config;
2use crate::data::manifest::PACKAGES_DIR_NAME;
3use crate::dataflow::bin_script::BIN_DIR_NAME;
4use structopt::StructOpt;
5use thiserror::Error;
6
7#[derive(StructOpt, Debug)]
8pub struct BinOpt {
9    /// Get the global .bin dir
10    #[structopt(short = "g", long = "global")]
11    pub global: bool,
12}
13
14#[derive(Clone, Debug, Error)]
15pub enum BinError {
16    #[error("The directory \"{0}\" does not contain wapm packages.")]
17    NotWapmProjectDir(String),
18}
19
20pub fn bin(options: BinOpt) -> anyhow::Result<()> {
21    let mut root_dir = match options.global {
22        true => Config::get_globals_directory()?,
23        false => Config::get_current_dir()?,
24    };
25    root_dir.push(PACKAGES_DIR_NAME);
26
27    // for wapm bin -g, display the global path even if it does not exist
28    // otherwise error if the bin directory does not exist in the local directory
29    if !root_dir.exists() && !options.global {
30        return Err(BinError::NotWapmProjectDir(root_dir.to_string_lossy().to_string()).into());
31    }
32
33    root_dir.push(BIN_DIR_NAME);
34    let bin_dir = root_dir;
35    println!("{}", bin_dir.display());
36    Ok(())
37}