Skip to content

Commit

Permalink
feat: add global install
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickard Natt och Dag committed Apr 5, 2023
1 parent e3f43a9 commit 51d437f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::utils::{helpers, node};
use helpers::Result;

pub fn run(packages: Vec<String>, dev: bool, sync_lockfile: bool) -> Result<()> {
match (packages, dev, sync_lockfile) {
(packages, _, sync_lockfile) if packages.is_empty() => node::install_all(sync_lockfile),
(packages, true, _) => node::install_dev(&packages),
(packages, false, _) => node::install(&packages),
pub fn run(packages: Vec<String>, dev: bool, sync_lockfile: bool, global: bool) -> Result<()> {
match (packages, dev, sync_lockfile, global) {
(packages, _, _, true) => node::install_global(&packages),
(packages, _, sync_lockfile, false) if packages.is_empty() => {
node::install_all(sync_lockfile)
}
(packages, true, _, false) => node::install_dev(&packages),
(packages, false, _, false) => node::install(&packages),
};

Ok(())
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ enum Cli {
/// Run install with --lockfile-only (npm only)
#[clap(long, short)]
sync_lockfile: bool,
/// Install globally
#[clap(long, short)]
global: bool,
},

/// Remove any setup from add command
Expand Down Expand Up @@ -143,7 +146,8 @@ pub fn run() -> Result<()> {
dev,
packages,
sync_lockfile,
} => install::run(packages, dev, sync_lockfile)?,
global,
} => install::run(packages, dev, sync_lockfile, global)?,

Cli::Remove(RemoveCommand::Config) => remove::config()?,
Cli::Remove(RemoveCommand::Git) => remove::git()?,
Expand Down
25 changes: 24 additions & 1 deletion src/utils/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,30 @@ pub fn install_dev(pkgs: &[String]) {
messager.success("Installed (dev)");
}

pub fn uninstall(pkgs: &[String]) {
pub fn install_global(pkgs: &[String]) {
let packages = pkgs.join(", ");
let messager = Message::new(&packages);
let package_manager = NodeInstaller::default();

let mut arguments = match package_manager {
NodeInstaller::Npm => vec!["install", "--global"],
NodeInstaller::Yarn => vec!["global", "add"],
NodeInstaller::Pnpm => vec!["add", "--global"],
NodeInstaller::Bun => vec!["add", "--global"],
};

pkgs.iter().for_each(|p| {
arguments.push(p);
});

messager.install("Installing (globally)");

helpers::spawn_command(&package_manager.to_string(), &arguments)
.unwrap_or_else(|_| panic!("Failed to install {}", packages));

messager.success("Installed (globally)");
}

let packages = pkgs.join(", ");
let messager = Message::new(&packages);
let package_manager = NodeInstaller::default();
Expand Down

0 comments on commit 51d437f

Please sign in to comment.