Skip to content

Commit

Permalink
feat(install): install all dependencies without argument
Browse files Browse the repository at this point in the history
Either runs `npm install` or `yarn install`, depending on lockfile or
config, if no package argument is provided
  • Loading branch information
Rickard Natt och Dag committed Apr 15, 2021
1 parent 0aaba04 commit 6570a73
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -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<String>, dev: bool) -> Result<()> {
match (packages, dev) {
(Some(packages), true) => node::install_dev(&packages),
(Some(packages), false) => node::install(&packages),
(None, _) => node::install_all(),
};

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ enum Cli {
/// Install a Node package
Install {
/// The name of the package
name: String,
packages: Option<String>,
/// Install as devDependency
#[structopt(long, short)]
dev: bool,
Expand Down Expand Up @@ -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()?,
Expand Down
45 changes: 35 additions & 10 deletions src/utils/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand All @@ -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]);
}

Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 6570a73

Please sign in to comment.