Skip to content

Commit

Permalink
feat(node): add pnpm to install
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickard Natt och Dag committed Mar 7, 2023
1 parent 00032bd commit 34f0496
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
pub enum NodeInstaller {
Npm,
Yarn,
Pnpm,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
50 changes: 46 additions & 4 deletions src/utils/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lazy_static! {

struct Npm {}
struct Yarn {}
struct Pnpm {}

impl Npm {
fn install(sync_lockfile: bool) {
Expand Down Expand Up @@ -78,12 +79,47 @@ impl Yarn {
}
}

impl Pnpm {
fn install(_sync_lockfile: bool) {
println!(
"Installing dependencies using {manager}",
manager = "pnpm".green()
);
helpers::spawn_command("pnpm", &["install"]).unwrap();
}

fn install_pkg(pkg: &str) {
helpers::spawn_command("pnpm", &["add", "--save-exact", pkg]).unwrap();
}

fn install_dev_pkg(pkg: &str) {
helpers::spawn_command("pnpm", &["add", "--save-exact", "--save-dev", pkg]).unwrap();
}

fn uninstall(pkg: &str) {
helpers::spawn_command("pnpm", &["remove", pkg]).unwrap();
}

fn update() {
helpers::spawn_command("pnpm", &["update", "--interactive", "--latest"]).unwrap();
}

fn run(script: &str) {
helpers::spawn_command("pnpm", &["run", script]).expect("Could not start script");
}
}

fn find_package_manager() -> config::NodeInstaller {
match (fs::metadata("package-lock.json"), fs::metadata("yarn.lock")) {
match (
fs::metadata("package-lock.json"),
fs::metadata("yarn.lock"),
fs::metadata("pnpm-lock.yaml"),
) {
// Can't decide, use config
(Err(_), Err(_)) | (Ok(_), Ok(_)) => config::get().unwrap().node_installer,
(Ok(_), Err(_)) => NodeInstaller::Npm,
(Err(_), Ok(_)) => NodeInstaller::Yarn,
(Ok(_), Err(_), Err(_)) => NodeInstaller::Npm,
(Err(_), Ok(_), Err(_)) => NodeInstaller::Yarn,
(Err(_), Err(_), Ok(_)) => NodeInstaller::Pnpm,
_ => config::get().unwrap().node_installer,
}
}

Expand Down Expand Up @@ -122,6 +158,7 @@ pub fn install_all(sync_lockfile: bool) {
let installer = match find_package_manager() {
NodeInstaller::Npm => Npm::install,
NodeInstaller::Yarn => Yarn::install,
NodeInstaller::Pnpm => Pnpm::install,
};

installer(sync_lockfile);
Expand All @@ -131,6 +168,7 @@ pub fn install(pkgs: &str) {
let installer = match find_package_manager() {
NodeInstaller::Npm => Npm::install_pkg,
NodeInstaller::Yarn => Yarn::install_pkg,
NodeInstaller::Pnpm => Pnpm::install_pkg,
};

packages(pkgs).iter().for_each(|p| {
Expand All @@ -144,6 +182,7 @@ pub fn install_dev(pkgs: &str) {
let installer = match find_package_manager() {
NodeInstaller::Npm => Npm::install_dev_pkg,
NodeInstaller::Yarn => Yarn::install_dev_pkg,
NodeInstaller::Pnpm => Pnpm::install_dev_pkg,
};

packages(pkgs).iter().for_each(|p| {
Expand All @@ -157,6 +196,7 @@ pub fn uninstall(pkg: &str) {
let uninstaller = match find_package_manager() {
NodeInstaller::Npm => Npm::uninstall,
NodeInstaller::Yarn => Yarn::uninstall,
NodeInstaller::Pnpm => Pnpm::uninstall,
};

packages(pkg).iter().for_each(|p| {
Expand All @@ -170,6 +210,7 @@ pub fn update() -> Result<()> {
let updater = match find_package_manager() {
NodeInstaller::Npm => Npm::update,
NodeInstaller::Yarn => Yarn::update,
NodeInstaller::Pnpm => Pnpm::update,
};

updater();
Expand Down Expand Up @@ -209,6 +250,7 @@ pub fn run_script(script: &str) {
let script_runner = match find_package_manager() {
NodeInstaller::Npm => Npm::run,
NodeInstaller::Yarn => Yarn::run,
NodeInstaller::Pnpm => Pnpm::run,
};

script_runner(script);
Expand Down

0 comments on commit 34f0496

Please sign in to comment.