diff --git a/src/commands/install.rs b/src/commands/install.rs index f6d604c..96f7ab1 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -1,10 +1,11 @@ use crate::utils::{helpers, node}; use helpers::Result; -pub fn run(name: String, dev: bool) -> Result<()> { - match dev { - true => node::install_dev(&name), - false => node::install(&name), +pub fn run(packages: Option, dev: bool) -> Result<()> { + match (packages, dev) { + (Some(packages), true) => node::install_dev(&packages), + (Some(packages), false) => node::install(&packages), + (None, _) => node::install_all(), }; Ok(()) diff --git a/src/lib.rs b/src/lib.rs index c665528..fff51e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,7 +86,7 @@ enum Cli { /// Install a Node package Install { /// The name of the package - name: String, + packages: Option, /// Install as devDependency #[structopt(long, short)] dev: bool, @@ -127,7 +127,7 @@ pub fn run() -> Result<()> { Cli::GithubActions { no_npm, project } => github_actions::run(no_npm, project)?, Cli::Graphql { name } => graphql::run(name)?, - Cli::Install { dev, name } => install::run(name, dev)?, + Cli::Install { dev, packages } => install::run(packages, dev)?, Cli::Remove(RemoveCommand::Config) => remove::config()?, Cli::Remove(RemoveCommand::Git) => remove::git()?, diff --git a/src/utils/node.rs b/src/utils/node.rs index fd7241d..efcb775 100644 --- a/src/utils/node.rs +++ b/src/utils/node.rs @@ -16,11 +16,19 @@ struct Npm {} struct Yarn {} impl Npm { - fn install(pkg: &str) { + fn install() { + println!( + "Installing dependencies using {manager}", + manager = "npm".green() + ); + helpers::run_command("npm", &["install"]); + } + + fn install_pkg(pkg: &str) { helpers::run_command("npm", &["install", "--save-exact", pkg]); } - fn install_dev(pkg: &str) { + fn install_dev_pkg(pkg: &str) { helpers::run_command("npm", &["install", "--save-exact", "--save-dev", pkg]); } @@ -34,11 +42,19 @@ impl Npm { } impl Yarn { - fn install(pkg: &str) { + fn install() { + println!( + "Installing dependencies using {manager}", + manager = "yarn".green() + ); + helpers::run_command("yarn", &["install"]); + } + + fn install_pkg(pkg: &str) { helpers::run_command("yarn", &["add", pkg]); } - fn install_dev(pkg: &str) { + fn install_dev_pkg(pkg: &str) { helpers::run_command("yarn", &["add", "--dev", pkg]); } @@ -81,25 +97,34 @@ fn success_message(code: InstallationType, pkg: &str) { ); } -pub fn install(pkg: &str) { +pub fn install_all() { let installer = match find_package_manager() { NodeInstaller::Npm => Npm::install, NodeInstaller::Yarn => Yarn::install, }; - packages(pkg).iter().for_each(|p| { + installer(); +} + +pub fn install(pkgs: &str) { + let installer = match find_package_manager() { + NodeInstaller::Npm => Npm::install_pkg, + NodeInstaller::Yarn => Yarn::install_pkg, + }; + + packages(pkgs).iter().for_each(|p| { installer(p); success_message(InstallationType::Install, p); }); } -pub fn install_dev(pkg: &str) { +pub fn install_dev(pkgs: &str) { let installer = match find_package_manager() { - NodeInstaller::Npm => Npm::install_dev, - NodeInstaller::Yarn => Yarn::install_dev, + NodeInstaller::Npm => Npm::install_dev_pkg, + NodeInstaller::Yarn => Yarn::install_dev_pkg, }; - packages(pkg).iter().for_each(|p| { + packages(pkgs).iter().for_each(|p| { installer(p); success_message(InstallationType::InstallDev, p); });