From a2a8eed082bebf2eac19b348ee59919df29a3892 Mon Sep 17 00:00:00 2001 From: Rickard Natt och Dag Date: Mon, 23 May 2022 11:13:58 +0200 Subject: [PATCH] feat(npm): add lockfile sync flag --- src/commands/install.rs | 10 +++++----- src/lib.rs | 9 ++++++++- src/utils/node.rs | 13 ++++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/commands/install.rs b/src/commands/install.rs index 96f7ab1..1eb6b69 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -1,11 +1,11 @@ use crate::utils::{helpers, node}; use helpers::Result; -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(), +pub fn run(packages: Option, dev: bool, sync_lockfile: bool) -> Result<()> { + match (packages, dev, sync_lockfile) { + (Some(packages), true, _) => node::install_dev(&packages), + (Some(packages), false, _) => node::install(&packages), + (None, _, sync_lockfile) => node::install_all(sync_lockfile), }; Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 3f99af8..9f43fef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,6 +95,9 @@ enum Cli { /// Install as devDependency #[clap(long, short)] dev: bool, + /// Run install with --lockfile-only (npm only) + #[clap(long, short)] + sync_lockfile: bool, }, /// Remove any setup from add command @@ -137,7 +140,11 @@ pub fn run() -> Result<()> { Cli::GithubActions { no_npm, project } => github_actions::run(no_npm, project)?, Cli::Graphql { name } => graphql::run(name)?, - Cli::Install { dev, packages } => install::run(packages, dev)?, + Cli::Install { + dev, + packages, + sync_lockfile, + } => install::run(packages, dev, sync_lockfile)?, 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 7d417dc..74c6db7 100644 --- a/src/utils/node.rs +++ b/src/utils/node.rs @@ -16,12 +16,15 @@ struct Npm {} struct Yarn {} impl Npm { - fn install() { + fn install(sync_lockfile: bool) { println!( "Installing dependencies using {manager}", manager = "npm".green() ); - helpers::spawn_command("npm", &["install"]).unwrap(); + match sync_lockfile { + false => helpers::spawn_command("npm", &["install"]).unwrap(), + true => helpers::spawn_command("npm", &["install", "--lockfile-only"]).unwrap(), + }; } fn install_pkg(pkg: &str) { @@ -46,7 +49,7 @@ impl Npm { } impl Yarn { - fn install() { + fn install(_sync_lockfile: bool) { println!( "Installing dependencies using {manager}", manager = "yarn".green() @@ -115,13 +118,13 @@ fn install_message(code: InstallationType, pkg: &str) { println!("⌛ {text} {pkg}", text = text, pkg = pkg.blue()); } -pub fn install_all() { +pub fn install_all(sync_lockfile: bool) { let installer = match find_package_manager() { NodeInstaller::Npm => Npm::install, NodeInstaller::Yarn => Yarn::install, }; - installer(); + installer(sync_lockfile); } pub fn install(pkgs: &str) {