Skip to content

Commit

Permalink
feat(cli): detect project NPM package manager on init (#10504)
Browse files Browse the repository at this point in the history
* feat(cli): improve init behavior to ask once

* chore(cli): bump version

* fix(clippy): fix clippy warning

* feat(cli): add `bun` and fix version bump msg

* refactor(cli): auto detect the package manager

* move function

* update change file

* update change file

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
  • Loading branch information
fu050409 and lucasfernog authored Aug 11, 2024
1 parent 416f845 commit 71d0064
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changes/improve-cli-init.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": patch:enhance
"@tauri-apps/cli": patch:enhance
---

Improve the `init` command behavior by detecting the project NPM package manager.
8 changes: 7 additions & 1 deletion tooling/cli/src/helpers/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl PackageManager {
let mut use_npm = false;
let mut use_pnpm = false;
let mut use_yarn = false;
let mut use_bun = false;

if let Ok(entries) = std::fs::read_dir(path) {
for entry in entries.flatten() {
Expand All @@ -48,11 +49,13 @@ impl PackageManager {
use_pnpm = true;
} else if name.as_ref() == "yarn.lock" {
use_yarn = true;
} else if name.as_ref() == "bun.lockb" {
use_bun = true;
}
}
}

if !use_npm && !use_pnpm && !use_yarn {
if !use_npm && !use_pnpm && !use_yarn && !use_bun {
return Vec::new();
}

Expand All @@ -67,6 +70,9 @@ impl PackageManager {
if use_yarn {
found.push(PackageManager::Yarn);
}
if use_bun {
found.push(PackageManager::Bun);
}

found
}
Expand Down
30 changes: 28 additions & 2 deletions tooling/cli/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::{
helpers::{
framework::{infer_from_package_json as infer_framework, Framework},
npm::PackageManager,
prompts, resolve_tauri_path, template,
},
VersionMetadata,
Expand Down Expand Up @@ -130,13 +131,18 @@ impl Options {
)
})?;

let detected_package_manager = match PackageManager::from_project(&self.directory).first() {
Some(&package_manager) => package_manager,
None => PackageManager::Npm,
};

self.before_dev_command = self
.before_dev_command
.map(|s| Ok(Some(s)))
.unwrap_or_else(|| {
prompts::input(
"What is your frontend dev command?",
Some("npm run dev".to_string()),
Some(default_dev_command(detected_package_manager).into()),
self.ci,
true,
)
Expand All @@ -148,7 +154,7 @@ impl Options {
.unwrap_or_else(|| {
prompts::input(
"What is your frontend build command?",
Some("npm run build".to_string()),
Some(default_build_command(detected_package_manager).into()),
self.ci,
true,
)
Expand All @@ -158,6 +164,26 @@ impl Options {
}
}

fn default_dev_command(pm: PackageManager) -> &'static str {
match pm {
PackageManager::Yarn => "yarn dev",
PackageManager::YarnBerry => "yarn dev",
PackageManager::Npm => "npm run dev",
PackageManager::Pnpm => "pnpm dev",
PackageManager::Bun => "bun dev",
}
}

fn default_build_command(pm: PackageManager) -> &'static str {
match pm {
PackageManager::Yarn => "yarn build",
PackageManager::YarnBerry => "yarn build",
PackageManager::Npm => "npm run build",
PackageManager::Pnpm => "pnpm build",
PackageManager::Bun => "bun build",
}
}

pub fn command(mut options: Options) -> Result<()> {
options = options.load()?;

Expand Down

0 comments on commit 71d0064

Please sign in to comment.