Skip to content

Commit

Permalink
feat(npm): add lockfile sync flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickard Natt och Dag committed May 23, 2022
1 parent dcf6031 commit a2a8eed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::utils::{helpers, node};
use helpers::Result;

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(),
pub fn run(packages: Option<String>, 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(())
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()?,
Expand Down
13 changes: 8 additions & 5 deletions src/utils/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -46,7 +49,7 @@ impl Npm {
}

impl Yarn {
fn install() {
fn install(_sync_lockfile: bool) {
println!(
"Installing dependencies using {manager}",
manager = "yarn".green()
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a2a8eed

Please sign in to comment.